Connection Problems

asimeone18

New member
Joined
Oct 6, 2006
Messages
2
Programming Experience
Beginner
I'm still new at this, can anyone tell me if there is a problem with this code. All I want to do is establish a connection to a ms access database. I've tried everything i can think of :mad:

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Declare and populate the data table
Dim dt As New DataTable
Dim connStr As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = db1.mdb"
 
Dim sqlStr As String = "SELECT * FROM Some"
Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
dataAdapter.Fill(dt)
dataAdapter.Dispose()
 
dgDisplay.DataSource = dt
 
End Sub
 
Last edited by a moderator:
Hmmm try dataset

Try using a dataset, instead of using data table u can also use dataset and it is a widely used approach, the only thing u ve to do in currently written code is replace dataTable by DataSet.
dim ds as new dataset
datadapter.fill(ds,"Name of Source Table")
 
You don't have to use a Dataset if you don't feel that you need the functionality it provides. It may help if you can post the exception you are getting? From here it looks like the problem maybe....

"Data Source = db1.mdb"
What is db1.mdb's actual location, i.e the full file path.
 
I changed the datatable to dataset before, i did it again just to check, it still didnt work. I am getting the same error message which is An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

My database is located in the bin folder, i've tried it with the full path and no path, neither attempt has worked.

Thanks for the response
 
Ok, so we'll step through this a bit at a time. First, comment out the dataadapter.fill line, as we just want to test if the connection is succeeding. Then put in a messagebox to show if the connection is actually open.

VB.NET:
If ....ConnectionState = ConnectionState.Open Then...
MseeageBox.Show("The Connection Is Open")
End if.

If that is all ok, i would suspect that you haven't spelled the table name correctly in your SQL Statement, Check the casing of the table name. As a rule i don't provide the connection string directly to the dataadapters constructor as i feel it makes it difficult to see what is actually going on. I would normally have a separate connection object, and a separate OleDbCommand

In addition all potentially error causing code should be protected in a Try/Catch Block, then the error should be logged or at the very least shown to the user so they can copy it down. Unhandled expceptions are mostly completley Non-descriptive and of no help to any one. Get the actual exception message with

VB.NET:
MessageBox.Show(Ex.Message)

Or the inner exception, stack trace if you want to 'dig a little deeper'
 
This way....

Your code was like this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Declare and populate the data table
Dim dt As New DataTable
Dim connStr As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = db1.mdb"

Dim sqlStr As String = "SELECT * FROM Some"
Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
dataAdapter.Fill(dt)
dataAdapter.Dispose()

dgDisplay.DataSource = dt

End Sub

I feel the problem lies in the above lines of urs marked red, just remove (2nd line marked red) or comment it out and declare the connection string (Ist line marked red) like this
dim connStr as new OLEDB.OLEDBCONNECTION("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = db1.mdb")

i feel this should most probably fix ur problem, do feed back if it gets solved, i ll b glad
 
Back
Top