Thanks a million.

Can I just run something else by you? I actually got code from
yourself a few weeks ago and I'm trying to apply it now to another
area of my project (as follows, )
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSave.Click
             objCurrentRow1 = objDataSet.Tables
("tblAttendance").NewRow
                Dim selA As String = lstStudents.Items.ToString
                Dim BRow As DataRow = objDataSet.Tables
("tblStudent").Rows.Find(selA)
                If BRow IsNot Nothing Then
                    'use the data relationship to find all matching
student rows
                    Dim studentRows() As DataRow = BRow.GetChildRows
("tblStudent")
                    If studentRows.Length > 0 Then
                        For Each student As DataRow In studentRows
                            Dim strID As String = student("StudentID")
                            'add this item
                            objCurrentRow1.Item("StudentID") = strID
                        Next
                    End If

What I'm aiming to do is relate the names in the listbox lstStudents
to their associated student IDs in the table tblStudent and then
populate another table with those studentIDs.

I'm clearly doing something wrong though cos when I run it it says
column StudentID cannot be null, so my code isnt working.

Any advice??

Just for context, I'll supply the following code also

        objDataSet.Clear()

        objStudentDA.FillSchema(objDataSet, SchemaType.Source,
"tblStudent")
        objStudentDA.Fill(objDataSet, "tblStudent")

        objClassDA.FillSchema(objDataSet, SchemaType.Source,
"tblClass")
        objClassDA.Fill(objDataSet, "tblClass")

        objAttendanceDA.FillSchema(objDataSet, SchemaType.Source,
"tblAttendance")
        objAttendanceDA.Fill(objDataSet, "tblAttendance")

        objDataSet.Relations.Clear()
        objDataSet.Relations.Add("Class2Student", _
                                 objDataSet.Tables("tblClass").Columns
("ClassID"), _
                                 objDataSet.Tables
("tblStudent").Columns("ClassID"))
        objDataSet.Relations.Add("Student2Attendance", _
                                 objDataSet.Tables
("tblStudent").Columns("StudentID"), _
                                 objDataSet.Tables
("tblAttendance").Columns("StudentID"))

thanks.

On Feb 5, 6:19 am, Cerebrus <[email protected]> wrote:
> I guess I can identify a couple of problems with the code. My version
> would be:
>
> ---
> For Each objChk in GroupBox2.Controls
>   If TypeOf objChk is CheckBox Then
>     Dim chk as CheckBox = DirectCast(objChk, CheckBox)
>     If chk.Checked = True Then
>       objCurrentRow1.Item("Present") = True
>     End If
>   End If
> Next
> ---
>
> The differences are:
> 1. You aren't storing the casted CheckBox into anything.
> 2. You aren't setting the value of the column "Present" to a boolean,
> rather you are setting to the object of the CheckBox itself.
>
> On Feb 4, 10:16 pm, Laura <[email protected]> wrote:
>
>
>
> > Hi,
> > I'm trying to loop through some checkboxes and save the values to a
> > row in the database (as below)
>
> > Dim objChk As Object
> > Dim Checked As Boolean = False
> >        For Each objChk In GroupBox2.Controls
> >                   If TypeOf objChk Is CheckBox AndAlso DirectCast
> > (objChk, CheckBox).Checked Then
> >                         objCurrentRow1.Item("Present") = objChk
> >                     End If
> >         Next
>
> > I've ran the project with breakpoints around this mark and it appears
> > to loop through 3/4 times and then I get the following error:
>
> > "Unable to cast object of type 'System.Windows.Forms.CheckBox' to type
> > 'System.IConvertible'.Couldn't store <System.Windows.Forms.CheckBox,
> > CheckState: 1> in Present Column.  Expected type is Boolean."
>
> > Can you identify any simple thing wrong with the above code that would
> > rectify this error??- Hide quoted text -
>
> - Show quoted text -

Reply via email to