How can I put my connectionString from the Web.Config file into my SqlConnection();

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bencoding
    New Member
    • Mar 2008
    • 21

    How can I put my connectionString from the Web.Config file into my SqlConnection();

    Wow, I've spent about 3 hrs now trying to figure this one out, none of the articles online were able to help me and some were using system libraries that were obsolete......a nyways,

    I am able to read my connection string from my web.config file withing a Response.Write( ); but when I put it in my SqlConnection([here]) it won't display, I don't get why

    Can someone PLEASE I'm begging for a simple solution for this, please help!


    This works fine:
    Code:
            string connectionInfo = ConfigurationManager.AppSettings["connectionString"];
            Response.Write(connectionInfo);
    But when I put it in my SqlConnection it won't load :(

    Code:
            SqlConnection myConn = new SqlConnection(connectionInfo );
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    What do you mean, won't display? It's not supposed to display anything.

    First of all, you're either putting it in the wrong place, or loading it wrong.
    You should put it in the "ConnectionStri ngs" section:
    [code=xml]
    <configuratio n>
    <connectionStri ngs>
    <add name="connStrin gName" connectionStrin g="Data Source=datasour ce;Initial Catalog=db;Pers ist Security Info=True;User ID=sa;Password= yeahRight" providerName="S ystem.Data.SqlC lient" />
    </connectionStrin gs>
    .....
    .....
    .....
    </configuration>
    [/code]

    And retrieve the value like this:
    Code:
    string connStr = ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString;
    SqlConnection conn = new SqlConnection(connStr);

    Comment

    • bencoding
      New Member
      • Mar 2008
      • 21

      #3
      yes I understand, I did put it there, I have tried this and I get "InvalidOperati onException was unhandled by user code" and it points to:

      Code:
      conn.Open();
      Using this code:

      Code:
         <connectionStrings>
          <add name="DatabaseConnectionString" connectionString="Data Source=[myDB];Integrated Security=True"
           providerName="System.Data.SqlClient" />
        </connectionStrings>


      Code:
          string connStr = ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString;
          SqlConnection myConn = new SqlConnection(connStr);

      ...this was what I was getting before, still not working.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Ok, do this:

        Code:
        try
        {
             conn.Open();
        }
        catch (Exception ex)
        {
             Response.Write("Exception! :: " + ex.Message);
              Response.End();
        }
        And let us know what the message is.


        Originally posted by bencoding
        yes I understand, I did put it there, I have tried this and I get "InvalidOperati onException was unhandled by user code" and it points to:

        Code:
        conn.Open();
        Using this code:

        Code:
           <connectionStrings>
            <add name="DatabaseConnectionString" connectionString="Data Source=[myDB];Integrated Security=True"
             providerName="System.Data.SqlClient" />
          </connectionStrings>


        Code:
            string connStr = ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString;
            SqlConnection myConn = new SqlConnection(connStr);

        ...this was what I was getting before, still not working.

        Comment

        Working...