OleDbConnection problems

minn

Active member
Joined
Apr 10, 2006
Messages
37
Programming Experience
Beginner
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!!
 
Dim con As OleDb.OleDbConnection = Nothing

con = New OleDbConnection"PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource= F:\AddressBook.mdb")

con.Open()

Try above code! If cannot work tell me~
 
try this:

VB.NET:
Try
 con.Open()
Catch sqlEx As OLEDBException
 MessageBox.Sho sqlEx.ToString
End Try

That should give you a MUCH more specific error.

-tg
 
Thanks guys,

I have tried both suggestions. With the first, i still get the error and with the try catch statement I get all sorts of other errors and more errors highlighted on my code screen and dont get any understandable error messages as you would expect.

Any other ideas?
 
Abit more information,

The error generated from the try, catch statement:

system.Data.oleDb.OleDbException could not find installable ISAM ..............................
 
VB.NET:
"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source=C:\SAMPLE.mdb;Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False"

Try using the above Connection String, dont forget to change the DB name.. "Data Source=C:\SAMPLE.mdb"

Bye
 
Thanks everyone, but problem solved:

con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource= F:\AddressBook.mdb"

should be a space in DataSource( i.e Data Source)

Yes, Yes, I know im an idiot!!
 
Back
Top