Why is the data from the database not showing to the textbox1.text? RRS feed

  • Question

  • I am learning from this website. This code is to show two fields (from the database addressbook.mdb which contains 5 records). The first field is FirstName and the second is SurName to the textbox1.text and textbox2.text


    Public Class Form1
     Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
      Dim con As New OleDb.OleDbConnection 'Con as a new connection object
      Dim dbProvider As String 'dbProvider is the technology used to make the connection
      Dim dbSource As String 'dbSource is the path of the database to be connected
      Dim ds As New DataSet 'ds is the new imaginary dataset to hold the data
      Dim da As OleDb.OleDbDataAdapter 'da is data adapter used to get the data from the connection object which is a copy of the database
      'and pass the date to the data set
      Dim sql As String 'sql is a variable who holds what is selected from data adapter
      dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
      dbSource = "Data Source =C:\My Documents\Downloads\AddressBook.mdb"
      If System.IO.File.Exists("C:\My Documents\Downloads\AddressBook.mdb") = True Then
       MsgBox("The database exist") 'Check if database file exist
       con.ConnectionString = dbProvider & dbSource ' open the connection
       con.Open() 'Now access to the Connection Object data
       sql = "SELECT tblContacts.FirstName, tblContacts.Surname FROM tblContacts" 'What data we want
       da = New OleDb.OleDbDataAdapter(sql, con) 'get those data from the connection object
       da.Fill(ds, "AddressBook") 'Fill them into the dataset object
       MsgBox("Database is now open")
       MsgBox("Database is now Closed")
       'Pass the data from the dataset to the textbox.text property so user can see them
       txtFirstName.Text = ds.Tables("AddressBook").Rows(0).Item(1)
       txtSurname.Text = ds.Tables("AddressBook").Rows(0).Item(2)
       MsgBox("No database found")
      End If
     End Sub
    End Class

    When I run it, I get the  msgbox: database file exit.

    msgbox databse now open

    msgbox database now close

    I cannot get the firstname or the surname

    It crash with an error index out of range, cannot find the collon 2.

    I know the index out of range error is when you give a number bigger than the index. I know also the database has 5 records in it. So what is I did wrong? Please help!

    Thank you!

    Thank for your help. I am newbie, I would really appreciate if you comment out your code. Thank You
    Friday, October 8, 2010 5:55 PM


  • Sorry for the trouble

    I did figure out what was wrong

    Thank you anyway!

    Thank for your help. I am newbie, I would really appreciate if you comment out your code. Thank You
    • Proposed as answer by Levi Domingos Friday, October 8, 2010 8:58 PM
    • Marked as answer by Liliane Teng Wednesday, October 13, 2010 8:55 AM
    Friday, October 8, 2010 6:13 PM