app.config file connection string when using c# Winforms to MSAccess database

G

GrantS

I am unable to get the connection to work with using the app.config
file.
the connection works when I use 'in line' connection as below:
------------------------------------------------------------------
I have a declaration in the class
private OleDbConnection con;

Then code in a procedure as below:
1. This works:
con = new OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=;Data
Source=C:\\Data\\C#Projects\\ProPay\\ProPay.mdb;");
con.Open();

2. This does not work:
con = new OleDbConnection(ConfigurationSettings.AppSettings["DBConnectionString"]);
con.Open();

The App.config file that is being called here is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- User application and configured property settings go
here.-->
<add key="DBConnectionString"
value="Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=;Data
Source=C:\\Data\\C#Projects\\ProPay\\ProPay.mdb;" />
</appSettings>
</configuration>

-----------------------------------------------------------

The error message provided (VisStudio 2003) is:
A first chance exception of type 'System.InvalidOperationException'
occurred in system.data.dll

Additional information: The ConnectionString property has not been
initialized.
-------------
I have tried using the connection string generated through using oleDB
connection in the Dynamic properties of the OleDBConnection object
that I dragged onto a form. This did not work.
I have tried playing with the syntax a myriad of ways (ie single
backslashes) - no luck.
I used a connection string I generated from oleDBconnection object
dragged onto a form - no luck.
I have called the config file by MYApp.exe.config

If someone can point me in the right direction, I would be most
greatful. If there is a site with information relating to Data access
Using C# and MSAccess, this would be great too.
I have not had much joy using parametized queries using
oleDBCommand.CommandType.StoredProcedure. Can this be done or can one
only use "texttype?"
I have adapted info from
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=3
which relates to data Access and SQL databases but this uses stored
procedures which is where I am not winning.

Thanks in anticipation.

Grant
 
R

Roman S. Golubin

Hello GrantS!

Try app.config with one backslash in path:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- User application and configured property settings go
here.-->
<add key="DBConnectionString"
value="Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=;Data
Source=C:\Data\C#Projects\ProPay\ProPay.mdb;" />
</appSettings>
</configuration>


--
WBR, Roman S. Golubin
ICQ UIN 63253392
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top