0

The Exception is on the code The code is

string cs=ConfigurationManager.ConnectionStrings["Sam"].ConnectionString;
using(SqlConnection con = new SqlConnection(cs))
{
    SqlCommand cmd = new SqlCommand("Select * from tblProduct", con);
    con.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    GridView1.DataSource = rdr;
    GridView1.DataBind();
}

So I have a one web-Config file.so I am connect with it. The code inside a web config is

<connectionStrings>
    <add name ="Sam"
    connectionString=" data source=.\\SQLExpress; database=sample; Integrated Security=true " />
</connectionStrings>

After Compiling it there is no error but when I debug it, at the run time I got

An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code. Additional information: Instance failure.

Why I am getting such error

Christos
  • 53,228
  • 8
  • 76
  • 108
wasim ali
  • 39
  • 1
  • 10

1 Answers1

2

You're getting an error when you try to open a connection to the database. you need to modify your connection string. Try to replace the double-slash by only one. Also try to avoid the space inside your connection string.

<connectionStrings>
    <add name ="Sam"
    connectionString="data source=.\SQLExpress;database=sample;Integrated Security=true"/>
</connectionStrings>
MRebai
  • 5,344
  • 3
  • 33
  • 52