hi all:
I am a newbie , trying to connect my access to vb.net express edition I wrote the following code:

Imports System.Data
Public Class Form1

Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim con As OleDb.OleDbConnection
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0 ; Data Source = "C:\Documents and Settings\shassanzadeh\Desktop\Sara\extra\AddressBook.mdp"

End Sub
End Class


but the compiler gives me error for this line:
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0 ; Data Source = "C:\Documents and and Settings\shassanzadeh\Desktop\Sara\extra\AddressBook.mdp"


the erros says:Character constant must contain exactly one character.
can you guys help me ...

Dani AI

Generated

The compiler error "Character constant must contain exactly one character" was caused by unmatched double quotes inside a VB string literal: the compiler saw the first quote, then an unexpected inner quote (the one around the file path) and treated the remainder incorrectly. The quick remedies are either remove the extra quotes around the Data Source value (as pointed out) or escape an embedded quote by doubling it inside the string. Either approach prevents the string from being cut short by an unexpected quote.

The runtime OleDbException on the open call (“cannot find the file”) is a separate, common problem. Typical causes: the filename extension is wrong (Access DBs are usually .mdb or .accdb, not .mdp), the path is incorrect or the file was moved, the app lacks file permissions for that folder, or Visual Studio is opening a copied file from bin\Debug instead of the original (check the file’s “Copy to Output Directory” property). Verifying the exact file the code tries to open at runtime is the fastest way to find the culprit.

A practical check-and-open pattern (illustrative) — verify the path, choose the correct provider for the file type, and ensure the connection is always disposed:

Dim dbPath As String = "C:\path\AddressBook.mdb"
If Not System.IO.File.Exists(dbPath) Then Throw New System.IO.FileNotFoundException("DB not found", dbPath)

Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath  ' use ACE for .accdb; Jet for older .mdb
Using conn As New System.Data.OleDb.OleDbConnection(connString)
    conn.Open()
    ' perform queries
End Using

Checklist: remove/escape inner quotes, confirm extension (.mdb/.accdb), test System.IO.File.Exists at runtime, inspect “Copy to Output Directory” behavior, and if running on a 64‑bit machine either install the ACE driver or target x86. Thanks to for the initial diagnosis and for the sample reference; the thread shows resolved the immediate issue.

Recommended Answers

All 9 Replies

You don't need to surround the database path in quotes inside a connection string. The key/value pairs are all delimited. It should look like this instead.

con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\shassanzadeh\Desktop\Sara\extra\AddressBook.mdp"

thanks hamrick
I did that now I have this code :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'seting the data connection
Dim sql As String
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\shassanzadeh\Desktop\Sara\extra\AddressBook.mdp"
'setting the data Adaptor and Data set

con.Open()
Sql = "SELECT * FROM tblContacts"
da = New OleDb.OleDbDataAdapter(Sql, con)
MsgBox("connection open")
con.Close()
MsgBox("connection close")

End Sub

and no syntex error but while I compile it doesnt show any thing and after that I get this error"OleDbException was unhandled "and it poits to a yellow line on con.open()
well according to this link because I am using express edition , I should set my project refrences to System.Xml.dll but here there is no such thing it is only system.xml what do you think I should do?

because I am using express edition , I should set my project refrences to System.Xml.dll but here there is no such thing it is only system.xml

System.Xml and System.Xml.dll are the same thing in the reference picker. But that reference should have been added when you made a windows forms project...

i changed that but still it gives errors on the line con.open()
I redo it again also I enclude at the top system.Xml but still it doesnt work. :'(

What message does the exception give you?

it says it cant find this file while I double checked and the path is 100%correct

Put it somewhere else without all of the extra nesting, like C:\. Make sure that the extension is right. If this is access, it should be *.mdb, not *.mdp, I think. When it tells you it can't find the file, it can't find the file. :) That means the connection string is wrong somehow.

Read this article and you can even download a working sample which uses Connection to Access DB through ASP. Click here

thankx hamrick it is solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.