Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
whenever i run my application i got "The ConnectionString property has not been initialized." this error recently i changed my connection string first it was a local connection string but now im created dyanamic connection string using web.config file , while when its a local connection application running fine but now its throw above exception

XML
web.config file : 

<configuration>
  <connectionstrings>
    <add name="AccessConnectionString" connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|BillAndReceipt.mdb;Persist Security Info=False;" providername="System.Data.OleDb" />
  </connectionstrings>
</configuration>


What I have tried:

my previous connection string is :
OleDbConnection cnn=new OleDbConnection ();
string connectionString = null;
connectionString = @" provider=Microsoft.ACE.OLEDB.12.0; Data Source=E:\Database\BillAndReceipt.accdb;Persist Security Info=False;";

my latest connection string :

connectionString = ConfigurationSettings.AppSettings["AccessConnectionString"];


cnn.ConnectionString = connectionString;
cnn.Open();


exeception is give cnn.open(); line
Posted
Updated 20-Mar-16 1:07am
v2

1. String file path should contain a \ after |DataDirectory| as in...
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BillAndReceipt.mdb;Persist Security Info=False;

2. Also, you seem to have different extensions for local and DataDirectory, with accdb and mdb on the other one.
Maybe you want to look into your filename extension as well.

3. Finally, there are different ways to retrieve the connection string from a web.config file using c# depending on the method you store it.
There is a good article on that at
Store Connection String in Web.config - ConnectionStrings.com[^]
There seems to be a ptoblem with that too in the c# code. only make sure too your c# header has
C#
using System.Configuration;
 
Share this answer
 
v5
Comments
Atul Rokade 20-Mar-16 0:26am    
i changed in web.config file put
add name="AccessConnectionString" connectionString="Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BillAndReceipt.mdb;Persist Security Info=False;" providerName="System.Data.OleDb"

but still got same error
Homero Rivera 20-Mar-16 0:29am    
Could you be messing with your file extension? I see you use .mdb and then .accdb
What is the right extension?
Atul Rokade 20-Mar-16 0:32am    
@Homero : im using access database and my file extension is .accdb
Homero Rivera 20-Mar-16 0:34am    
your connection string in web config has .mdb at the end
<add name="AccessConnectionString" connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|BillAndReceipt.mdb;Persist Security Info=False;" providername="System.Data.OleDb">

change BillAndReceipt.mdb to BillAndReceipt.accdb
Atul Rokade 20-Mar-16 0:36am    
@Homero : i changed it but still got same error
Try with below code:

Add following connection string web.config
XML
<configuration>
  <connectionstrings>
    <add name="AccessConnectionString"             connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|BillAndReceipt.mdb;Persist Security Info=False;" providername="System.Data.OleDb" />
  </connectionstrings>
</configuration>

Add System.Configuration as a reference in your current project. Once reference is added, fetch connection string from web.config like below:
C#
// Add a using directive at the top of your code file
using System.Configuration;

// Get connection string value from web.config
string connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;

// Create OleDbConnection object with connection string
OleDbConnection cn = new OleDbConnection(); 
cn.ConnectionString = connectionString;
cn.Open();
 
Share this answer
 
v6
Comments
Atul Rokade 20-Mar-16 1:29am    
manas bhai : i was tried this also but ConfigurationManager doesnot exist error is come
[no name] 20-Mar-16 1:32am    
Did you add System.Configuration dll reference in your current project. I am sure that you missing that one otherwise it shouldn't show "ConfigurationManager doesnot exist". Cross check it and add the reference.
Atul Rokade 20-Mar-16 1:43am    
Manas_kumar bhai : yes bhai im not added configurationManager.dll file i added that but now different error is coming

An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'.
Atul Rokade 20-Mar-16 1:49am    
i changed connection string now

add name="AccessConnectionString" connectionString="Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BillAndReceipt.accdb;Persist Security Info=False;Provider=SQLOLEDB" providerName="System.Data.OleDb.OleDbConnection"

now it giving me cnn.open() this line

Invalid connection string attribute
add name="AccessConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BillAndReceipt.accdb;Persist Security Info=False;providerName="System.Data.OleDb"


guys this string i think working fine hopefully....

thank you manas_kumar sir and homero sir
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900