Hi guys , i want to fill a textbox with a data from my mysql database and i dont know how to do it , i already searched without a result , if someone can help me i would appreciate it .

Recommended Answers

All 9 Replies

bump

xVent,

... Open DB connection
  ... Execute Query and assign result into var variable.
  txtData.Text=var;
  ...
  ...

wont what you say just show in the textbox the SQL string?

bump

Post your code work.

First i have the main form , then i have a button that opens a new form if you are connected into the database(when you click to connect it opens the new form)

and then in the new form i have this :

Imports System
Imports System.Data
Imports MySql.Data.MySqlClient

Public Class Form2

    Dim con As New MySqlConnection()
    Dim cmd As New MySqlCommand
    Dim adptr As New MySqlDataAdapter
    Dim table As New DataTable

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        con.ConnectionString = "Database=" & Form1.TextBox1.Text & ";Data Source=" & Form1.TextBox2.Text & ";User Id=" & Form1.TextBox3.Text & ";Password=" & Form1.TextBox4.Text & ""
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "SQL CODE" & ""
        adptr.SelectCommand = cmd
        adptr.Fill(table)

    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        con.ConnectionString = "Database=" & Form1.TextBox1.Text & ";Data Source=" & Form1.TextBox2.Text & ";User Id=" & Form1.TextBox3.Text & ";Password=" & Form1.TextBox4.Text & ""
        con.Open()
        cmd.Connection = con

        Dim var As String = "SQL CODE"
        TextBox1.Text = var


    End Sub
End Class
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient

Public Class Form2

    Dim con As New MySqlConnection()
    Dim cmd As New MySqlCommand
    Dim adptr As New MySqlDataAdapter
    Dim table As New DataTable

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        con.ConnectionString = "Database=" & Form1.TextBox1.Text & ";Data Source=" & Form1.TextBox2.Text & ";User Id=" & Form1.TextBox3.Text & ";Password=" & Form1.TextBox4.Text & ""
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "SQL CODE" & ""
        adptr.SelectCommand = cmd
        adptr.Fill(table)

    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        con.ConnectionString = "Database=" & Form1.TextBox1.Text & ";Data Source=" & Form1.TextBox2.Text & ";User Id=" & Form1.TextBox3.Text & ";Password=" & Form1.TextBox4.Text & ""
        con.Open()
        cmd.Connection = con

        Dim var As String = "SQL CODE"
        TextBox1.Text = var


    End Sub
End Class

after declaring var... u r not executing your SQL CODE...
Execute the code and then store the result in var.

xVent,
I think the following code will help you. I presume that you have a table emp(eno int, ename varchar(30)):

Dim con As New MySqlConnection()
    Dim cmd As New MySqlCommand
    Dim adptr As New MySqlDataAdapter
    Dim table As New DataTable
    ....
   con.ConnectionString="set_your_connectionString"
   adpter=new MySqlDataAdapter("select * from emp",con)
   adpter.Fill(table)
   TextBox1.Text=""
   Dim i as integer
   for i=0 to table.Rows.Count - 1
       TextBox1.Text = TextBox1.Text & "   " & table.Rows(i)("eno") & "  " & table.Rows(i)("ename")
   next
   ...

Thank you adatapost and others!

It worked!

Topic can close now.

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.