aruni
aruni

Reputation: 2752

How to give connection string as a key value?

I want to give connection string as a key. So I write in web.config this code.

    <add key="connectstring" value="Data Source=USER-PC;Initial Catalog=DBName; Integrated Security=False; providerName=System.Data.SqlClient"/>
  </appSettings>

but there is error and it says

Keyword not supported: 'providername'.
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: Keyword not supported: 'providername'.

Source Error:

Line 28:             SqlConnection conn;
Line 29:             conn = new SqlConnection(ConfigurationManager.AppSettings["connectstring"].ToString());
Line 30:                 
Line 31: 

How can I solve it??

Upvotes: 0

Views: 7720

Answers (2)

Patrick Guimalan
Patrick Guimalan

Reputation: 1010

<appSettings>
<add key="connectstring" value="Data Source=USER-PC;Initial Catalog=DBName; Integrated Security=False;" providerName="System.Data.SqlClient"/>

separate providerName in your value

NOTE: There is an error in your value, add a double qoutation after False;

Upvotes: 1

IrishChieftain
IrishChieftain

Reputation: 15252

Try using the ConfigurationManager object:

string connectionString 
    = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;

<connectionStrings>
    <remove name="LocalSqlServer" />
    <add name="LocalSqlServer" 
        connectionString="Initial Catalog=MyDB;Data Source=MyPC;Integrated Security=SSPI;"
            providerName="System.Data.SqlClient" />
<connectionStrings>

To use appSettings:

System.Configuration.ConfigurationManager.Appsettings.Get("connectstring");

Upvotes: 1

Related Questions