connection string in Web.Config

Discussion in 'ASP .Net' started by John A Grandy, Feb 16, 2006.

  1. 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 A Grandy, Feb 16, 2006
    #1
    1. Advertising

  2. 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" wrote:

    > 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>
    >
    >
    >
    =?Utf-8?B?YnJpYW5zW01DU0Rd?=, Feb 16, 2006
    #2
    1. Advertising

  3. 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
    blackstaronline.net, Feb 16, 2006
    #3
  4. 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[MCSD]" wrote:

    > 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" wrote:
    >
    > > 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>
    > >
    > >
    > >
    =?Utf-8?B?YnJpYW5zW01DU0Rd?=, Feb 16, 2006
    #4
  5. Yes. I am doing 2.0 , so this is what I want. Thanks.

    "brians[MCSD]" <> wrote in message
    news:...
    > 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" wrote:
    >
    >> 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 A Grandy, Feb 16, 2006
    #5
  6. John A Grandy

    Guest

    John A Grandy wrote:
    > Yes. I am doing 2.0 , so this is what I want. Thanks.
    >
    > "brians[MCSD]" <> wrote in message
    > news:...
    > > 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" wrote:
    > >
    > >> 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>
    > >>
    > >>
    > >>
    , Feb 26, 2006
    #6
  7. John A Grandy

    Guest

    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?
    , Feb 26, 2006
    #7
  8. I have found it easier to give up altogether on appSettings,
    and use connectionStrings instead, although you can use either.

    For VB and C# examples, see :
    http://www.asp.net/QuickStart/aspnet/doc/management/retrieve.aspx




    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en espaƱol : http://asp.net.do/foros/
    ===================================
    <> wrote in message news:...
    >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?
    >
    Juan T. Llibre, Feb 26, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michelle A.
    Replies:
    0
    Views:
    551
    Michelle A.
    Aug 20, 2003
  2. Replies:
    4
    Views:
    13,578
  3. Replies:
    0
    Views:
    839
  4. CSharpner
    Replies:
    0
    Views:
    1,018
    CSharpner
    Apr 9, 2007
  5. Replies:
    3
    Views:
    846
    Samuel R. Neff
    Aug 3, 2007
Loading...

Share This Page