Object reference not set to an instance of an object.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcmahon
    New Member
    • Feb 2009
    • 9

    Object reference not set to an instance of an object.

    I need help with an error I'm getting;
    "Object reference not set to an instance of an object." The error occurs at the line :
    Code:
    Dim intselA As Integer = CInt(lstStudents.SelectedItem.ToString)
    The items in lstStudents is text. StudentID is an autonumber.
    What I'm trying to do is populate a the database with all the studentID's of the names in the list box. (i.e. every name in the list box has a corresponding studentID)

    Your help would be really appreciated.

    Thanks
    Code:
                    Dim intselA As Integer = CInt(lstStudents.SelectedItem.ToString)
                    Dim BRow As DataRow = objDataSet.Tables("tblStudent").Rows.Find(intselA)
                    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
                    End If
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You are not verifying that lstStudents.Sel ectedItem is a valid object (in vb.net you would check if it equals Nothing)

    The default selected index is -1 (invalid) to note that nothing is selected in the control. This comes up when applying a datasource to the control as well as when a user de-selects all the items

    Comment

    Working...