0

after i programming this code for fill text-box when combo-box selected Index changed

i got this error 'Object reference not set to an instance of an object.', whats can i do ??

Private Sub participant1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles participant1.SelectedIndexChanged

    Try
        Dim cmd As SqlCommand = New SqlCommand()
        Dim datareader As SqlDataReader = Nothing
        If Class1.sqlcon.State = ConnectionState.Open Then
            Class1.sqlcon.Close()
        End If
        Class1.sqlcon.Open()
        Dim query As String
        query = " select * from tparticipant where namea = '" & participant1.Text & "'"
        cmd = New SqlCommand(query, Class1.sqlcon)
        While datareader.Read
            If datareader IsNot Nothing Then
                ID.Text = datareader.GetInt32("ID")
                total.Text = datareader.GetInt32("total")
            End If

        End While

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try`
Mark
  • 8,140
  • 1
  • 14
  • 29
ahmad sattot
  • 3
  • 1
  • 4
  • At a quick glance, it looks like `datareader` is never set (other than to `Nothing`), so the `While datareader.Read` will fail. – Mark Jan 11 '15 at 00:33
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mark Jan 11 '15 at 00:34

2 Answers2

0

You need to assign the cmd.ExecuteReader() to your datareader. Thats where your NullReference is coming from.

datareader = cmd.ExecuteReader()
Sievajet
  • 3,443
  • 2
  • 18
  • 22
0
 Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim cn As New SqlClient.SqlConnection("Data Source=thee-pc;Initial Catalog=jobportal;Integrated Security=True")
    Dim cmd As New SqlClient.SqlCommand
    Dim tbl As New DataTable
    Dim da As New SqlClient.SqlDataAdapter
    Dim reader As SqlClient.SqlDataReader
    Try
        cn.Open()
        Dim sql As String
        sql = "select * from Register"
        cmd = New SqlClient.SqlCommand(sql, cn)
        reader = cmd.ExecuteReader
        While reader.Read
            Dim id = reader.Item("cid")
            ComboBox1.Items.Add(id)
        End While
        cn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim cn As New SqlClient.SqlConnection("Data Source=thee-pc;Initial Catalog=jobportal;Integrated Security=True")
    Dim cmd As New SqlClient.SqlCommand
    Dim tbl As New DataTable
    Dim da As New SqlClient.SqlDataAdapter
    Dim reader As SqlClient.SqlDataReader
    Try
        cn.Open()
        Dim sql As String
        sql = "select * from register where cid ='" + ComboBox1.Text + "'"
        cmd = New SqlClient.SqlCommand(sql, cn)
        reader = cmd.ExecuteReader
        While reader.Read
            TextBox1.Text = reader.Item("cname")
            TextBox2.Text = reader.Item("dob")

        End While
        cn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

End Class

Rizier123
  • 58,877
  • 16
  • 101
  • 156