connection string in web.config

Last post 01-11-2008 5:53 AM by deep_charate. 5 replies.

Sort Posts:

  • connection string in web.config

    01-10-2008, 10:34 PM
    • Member
      45 point Member
    • aspusername
    • Member since 11-06-2007, 7:59 PM
    • Posts 247

    Hello,

    I have a connection string in my web.config file <add name = ...> you know when you configure your gridview I clicked yes when it prompted me whether to save it in the web.config file or not.

    I would like to insert a row into the same table.  Do I have to create the connection string again?  SqlConnection cn = .... Or is it self implied?  Or how do I access the connection string that was added in the web.config file?

     Thank you.

  • Re: connection string in web.config

    01-10-2008, 10:58 PM
    Answer
    • Participant
      1,368 point Participant
    • bcanonica
    • Member since 12-08-2006, 9:11 PM
    • Fort Washington, PA
    • Posts 346

    I am guessing you did it in the browser so to use it again it will prompt you when populaing another control whether to use the same connection or not.  Otherwise you can access it by creating a connection object and using this path ConfigurationSettings.appSettings("ConnectionString").ToString()

    Remember to mark any post that is helpful as an answer and change posts status to Resolved to help others in the future.

    cheers,

    BC

    Top IT Providers
  • Re: connection string in web.config

    01-10-2008, 11:06 PM
    Answer
    • Contributor
      2,214 point Contributor
    • mosessaur
    • Member since 07-11-2002, 6:40 PM
    • Cairo
    • Posts 353

     to get a connection string from your web.config use

    string connStr = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;

    And you do not need to recreate your connection string again on your web.config.

    But of course if you are creating a SqlConnection object, you just need to get your connection string to pass it to your SqlConnection instance because this is not done automatically.

     

     

     

     

    Muhammad M. Mosa Soliman
    Software Engineer
  • Re: connection string in web.config

    01-11-2008, 3:18 AM
    Answer

    no need . u can access the connectionstring from web.config

    System.Configuration.ConfigurationManager.AppSettings("ConnectionString")

    Thank u

    Baba

    Please remember to click "Mark as Answer" on this post if it helped you. 

    Thank u

    Baba

    Please remember to click "Mark as Answer" on this post if it helped you.
  • Re: connection string in web.config

    01-11-2008, 3:46 AM
    Answer
    • All-Star
      91,768 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    Using Connection string in webconfig

    string conn = (string)ConfigurationSettings.ConnectionStrings["ConnectionString"];

    or if you are using appSettings in your Webconfig then you can have something like this

    System.Configuration.ConfigurationManager.AppSettings["Connection String Key Name"]; 

    for more informations you can google the word "web config connection string" to obtain more results

    Hope my suggestion helps. 

    "Code,Beer and Music ~ my way of being a programmer"

  • Re: connection string in web.config

    01-11-2008, 5:53 AM
    Answer
    • Member
      88 point Member
    • deep_charate
    • Member since 01-08-2008, 5:13 AM
    • Pune
    • Posts 19

    Include the namespace into class file 

     using System.Configuration;

    add the following code to retrieve the namespace


                    string constring = ConfigurationManager.AppSettings["ConnectionString"];

                    con = new SqlConnection(constring) ;

    Filed under:
Page 1 of 1 (6 items)