1

I'm trying to implement a search engine in ASP.NET and VB.net. The search is from a .sdf database. I keep getting a:

System.NullReferenceException: Object reference not set to an instance of an object

The error occurs on the line

myDA.SelectCommand = cm

But I can't point out the source of error since my code looks clean. I will appreciate some Help

Private Sub searchButton_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
    registration.Visible = True
    con.Open()
    cm.Connection = con
    cm.CommandText = "SELECT * FROM records WHERE phone = " & phoneTextBox.Text
    cm.ExecuteNonQuery()
    myDA.SelectCommand = cm
    myDA.Fill(myDataSet, "records")

    If myDataSet.Tables("records").Rows.Count = 0 Then
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        Response.Write("Record not Found")
    Else
        TextBox1.Text = myDataSet.Tables("Records").Rows(0).Item("phone")
        TextBox2.Text = myDataSet.Tables("Records").Rows(0).Item("name")
        TextBox3.Text = myDataSet.Tables("Records").Rows(0).Item("id")
        TextBox4.Text = myDataSet.Tables("Records").Rows(0).Item("pin")
        TextBox5.Text = myDataSet.Tables("Records").Rows(0).Item("area")
        TextBox6.Text = myDataSet.Tables("Records").Rows(0).Item("subscription")
    End If
End Sub
Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
Tunan
  • 39
  • 3

1 Answers1

0

your error is here

          cm.CommandText = "SELECT * FROM records WHERE phone = " +& phoneTextBox.Text

phone numbers are string or varchar fields which means they have to be put in 'quotes'.since you havent the query isnt executing and returning you an empty result(0 rows). when you try access the first row i.e row[0] since there are no rows your are getting a nullrefrence exception

modify your code like this

     cm.CommandText = "SELECT * FROM records WHERE phone = '" +phoneTextBox.Text+"'"
Sujit.Warrier
  • 2,815
  • 2
  • 28
  • 47