Click here to Skip to main content
15,850,298 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
I am getting this exception(The ConnectionString property has not been initialized) while connecting to my sql server database.

app.config code:

    <add key="databasepath" value="Data Source=ABC-PC\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True"/>

code behind code:

SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationSettings.AppSettings["databasepath"];


what i am doing wrong?
Updated 26-Sep-19 4:23am
Agent__007 29-Dec-14 6:00am    
Are you sure the config section you have shown is for the same C# project from which you are trying to open a connection?

Hi Uday,
string abc = ConfigurationSettings.AppSettings["databasepath"];

now pass it into sqlconnection.

try it
all the best.

Share this answer
Member 8735352 8-Aug-13 3:22am    
Is ConfigurationSettings & ConfigurationManager same??? Bcoz ma app shows "ConfigurationSetting" is obselete...
Bit late I know but,

There is a similar section in the app/web.config specifically for connectionStrings:

<add name="MyConnectionString">
connectionString="Data Source=SQLServer\Instance;Initial Catalog=MyDatabaseName;User ID=sqlServerUser;Password=Password"
providerName="System.Data.SqlClient" />

this goes into the <configuration> section of the config. Sql, authentication is optional - substitute your connection string if required to use windows authentication via Integrated Security=True.


change the code you've written to:

SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionString["MyConnectionString"].ConnectionString;


The benefit of using this section is it is clear to other developers what the purpose of this string is, which is obviously as a connection string.
Share this answer
first u debug your program by using breakpoints.........
then i preferred u to build your database connection through Datasets.............
I think it is easily accessible & simple in programming...........
Best of luck................
Share this answer
Member 4566937 4-Jul-13 3:00am    
private bool SearchTable()

String constr = System.Configuration.ConfigurationManager.AppSettings["connectionString"];
SqlConnection con = new SqlConnection(constr);
string query="Select * from LoginTable Where Name='"+TextBox1.Text+"'|and password='"+TextBox2.Text+"'";
SqlDataAdapter adapt=new SqlDataAdapter (query,con);

DataSet ds = new DataSet();
if (ds.Tables[0].Rows.Count > 0)
return true;
return false;

while executing "The ConnectionString property has not been initialized"error is occurring,find me a solution
string constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();
SqlConnection con = new SqlConnection(constr);
Share this answer

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

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