andre_ss6
andre_ss6

Reputation: 1255

VB: Fill multiple textboxes with data from MySQL DB

So, I have this program which has to store a lot of different info from users and shows it through Textboxes, Numerics, etc when the Form loads. At first I thought it would be easy, but as soon as I started writing the code I figured that if I would do the easy way (the way I know) I would have to write hundreds of Subs, each one with a MySQL query and then assign, one by one, the values to their respective textboxes, comboboxes, etc.

So, how can I pull data from multiple rows from a MySQL DB and then assign the data from each one of those rows to textboxes?

This is what I have now, which works fine, but only for getting one single value from the DB:

Imports MySql.Data.MySqlClient

Public Class GetInfo


    Public Shared Sub Run()

        Dim reader As MySqlDataReader
        Dim result As String

        Dim Query_Read As String = "Select Nome FROM dk_db_sql_yog." & Username
        Dim Cmd_Read_Name As New MySqlCommand(Query_Read)
        Cmd_Read_Name.Connection = Connect
        reader = Cmd_Read_Name.ExecuteReader()
        If reader.Read() Then
            If reader.IsDBNull(0) Then
                result = ""
            Else
                result = reader.GetString(0)
            End If
        End If

        Form1.Name_Textbox.Text = result
        reader.Close()

     End Sub

End Class

Upvotes: 0

Views: 5667

Answers (1)

Ruben_PH
Ruben_PH

Reputation: 1812

As I understand your question, you may use a datatable:

Dim reader As MySqlDataReader
Dim result As New Datatable

Dim Query_Read As String = "Select Nome, Nome1, Nome2 FROM dk_db_sql_yog." & Username
Dim Cmd_Read_Name As New MySqlCommand(Query_Read)
Cmd_Read_Name.Connection = Connect
result.Load(Cmd_Read_Name.ExecuteReader)

With Form1
  For Each dtrow AS DataRow in result.rows
     .Name_Textbox1.Text = dtrow(0)
     .Name_Textbox2.Text = dtrow(1)
     .Name_Textbox3.Text = dtrow(2)
  Next
End With

Upvotes: 1

Related Questions