locked
SqlConnection pulling from web.config connection? RRS feed

  • Question

  • User-939752170 posted

    Hi, I want to use a connection that I defined in the web.config file.

    Web.config:

    <connectionStrings>

    <add name="myConnectionString" connectionString="Data Source=testservername.domain.com;Initial Catalog=test;User ID=accountId;Password=PasswordHere"

    providerName="System.Data.SqlClient" />

    </connectionStrings>

    In the aspx.cs page, I want to use this connection, whats the syntax for that? I tried:

    SqlConnection myConnection = new SqlConnection(myConnectionString); (but it doesnt understand myConnectionString, how do I tell it that myConnectionString is in my web.config?) Thanks!

     

    Tuesday, October 14, 2008 8:18 PM

Answers

  • User6402966 posted

    Try this http://www.odetocode.com/Articles/418.aspx.

    Hope that helps.

    Aaron

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 14, 2008 9:19 PM
  • User1281381861 posted

    Hi,caligali

    TRy this in web config

    Web.config:

    <connectionStrings>

    <add name="myConnectionString" connectionString="Data Source=testservername.domain.com;Initial Catalog=test;User ID=accountId;Password=PasswordHere"

    providerName="System.Data.SqlClient" />

    </connectionStrings>

     

     

    in asp.cs file First add using anmesapce

    ie.

    using System.Web.Configuration ;

     

    and where u want to useconnection string

    write this

    string myConnection =WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

     

    By this u will get the connection string in myConnection  variable.

     

     

     

    PLZ MARK AS ANSWER IF IT HELP U.

    THANKS.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 15, 2008 1:48 AM

All replies

  • User6402966 posted

    Try this http://www.odetocode.com/Articles/418.aspx.

    Hope that helps.

    Aaron

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 14, 2008 9:19 PM
  • User1281381861 posted

    Hi,caligali

    TRy this in web config

    Web.config:

    <connectionStrings>

    <add name="myConnectionString" connectionString="Data Source=testservername.domain.com;Initial Catalog=test;User ID=accountId;Password=PasswordHere"

    providerName="System.Data.SqlClient" />

    </connectionStrings>

     

     

    in asp.cs file First add using anmesapce

    ie.

    using System.Web.Configuration ;

     

    and where u want to useconnection string

    write this

    string myConnection =WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

     

    By this u will get the connection string in myConnection  variable.

     

     

     

    PLZ MARK AS ANSWER IF IT HELP U.

    THANKS.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 15, 2008 1:48 AM
  • User-939752170 posted

     

    it says: Error    1    The name 'WebConfigurationManager' does not exist in the current context   

    Do I have to add a library or something?
    Wednesday, October 15, 2008 2:59 PM
  • User-939752170 posted

     I hadto add: using System.Web.Configuration;

    Thanks!

    Wednesday, October 15, 2008 3:03 PM
  • User-1933021438 posted

    Sorry for the stupid question, but I'm going through some beginner tutorials and don't understand something...


    how come the following code works:

    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);


    But this does not:

    SqlConnection conn1 = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\VocabWords.mdf;Integrated Security=True;User Instance=True");

    from web.config file:

    <configuration>
    	<connectionStrings>
      <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\VocabWords.mdf;Integrated Security=True;User Instance=True"
       providerName="System.Data.SqlClient" />
     </connectionStrings>
     <system.web>
    		<compilation debug="true" targetFramework="4.0"/>
    	</system.web>
    </configuration>


    The second line contains the same string that the first line points to right?

    Monday, January 31, 2011 11:50 PM