Please help,
I have declared variables to create the database connection and dataset and dataadapter in the form declaration.
I am then trying to define the database connection and open it so that i can use the dataadapter to fill the dataset. Here is my code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource= F:\AddressBook.mdb"
con.Open()
sql = "Select * From tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "AddressBook")
con.Close()
MaxRows = ds.Tables("AddressBook").Rows.Count
inc = -1
End Sub
Now the problem is that it throws an unhandled exception error at the con.open() line. This implies to me that it cannot connect to the database for some reason. I have made sure that the database is in the location and everything is fine.
I always seem to get this same error when trying to open database connections in this way in various different projects. However, if i use the con.open() statement when declaring and defining the database connection all in the form declaration, then i have no problems.
Could anyone please have a look at my code and help me with where i may be going wrong.
Much Appreciated!!
I have declared variables to create the database connection and dataset and dataadapter in the form declaration.
I am then trying to define the database connection and open it so that i can use the dataadapter to fill the dataset. Here is my code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource= F:\AddressBook.mdb"
con.Open()
sql = "Select * From tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "AddressBook")
con.Close()
MaxRows = ds.Tables("AddressBook").Rows.Count
inc = -1
End Sub
Now the problem is that it throws an unhandled exception error at the con.open() line. This implies to me that it cannot connect to the database for some reason. I have made sure that the database is in the location and everything is fine.
I always seem to get this same error when trying to open database connections in this way in various different projects. However, if i use the con.open() statement when declaring and defining the database connection all in the form declaration, then i have no problems.
Could anyone please have a look at my code and help me with where i may be going wrong.
Much Appreciated!!