简体   繁体   中英

How to get data from database to textbox on VB.net

hi im new in Visual basic. I have a button that when its clicked, its gonna find the student via their ID inputted by the user and its gonna output the data to the textfields. I'm not pretty sure if im doing this right. because i'm getting this error [image] >> http://img812.imageshack.us/img812/7650/gq0z.png

btw here's my code so far. can someone help me please? thanks!

        cmd.CommandText = "Select * from Student where Student_id = '" & id.Text & "'"
        cmd.Connection = db

        dr = cmd.ExecuteReader

        Try
            dr.Read()
            id.Text = dr.GetValue(0)
            Lname.Text = dr.GetValue(1)
            Fname.Text = dr.GetValue(2)
            Mname.Text = dr.GetValue(3)
            datet.Text = dr.GetValue(4)
            age.Text = dr.GetValue(5)
            male.Text = dr.GetValue(6)
            female.Text = dr.GetValue(7)
            status.Text = dr.GetValue(8)
            staddress.Text = dr.GetValue(9)
            cityAdd.Text = dr.GetValue(10)
            dr.Close()

        Catch ex As Exception
            MsgBox("" + ex.Message)
            dr.Close()
        End Try
cmd.CommandText = "Select * from Student where Student_id = '" & id.Text & "'"

change to:

if IsNumeric(id.text) Then
cmd.CommandText = "Select * from student where Student_id=@p1"
cmd.Prepare
cmd.Parameters.AddWithValue("@p1", id.text)
dr = cmd.ExecuteReader
....
Else
Exit Sub
End If

You can do it this way, or

 dr = cmd.ExecuteReader

    Try
       with dr
        .Read()
        id.Text = .GetValue(0)
        end with
        dr.Close()

or

with dr
    .read
    id.text = .item("id")
    .close

easier to read....

First add a reference, if you are using MySQL database put it bet. the class

Dim Connection As MySqlConnection
Dim command As MySqlCommand

Put this to your textbox

Connection = New MySqlConnection
Connection.ConnectionString = "Server=localhost;port=3306;userid=root;password=root;database=databasename"
Dim reader As MySqlDataReader

the root is default

Try
  Connection.Open()
  Dim query As String
  query= "Select * from Databasename.tablename where fieldname='" & textbox1.text & "'"
  Command = New MySqlCommand(query, Connection)
  reader = Command.ExecuteReader
  While reader.Read
    Dim sname As String
    sname = reader.GetString("Fieldname")
    textbox1.Items.Add(sname)
  End While
  Connection.Close()
Catch e MySqlException
  MsgBox (ex.Message)
Finally
  Connection.Dispose
End Try

If you are using Mysql Database, first add reference. To add reference of MySql Database

  1. Go to your solution explorer and rightclick on your project name
  2. Find add->references

    a window will be opened

  3. In that window,under Assemblies select framework.
  4. On right side there will be a list find and select Microsoft.VisualBasic.Compatability.Data
  5. In extensions, find and add MySql.Data and MSDATASRC
  6. Click OK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM