Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
We are getting problem with using connection string in web.config for a webform in ASP.Net application. The connection string is as follows:

Code in web.config:
<configuration>
     <appSettings>
            <add key="connectionString" value="Server=.\\SQLEXPRESS; database=MLM; Trusted_Connection=True"/>
      </appSettings>
        ....

And Code in WebForm is as below:

SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter adp;
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
          string connecStr = System.Configuration.ConfigurationSettings.AppSettings["connectionString"]; 
            con = new SqlConnection(connecStr); 
            cmd = new SqlCommand("Select * from tblNodeTree");
            cmd.Connection = con;
            ds = new DataSet(); 
            adp = new SqlDataAdapter(cmd);
            adp.Fill(ds, "tblNodeTree");	//  This line shows error message "instance failure" 
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();
        }


The Error Message is : Instance failure
But it works fine on replacing first line of code:

string connecStr = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
With
   string connecStr= "Server=.\\SQLEXPRESS; database=MLM; Trusted_Connection=True";   

What's the problem here with web.config's database connection. And what should be its optimum solution. Kindly advice.
Posted
Comments
walterhevedeich 2-Jun-11 4:56am    
Is this the exact error message?
ambarishtv 2-Jun-11 5:17am    
verify given connection string "connecStr" (via Web.config) using break point

Escape sequence is the issue. Remove one \ there.

Fixing Instance Failure when connecting to SQL Server 2005 Express[^]
 
Share this answer
 
Comments
ambarishtv 2-Jun-11 6:13am    
:thumbsup: .. +5
Uday P.Singh 2-Jun-11 6:16am    
nice solution my 5
You should place the connection string in the connectionStrings section.

Start here[^] and here[^].
 
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