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.
XML
<configuration>
    <system.web>
        <roleManager enabled="true" />
        <authentication mode="Forms" />
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.web>

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

</configuration>


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.
Posted

SessionState configures the session state settings for the current application.

HTML
<sessionstate mode="Off|InProc|StateServer|SQLServer">
              cookieless="true|false"
              timeout="number of minutes"
              stateConnectionString="tcpip=server:port"
              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.
XML
<appSettings>
<add key="myConnectionString" value="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</appSettings>


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

C#
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.
XML
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>

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

C#
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
 
Share this answer
 
Comments
sreekanthv12 22-Jun-12 3:02am    
thank you so much.
Vani Kulkarni 22-Jun-12 3:04am    
Welcome!
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