Click here to Skip to main content
15,887,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have instance failure error in code while connection string is right. plzz tell me hw can i remove this error..Thnxx.

<connectionstrings>
    <add name="ABC" connectionstring="Data Source=DEEPAK-PC\\SQLEXPRESS;Initial Catalog=Lastimage;User ID=sa;Password=sasa">
      providerName="System.Data.SqlClient" />
    <add name="LastimageConnectionString" connectionstring="Data Source=DEEPAK-PC\\SQLEXPRESS;Initial Catalog=Lastimage;User ID=sa;Password=sasa">
      providerName="System.Data.SqlClient" />
  </add></add></connectionstrings>


and code on .aspx.cs page is

C#
static string str = ConfigurationManager.ConnectionStrings["ABC"].ConnectionString;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            filldropdown();
        }
    }
    //SqlConnection con = new SqlConnection(str);
    public void filldropdown()
    {
        //SqlConnection con = new SqlConnection(@"Data Source=DEEPAK-PC\\SQLEXPRESS;Initial Catalog=Lastimage;User ID=sa;Password=sasa");
        SqlConnection con = new SqlConnection(str);
****        con.Open();
        SqlCommand cmd = new SqlCommand("Select EmpID from Tbl_Emp", con);
        //if (con.State == ConnectionState.Closed)
        //{
        //con.Open();
        //}
        SqlDataReader dr = cmd.ExecuteReader();
        TextBox3.Items.Clear();
        if (dr.HasRows)
        {
            //TextBox3.DataSource = dr["EmpID"].ToString();
            //TextBox3.DataBind();

            while (dr.Read())
            {
                TextBox3.Items.Add(dr["EmpID"].ToString());
            }
        }
        con.Close();
    }



Its showing an error in stared line..plzz tell hw can i remove it.
Posted
Updated 10-Jan-14 1:39am
v2

omit " \\ " from connection string in Data Source and use " \ " .
 
Share this answer
 
Comments
[no name] 6-Aug-15 18:08pm    
You mean replace something that does not exist in the connection string with something that is the same thing? Why? Why answer a question that has already been answered?
try this... <add name="myConSql" connectionstring="server=localhost;database=Lastimage;uid=sa;password=sasa" providername="System.Data.SqlClient"></add>
 
Share this answer
 
use this

XML
<add name="ABC" connectionstring="Data Source=DEEPAK-PC\\SQLEXPRESS;Initial Catalog=Lastimage;User ID=sa;Password=sasa" 
      providerName="System.Data.SqlClient" />
    <add name="LastimageConnectionString" connectionstring="Data Source=DEEPAK-PC\\SQLEXPRESS;Initial Catalog=Lastimage;User ID=sa;Password=sasa" 
      providerName="System.Data.SqlClient" />
 
Share this answer
 
v3
Comments
D.K.Pareek 10-Jan-14 7:55am    
@deviprasad same as above. i think provider name cannt be out of >
Deviprasad Das 10-Jan-14 8:11am    
Can you please paste the exception details?
D.K.Pareek 10-Jan-14 23:06pm    
connectionstring attribute is not allowed and when i update connectionstring as C and S then It agian gives error in con.open();

InvalidOperationException was unhandeled by user code.
(Instance failure.)
[no name] 11-Jan-14 0:04am    
try this... <add name="myConSql" connectionstring="server=localhost;database=Lastimage;uid=sa;password=sasa" providername="System.Data.SqlClient">
Change your connection string section in web.config file like:

<connectionstrings>
    <add name="ABC" connectionstring="Data Source=DEEPAK-PC\\SQLEXPRESS;Initial Catalog=Lastimage;User ID=sa;Password=sasa" providername="System.Data.SqlClient"></add> 
</connectionstrings>
 
Share this answer
 
v3
Comments
D.K.Pareek 10-Jan-14 7:49am    
@sandeep Its showing Error in provider name and also in connectionstring as (element is not allowed)
Sandeep Singh Shekhawat 10-Jan-14 7:58am    
Can you paste your error here.
D.K.Pareek 10-Jan-14 23:07pm    
connectionstring attribute is not allowed and when i update connectionstring as C and S then It agian gives error in con.open();

InvalidOperationException was unhandeled by user code.
(Instance failure.)
Sandeep Singh Shekhawat 10-Jan-14 23:16pm    
There are some common error in Web.Config file. Please use updated code which metion in above answer
<connectionstrings>
<add name="ABC" connectionstring="Data Source=DEEPAK-PC\\SQLEXPRESS;Initial Catalog=Lastimage;User ID=sa;Password=sasa" providername="System.Data.SqlClient">


"S" in connection string would be in capital letter means "S" instead of small letter "s"
D.K.Pareek 10-Jan-14 23:29pm    
I pasted ur above updated connectionstring bt gives error as Error

Unrecognized configuration section connectionstrings.

wen i change s in <connectionstring> as S which came in dropdownlist self then again gives error in connectionstring
wen i update as S then error in stared part..
<connectionstrings>
<add name="ABC" **connectionstring**="Data Source=DEEPAK-PC\\SQLEXPRESS;Initial Catalog=Lastimage;User ID=sa;Password=sasa" providername="System.Data.SqlClient">

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