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
http://www.homeandlearn.co.uk/NET/nets12p6.html
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")
con.Close()
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)
Else
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