0

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
5
  • if id.Text is empty you will get an error; you will also want to convert the text it contains to a number before you build the SQL. In the future post the actual error message (they are important info) rather than a link to a picture Commented Oct 28, 2013 at 18:44
  • I would recommend a : If NOT isdbnull(dr.getvalue(0))... for each one obviously incrementing the getvalue number for each one. Commented Oct 28, 2013 at 18:46
  • Unsolicited Advice: go ahead and change dr.getvalue(n) to dr.getvalue("nameOfColumn"). Its a better habit. Commented Oct 28, 2013 at 18:47
  • thanks! gonna check on that. Commented Oct 28, 2013 at 18:50
  • or change that to dr.item("nameofcolumn") a bit shorter... see answer below Commented Oct 28, 2013 at 18:52

3 Answers 3

4
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....

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.