Click here to Skip to main content
15,940,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem i want to access connection string in my app. i have configured connection string in session like this.
        <roleManager enabled="true" />
        <authentication mode="Forms" />
        <compilation debug="true" targetFramework="4.0" />

        <sessionState mode="SQLServer" sqlConnectionString="Data Source=Sreekanth; Integrated Security=True; Database=Northwind" sqlCommandTimeout="30" cookieless="UseUri" timeout="20"></sessionState>


i want to do update insert delete in my application i want to access this connection string. how to do i am not getting idea please help me thank you.

SessionState configures the session state settings for the current application.

<sessionstate mode="Off|InProc|StateServer|SQLServer">
              timeout="number of minutes"
              sqlConnectionString="sql connection string"
              stateNetworkTimeout="number of seconds"/></sessionstate>

To access Connection String:

Connection string in .NET 2.0 config file
In the appSettings location, add a key named whatever you like to reference your connection string to.
<add key="myConnectionString" value="server=localhost;database=myDb;uid=myUser;password=myPass;" />

To read the connection string from code, use the ConfigurationSettings class.

string connStr = ConfigurationSettings.AppSettings("myConnectionString");

Now you have the connection string loaded from web.config into your string variable in code.

Connection string in .NET 3.5 (and above) config file
Do not use appsettings in web.config. Instead use the connectionStrings section in web.config.
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />

To read the connection string into your code, use the ConfigurationSettings class.

string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
Share this answer
sreekanthv12 22-Jun-12 3:02am    
thank you so much.
Vani Kulkarni 22-Jun-12 3:04am    
vangapally Naveen Kumar 22-Jun-12 3:17am    
Good 5!
Frederic GIRARDIN 26-Feb-18 4:15am    
how to retrieve the name of the Connectionstring property of any DataSet?
Share this answer

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