"Error in Line 4- System.NullReferenceException: 'Object reference not set to an instance of    an object."

       Private Sub btnsearch3_Click(sender As Object, e As EventArgs) Handles btnsearch3.Click

        Dim vSearch As String = InputBox("Enter Employee Id to search the details:")

        If vSearch <> "" Then
          cmdSearch.CommandText = "SELECT * from Employee_Details WHERE Emp_Id =" & CInt(vSearch)
           cmdSearch.Connection = cnnOLEDB

       Dim rdrOLEDB As OleDbDataReader = cmdSearch.ExecuteReader

        If rdrOLEDB.Read = True Then
            txteid1.Text = rdrOLEDB.Item(0).ToString
            txtepn2.Text = rdrOLEDB.Item(1).ToString
            txtadd3.Text = rdrOLEDB.Item(2).ToString
            txtphon4.Text = rdrOLEDB.Item(3).ToString
            dtpdobirth5.Value = rdrOLEDB.Item(4).ToString
            txtapt6.Text = rdrOLEDB.Item(5).ToString
            dtpduty7.Value = rdrOLEDB.Item(6).ToString
            dtpdoa8.Value = rdrOLEDB.Item(7).ToString

            rdrOLEDB.Close()
            MsgBox("Record found")
            Exit Sub
        Else
            rdrOLEDB.Close()
            MsgBox("Record not found")
            Exit Sub
        End If
    Else
        MsgBox("Enter search value.")
        Exit Sub
    End If

Recommended Answers

All 2 Replies

It could be any of the objects you are trying to use but possibly have not defined. I can't say because you haven't included any definitions. Those objects may be defined elsewhere or they may not. Or they are defined elsewhere and are out of scope. Note that you haven't told us what line threw the error. Line 4 is blank so that isn't it. A common error is to declare a reference to an object instead of creating the object (omitting the New keyword).

To get error line number I would enclose inside a try-catch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.