Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a project in vs 2005.

I am trying to connect to a database and have stored the connection string in app.config file.


I am using the below code-


private void button1_Click(object sender, EventArgs e)
       {
           //SqlConnection con = new SqlConnection();
           //con.ConnectionString = "Server=./SqlExpress;Database=Northwind;Trusted_Connection=true";
           String con = ConfigurationManager.AppSettings["ConnectionString"];
           SqlConnection cont = new SqlConnection(con);

           try
           {
               cont.Open();
               System.Windows.Forms.MessageBox.Show("Sucess");
           }
           catch (Exception ed)
           {
               MessageBox.Show("Unsucess" + ed.Message);
           }
       }


My app.config file is as below

<configuration>
    <configsections>
    </configsections>
    <connectionstrings>
        <add key="ConnectionString" value="Data Source=A01hw306775\SQLEXPRESS;Initial Catalog=NorthWind;Integrated Security=True" />
    </connectionstrings>
</configuration>


When executing i get the following error

Connection string property not initialised.




Please let me know what has to be done to remove the error
Posted
Updated 5-Jul-11 20:51pm
v3
Comments
TweakBird 6-Jul-11 1:40am    
Did you tried in Google ?
bsb25 6-Jul-11 1:43am    
searched for it in google,but none of the soln are workin..
TweakBird 6-Jul-11 1:45am    
Try Uday P.Singh answer.

what is your code in app.config? Make sure you have written something like this:

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


And always check that you are using the right app.confing file if you have more than one app.config.
 
Share this answer
 
v2
Comments
bsb25 6-Jul-11 1:50am    
its still thwoing the same exception.
Uday P.Singh 6-Jul-11 1:53am    
does your solution has more than one app.config?
bsb25 6-Jul-11 1:55am    
no..i have only one app.config file.
Uday P.Singh 6-Jul-11 2:26am    
write my answer code in your app.confing and use this line to call this:
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];

it will surely resolve your problem.
Change to following code:
String con = ConfigurationManager.AppSettings["ConnectionString"];

to
String con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 
Share this answer
 
 private void button1_Click(object sender, EventArgs e)
        {
            //SqlConnection con = new SqlConnection();
            //con.ConnectionString = "Server=./SqlExpress;Database=Northwind;Trusted_Connection=true";
//changes            
string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//Changes         
   SqlConnection cont = new SqlConnection(con);
          
            try
            {
                cont.Open();
                System.Windows.Forms.MessageBox.Show("Sucess");
            }
            catch (Exception ed)
            {
                MessageBox.Show("Unsucess" + ed.Message);
            }
        }



also change you config file

XML
<configuration>
    <connectionStrings>
        <add name="ConnectionString" connectionString="Data Source=A01hw306775\SQLEXPRESS;Initial Catalog=NorthWind;Integrated Security=True" />
    </connectionStrings>
</configuration>
 
Share this answer
 
v3

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