Click here to Skip to main content
15,889,867 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:

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


code behind code:

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

conn.Open();


what i am doing wrong?
Posted
Updated 26-Sep-19 3:23am
Comments
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.

sanjeev
 
Share this answer
 
Comments
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:

<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.

then:

change the code you've written to:

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

conn.Open();

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
 
hai,
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................
bye.........
Thnks......
 
Share this answer
 
Comments
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();
adapt.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
return true;
}
else
{
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);
con.Open();
 
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