Input String was not in correct format - Error

polar

Member
Joined
Feb 12, 2009
Messages
7
Location
Ireland
Programming Experience
Beginner
Hi, I keep getting the following error: "Input String was not in correct format" and it relates to the following line of code:

VB.NET:
objRow1 = objDataSet.Tables("tblStudent").Rows.Find(selA)

I cannot grasp why this error keeps occuring. I have definately used this format (i.e. datarow = dataset.tables("tablename").rows.find(something)) elsewhere and have had no problems.

What I'm doing is reading the values in a listbox and trying to find their related IDs. The values in the list box are names from one particular table in a database and I need to get their IDs to save to a second table.

Here is my code:
VB.NET:
objCurrentRow1 = objDataSet.Tables("tblAttendance").NewRow

            Dim selA As String
            For iLoop = 0 To lstStudents.Items.Count - 1
                If lstStudents.Items(iLoop) IsNot Nothing Then
                    selA = lstStudents.Items(iLoop)
                    objRow1 = objDataSet.Tables("tblStudent").Rows.Find(selA.ToString)
                    objCurrentRow1.Item("StudentID") = selA
                End If
            Next iLoop
The loop works fine, I've ran it on its own, minus the 2lines of code towards the end. It reads the data from the listbox(about 20 names)

Any suggestions as to what may be causing this error??Thanks
 
Hey,
Thanks for that suggestion.

I have tried:
VB.NET:
Dim selA As Integer
selA = CStr(lstStudents.Items(iLoop))
and;
VB.NET:
Dim selA As Integer
selA = CStr(lstStudents.Items(iLoop).ToString)
and;
VB.NET:
Dim selA as String
selA = CInt(lstStudents.Items(iLoop).ToString)
and;
VB.NET:
Dim selA As String
selA = CStr(lstStudents.Items(iLoop).ToString)

I kept getting the same "Input string not in correct format" error and then when I started using Int.... I got "Cannot covert string "Clancy" to Integer" ("Clancy" being the first record in the list box)

Can you see anything else wrong with what I'm doing? Is the logic of my code wrong?
Any further suggestions would be really appreciated.

Thanks,
Laura
 
I'd revamp what youre doing completely.. the only thing is, I cannot work out what it is youre trying to do:

What I'm doing is reading the values in a listbox and trying to find their related IDs. The values in the list box are names from one particular table in a database and I need to get their IDs to save to a second table.
This seems contradictory; youre attempting to find child rows of a parent, or youre trying to store an ID in a table after the user picks a friendly name?

Please provide more info as to what youre trying to do
 
more info

Well, I got rid of that error I was getting by changing the values of the listbox from names to IDs.

To explain, I'm trying to develop an attendance recording system for a small school. On this particular form, the user selects a class to record attendance for, eg. ClassA. Then depending on that selection, a listbox is filled with all the names of students in that class. (I also have a listbox that is filled with all their studentIDs and that's what I use for saving, but from the user's point of view that listboxs visible property is set to invisible as the user does not need to see studentIDs)Then the user must tick off some checkboxes to identify whether the student is present or not and then hit the save button.

Since I got rid of that error, I have managed to get the saving working but only to an extent.
VB.NET:
             'Select the next row
            objCurrentRow1 = objDataSet.Tables("tblAttendance").NewRow 
            mintStudentID = objDataSet.Tables("tblStudent").Rows.Count
            Dim selA As String
            For iLoop = 0 To lstID.Items.Count - 1
                If lstID.Items(iLoop) IsNot Nothing Then
                    selA = (lstID.Items(iLoop).ToString)
                    'objRow1 = objDataSet.Tables("tblStudent").Rows.Find(selA)
                    objCurrentRow1.Item("StudentID") = selA
                    objCurrentRow1.Item("AttendanceDate") = dateToday

                End If
            Next iLoop

            Dim objChk As Object
            Dim Checked As Boolean = False
            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

            objDataSet.Tables("tblAttendance").Rows.Add(objCurrentRow1)

            objAttendanceDA.Update(objDataSet, "tblAttendance")
            objDataSet.Tables("tblAttendance").AcceptChanges()

            MessageBox.Show("Today's Attendace for 5th Year 1 has been saved.", "Saved", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)
The messsage box "saved" will appear but only one row is being sent to the database. On the windows form there could be over 20students and thus 20checkboxes so I need 20 new rows to save at once.
Is this line of code the fault?
VB.NET:
'Select the next row
            objCurrentRow1 = objDataSet.Tables("tblAttendance").NewRow
I hope I have explained what I'm trying to do and maybe you could offer some advice?

Thanks
 
Back
Top