Hi guys,

I’ve written an application and I’m trying to utilise a database behind it. I’ve created a mysql database, and table which all appears to work correctly. I appear to have connected to the db correctly too. And hit a bit of a brick wall. As a side-note, within Visual studio connected to the database using the add data source wizard, I then dragged and dropped my db table’s column onto my form that intends to display them all (6 textboxes, including primary key) and that now displays the first record of the table. I’m not sure how i did this so clarification would be appreciated! Haha. It confused me, because i did this as soon as I had setup the database with no visual basic code assisting, so there wasn’t even a connection string?!

One functionality i want to have is to press a button and cycle through the table’s records using the form by the unique id, how would I do this? I’m essentially asking how can I enter a number in a textbox which would then return me the relevant primary key and data attached to that record.

Help would be appreciated.

Dani AI

Generated

What happened when you dragged columns onto the form is VS creating designer objects (a typed DataSet/TableAdapter/BindingSource) so the controls were bound for you. , that’s why a record appears with no hand-written code. pointed you in the right direction if you want the designer approach; below are two practical alternatives you can apply immediately: a direct lookup-by-ID (good for single lookups) and a data-bound approach (good for cycling).

Direct lookup (button reads the ID textbox and runs a parameterized SELECT). This is simple, safe, and easy to wire into existing textboxes:

' Requires MySql.Data reference
Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
    Dim idValue As Integer
    If Not Integer.TryParse(txtId.Text.Trim(), idValue) Then
        MessageBox.Show("Enter a numeric ID")
        Return
    End If

    Dim connStr As String = "Server=localhost;Database=mydb;Uid=myuser;Pwd=mypassword;"
    Using conn As New MySql.Data.MySqlClient.MySqlConnection(connStr)
        conn.Open()
        Using cmd As New MySql.Data.MySqlClient.MySqlCommand("SELECT col1,col2 FROM mytable WHERE id=@id", conn)
            cmd.Parameters.AddWithValue("@id", idValue)
            Using rdr = cmd.ExecuteReader()
                If rdr.Read() Then
                    txtCol1.Text = If(IsDBNull(rdr("col1")), "", rdr("col1").ToString())
                    txtCol2.Text = If(IsDBNull(rdr("col2")), "", rdr("col2").ToString())
                Else
                    MessageBox.Show("Record not found.")
                End If
            End Using
        End Using
    End Using
End Sub

If you prefer to cycle through rows on the form, populate a DataTable once and bind it to a BindingSource. Then jump to a particular ID with bindingSource.Find("id", idValue) and set bindingSource.Position. Bound textboxes update automatically.

Quick troubleshooting: add the MySql.Data (Connector/NET) reference, avoid building SQL with string concat (use parameters), handle DBNull, and don’t leave credentials hard-coded in production. If you post your table/column names I can adapt the snippets to match them.

If you go in the properties of the project, in the settings tab you can see the connection string used.
Then, if you go in the designer part of the form, yu'll find all the struff Visual Studio created.

The easy way to do what you want is to add to the form a binding source, an a binding navigator. Here a how to video.

Hope this helps

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.