myConnection.Open()
    rtb_Address.Clear()
    txt_Name.Clear()
    Dim str As String
    str = "SELECT * FROM table1 WHERE (cus_ID = '" & txt_ID.Text & "')"
    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)

    dr = cmd.ExecuteReader()

    While dr.Read()
        rtb_Address.Text = dr("cus_Addr").ToString
        txt_Name.Text = dr("cus_Name").ToString
    End While
    myConnection.Close()

Error in dr = cmd.ExecuteReader()

dr is declared as OleDbDataReader

有帮助吗?

解决方案

cus_ID is probaly a numeric data type, but you try to query it with a string: (cus_ID = 'thevalue').

Just remove the enclosing ': (cus_ID = thevalue)

or better, use a parameterized query to prevent sql-injection.

其他提示

I would recommend the following:

 Using cmd As New OleDbCommand("SELECT * FROM table1 WHERE cus_ID = @ID", con)
    cmd.Parameters.AddWithValue("@ID", txt_ID.Text)

    dr = cmd.ExecuteReader()

    While dr.Read()
      rtb_Address.Text = dr("cus_Addr").ToString
      txt_Name.Text = dr("cus_Name").ToString
    End While

 End Using
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top