connection string in Web.Config

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

If a SQLDataSource add the following to my web.config file, how to I
programmatically extract the connection string:

<configuration>

<appSettings/>

<connectionStrings>

<add name="GUIDBConnectionString" connectionString="Data
Source=(local);Initial Catalog=GUI;Integrated Security=True"

providerName="System.Data.SqlClient" />

</connectionStrings>
 
Hello John,

To get that string try:

// Get the connectionString.
ConnectionStringSettings connectionStringSettings =
ConfigurationManager.ConnectionStrings["GUIDBConnectionString"];

Then to use it:
// Initialize the connection with the string
SqlConnection sqlConnection - new
SqlConnection(connectionStringSettings.ConnectionString);
 
Here is what works for me;

--->In your WEB.CONFIG file
<appSettings>
<add key="DBConn" value="Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\inetpub\wwwroot\data\test.mdb" />
</appSettings>

-->In your page
Dim objConn As OleDBConnection = New
OleDBConnection(ConfigurationSettings.AppSettings("DBConn"))

Hope this helps,
Jeremy Reid
http://blackstaronline.net/hgtit
 
Sorry, I am very much in the mindset of ASP.NET 2.0. they have added some
nice additional configuration management features. For 1.1 try:

web.config:
<appSettings>
<add key="ConnectionString" value="server=...;database=...;user id=...;
password=...;" />
</appSettings>

In your connection code:
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

--
brians
http://www.limbertech.com

brians said:
Hello John,

To get that string try:

// Get the connectionString.
ConnectionStringSettings connectionStringSettings =
ConfigurationManager.ConnectionStrings["GUIDBConnectionString"];

Then to use it:
// Initialize the connection with the string
SqlConnection sqlConnection - new
SqlConnection(connectionStringSettings.ConnectionString);

--
I think that should do it - brians
http://www.limbertech.com


John A Grandy said:
If a SQLDataSource add the following to my web.config file, how to I
programmatically extract the connection string:

<configuration>

<appSettings/>

<connectionStrings>

<add name="GUIDBConnectionString" connectionString="Data
Source=(local);Initial Catalog=GUI;Integrated Security=True"

providerName="System.Data.SqlClient" />

</connectionStrings>
 
Yes. I am doing 2.0 , so this is what I want. Thanks.

brians said:
Hello John,

To get that string try:

// Get the connectionString.
ConnectionStringSettings connectionStringSettings =
ConfigurationManager.ConnectionStrings["GUIDBConnectionString"];

Then to use it:
// Initialize the connection with the string
SqlConnection sqlConnection - new
SqlConnection(connectionStringSettings.ConnectionString);

--
I think that should do it - brians
http://www.limbertech.com


John A Grandy said:
If a SQLDataSource add the following to my web.config file, how to I
programmatically extract the connection string:

<configuration>

<appSettings/>

<connectionStrings>

<add name="GUIDBConnectionString" connectionString="Data
Source=(local);Initial Catalog=GUI;Integrated Security=True"

providerName="System.Data.SqlClient" />

</connectionStrings>
 
John said:
Yes. I am doing 2.0 , so this is what I want. Thanks.

brians said:
Hello John,

To get that string try:

// Get the connectionString.
ConnectionStringSettings connectionStringSettings =
ConfigurationManager.ConnectionStrings["GUIDBConnectionString"];

Then to use it:
// Initialize the connection with the string
SqlConnection sqlConnection - new
SqlConnection(connectionStringSettings.ConnectionString);

--
I think that should do it - brians
http://www.limbertech.com


John A Grandy said:
If a SQLDataSource add the following to my web.config file, how to I
programmatically extract the connection string:

<configuration>

<appSettings/>

<connectionStrings>

<add name="GUIDBConnectionString" connectionString="Data
Source=(local);Initial Catalog=GUI;Integrated Security=True"

providerName="System.Data.SqlClient" />

</connectionStrings>
 
I tried to use:
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"])
in my connection code, but I got the error message below.

'Public Shared ReadOnly Property AppSettings() As
System.Collections.Specialized.NameValueCollection' is obsolete: 'This
method is obsolete, it has been replaced by
System.Configuration!System.Configuration.ConfigurationManager.AppSettings'

Is there something I should be doing differently in asp.net 2.0?
 
Back
Top