none
Want to grab the Connection String from app.config file. RRS feed

  • Question

  • I am making one Windows based application where I want to make connection in App.config file but I am unable to grab this from .cs file.

    Please help me in this which will be highly appreciated. 

    SqlConnection connection = new SqlConnection();

    connection.ConnectionString = ConfigurationSettings.AppSettings["Amit"];

    connection.Open();

    Whenever I try to call the connection from App.config file, it does not initialize in "connection".

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <connectionStrings>

      <!--<appSettings>-->

        <add name ="Amit" connectionString=" Data Source= Amit\\SQLEXPRESS; Initial Catalog=Directory; User Id=Amit; Password=password"

             />

      <!--</appSettings>-->

      </connectionStrings>

    </configuration>


    Amit
    Saturday, October 16, 2010 7:24 AM

Answers

  • Code complains because, as it states, System.Configuration.ConfigurationSettings.AppSettings is obsolete. You need to use different class called ConfigurationManager to access information from the configuration file. To Access connection string, your code would look like

    connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Amit"].ConnectionString

     Make sure that your application has reference to System.Configuration.dll assembly

     


    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by Amit Baranwal Tuesday, October 26, 2010 1:10 PM
    Monday, October 25, 2010 10:39 AM
    Moderator

All replies

  • 1.  The \\ should be changed to \.  (C# escape sequences are not used in XML files.)

    2.  The single space character between Source= and Amit should be removed.

    3. > connection.ConnectionString = ConfigurationSettings.AppSettings["Amit"];

    Use: connection.ConnectionString = ConfigurationSettings.ConnectionStrings["Amit"].ConnectionString instead.  It is not an AppSetting.

    Please post full error message and stack trace if you continue to have problems.

    • Proposed as answer by liurong luo Tuesday, October 19, 2010 7:52 AM
    • Marked as answer by liurong luo Friday, October 22, 2010 8:52 AM
    • Unmarked as answer by VMazurModerator Monday, October 25, 2010 10:43 AM
    Saturday, October 16, 2010 2:20 PM
  • Hi

    Thanks for reply. Now it's working but I have one more doubt when ever I am trying to access the connection from App.config file it's proper working but some times its giving compiler warning. I am also sending the error message which is:

    Warning 1 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings' C:\Users\Amit\Documents\Visual Studio 2005\Projects\Directory\Directory\Cust_Directory.cs 115 40 Directory

    2nd you suggest me to use connection.ConnectionString = ConfigurationSettings.ConnectionStrings["Amit"].ConnectionString  instead of connection.ConnectionString = ConfigurationSettings.AppSettings["Amit"]; but I am getting error while building the project.                        Error Message is:

    Error 1 'System.Configuration.ConfigurationSettings' does not contain a definition for 'ConnectionStrings' C:\Users\Amit\Documents\Visual Studio 2005\Projects\Directory\Directory\Cust_Directory.cs 116 62 Directory
     

    Please help me in this regard.

    Thanks in advance.


    Amit
    Saturday, October 23, 2010 10:50 AM
  • Code complains because, as it states, System.Configuration.ConfigurationSettings.AppSettings is obsolete. You need to use different class called ConfigurationManager to access information from the configuration file. To Access connection string, your code would look like

    connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Amit"].ConnectionString

     Make sure that your application has reference to System.Configuration.dll assembly

     


    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by Amit Baranwal Tuesday, October 26, 2010 1:10 PM
    Monday, October 25, 2010 10:39 AM
    Moderator
  • Hi Thanks a lot for hepls. Now it's working fine.
    Amit
    Tuesday, October 26, 2010 1:10 PM