Error Connection with OleDB

Joined
Nov 22, 2002
Messages
18
Location
Singapore
HI, I have an error when i try to connect to my access database.

My code is as follows:
Code:
Imports System
Imports System.Data
Imports System.Data.OleDb


Public Class ListForm
    Inherits System.Windows.Forms.Form
 

    Private Sub ListForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strConnection As String = _
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
             & "C:\Reminder.mdb"
        Dim cn As OleDbConnection = New OleDbConnection(strConnection)
        cn.Open()
        cn.Close()

    End Sub
End Class

but when i click the form, this error appeared:

An unhandled exception of type System.Data.OleDb.OleDbException occurred in system.data.dll

Can somebody help me with this error?

I have installed MDAC 2.7, but still it cant work.

Is Northwind.mdb sample database is part of vs.net?because i cant find it.

Thanks for your help.
 
Finally I found the mistake.
Actually I type the path wrongly.
the sample below is for example only, not the real path.
I found it because I put Try catch:

Imports System
Imports System.Data
Imports System.Data.OleDb


Public Class ListForm
Inherits System.Windows.Forms.Form


Private Sub ListForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConnection As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& "C:\Reminder.mdb"
Dim cn As OleDbConnection = New OleDbConnection(strConnection)
Code:
try
        cn.Open()
catch test as system.exception
    messagebox.show(test.message)
     exit sub
end try
cn.Close()

End Sub
End Class

now, my other question is:
is there any vb.net function to check whether the connection is open or not, like connectionstate from vb6?
Thanks
 
Last edited by a moderator:
cn.State will give you the current state of the connection

e.g.

Code:
if cn.state = ConnectionState.Open then
Do Somthing
End if
 
Back
Top