0
DataSet ds = new DataSet("Transactions");

using (SqlConnection conn = new SqlConnection("myConnectionString"))
{
   SqlCommand sqlComm = new SqlCommand("[dbo].[GetFullTransactionList]", conn);
   sqlComm.CommandType = CommandType.StoredProcedure;

   SqlDataAdapter da = new SqlDataAdapter();
   da.SelectCommand = sqlComm;
   da.Fill(ds);
}

return ds;

Relevant content of app.config:

<configuration>
   <connectionStrings>
      <add name="myConnectionString"
           connectionString="Data Source=LAPTOP-LT;Initial Catalog=myDb;User ID=sa;Password=abc"
           providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

I'm getting this exception:

Format of the initialization string does not conform to specification starting at index 0. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.

Source Error:

Line 44:             DataSet ds = new DataSet("Transactions");
Line 45:             using (SqlConnection conn = new SqlConnection("myConnectionString"))
1
  • is [dbo].[GetFullTransactionList] stored procedure require any argument to be passed? Commented Jul 22, 2013 at 12:42

3 Answers 3

1

The correct way to refer a connection string in the app.config is:

string cnnString = ConfigurationManager.ConnectionStrings["yourCnnString"].ConnectionString;
using(SqlConnection conn = new SqlConnection(cnnString))
{ 

}

do not forget the final property ConnectionString

Sign up to request clarification or add additional context in comments.

4 Comments

its not able to access the connection string from the App.config
What do you mean? Do you have any kind of error message? Do you include using System.Configuration;?
It gives a null reference exception. yes i have included using System.configuration;
The only possibility that I can think to is the string youCnnString. This should be your myConnectionString
0

To use connection string from config file, you need to access it ConfigurationManager.

So it will be:

   using (SqlConnection conn = new 
     SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"]))
   {
          ...
   }

Refer to documentation to get more info

Comments

0

SqlConnection expect a real connection string not the key from app.config. Try to read it with

ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.