Where am I missing? A code that tries to recover data from an Access database with VB.NET

Asked

Viewed 38 times

1

Good night, My name is Lucas and may you help me with the following doubt.

I have a Windows Form in visual studio where I have a button that executes the code below. It reads an X value in the textbox1 and should return a Y value that is in the database.

If by typing 100 into texbox1 and executing the code it brings me the value 100-190421 in textbox2, but if I search 100-190421 in textbox1 I can’t bring either the value of the COD column or the value of the MYNAME column. **(this is my problem). **Although the data exists in the database it returns the error There’s no line at position 0, which is giving me a lot of headache, I have researched many but have not found yet an answer.

'''

    Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\bd.accdb;Jet OLEDB:Database Password=Strawberry21;")
    Dim cmd As New OleDb.OleDbCommand("Select * from Table1 where REG=" & TextBox1.Text, conn)
    Dim da As New OleDb.OleDbDataAdapter(cmd)
    Dim publictable As New DataTable

    conn.Open()

    Try

        da.Fill(publictable)
        TextBox2.Text = publictable.Rows(0).Item(2).ToString

    Catch ex As Exception

        MsgBox(ex.Message)

    Finally

        conn.Close()

    End Try

'''

Here’s a picture of the comic book.

inserir a descrição da imagem aqui

  • reg field is text right? use simple quotes in query: "Select * from Table1 where REG='" & TextBox1.Text &"'"

  • Hi Ricardo, the REG field is text yes, I tried but also did not work, returned the same error :(, but vlw :D

  • tried to run the query in the direct database? check if it has spaces, but a text field should have simple quotes always

  • 1

    I got Ricardo, his correction was right, I renewed the consultation and it worked super well, very grateful!! The first time I went to consult I missed a digit in the search value.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.