user2387900
user2387900

Reputation: 215

Unable to call connection string from web config file

I want to establish a connection with sql database. I want to place the connection string in the web config file. So i wrote the code in the web config file as:

<add name="DBCS" connectionString="data source=sahil-pc\\sqlexpress; database=abadakDb; Integrated Security=true " providerName="System.Data.SqlClient"/>

Now, I want to establish the connection in the asp.net, so i wrote a method in my cs file as:

 private DataSet getData(string procName, SqlParameter param)

    {
        string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

        SqlConnection conn = new SqlConnection(cs);

        SqlDataAdapter da = new SqlDataAdapter(procName, conn);

        da.SelectCommand.CommandType = CommandType.StoredProcedure;

        DataSet ds = new DataSet();

        da.Fill(ds);

        return ds;

    }

Here, i am getting the error in the da.Fill(ds) line. It says

System.InvalidOperationException: Instance failure.

I checked the code from the internet. I dont know where I am going wrong. If I explicitly write the connection string in the function then it works fine. So the problem is with connection string in the web config file or the calling thing to web config file. Can you please help me out.

Upvotes: 0

Views: 2364

Answers (1)

dbugger
dbugger

Reputation: 16389

Change your connection string in the web.config to:

"data source=sahil-pc\sqlexpress; database=abadakDb; Integrated Security=true " providerName="System.Data.SqlClient"

in code, the \\ resolves to \, it comes in exactly as is from the web.config

the same as putting a literal @ before the string

Upvotes: 1

Related Questions