5

I'm using this code to read the connection string from my app.config file but it always return a null value. My App.config file is under my project. Both methods are resulting null values:

public SqlConnection getConnection()
{
    try
    {
        // connectionString = ConfigurationManager.AppSettings["dbConn"];

        connectionString = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
        connectionString = System.Configuration.ConfigurationManager.AppSettings["dbConn"];
        sqlConnection = new SqlConnection(connectionString);  

        sqlConnection = new SqlConnection(connectionString);
    }
    catch (Exception ex)
    {

    }
    return sqlConnection;
}

This is my app.config file declaration:

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
  <connectionStrings>
    <add name="dbConn" providerName="System.Data.SqlClient"
          connectionString="Data Source=VANYA\SQLEXPRESS;Initial Catalog=mydatabase;User  Id=sa;Password=123" />
  </connectionStrings>
</configuration>
2
  • 1
    Does your project happen to be a class library project? Those do not actually use their own app.config file - you'll need to put your connection string into the hosting application (the main program or web app / web site) using your class library Commented Mar 25, 2012 at 11:52
  • marc_s ++ worked for me.. Commented Feb 4, 2016 at 10:21

4 Answers 4

5

I think your problem that you try to read connection string twice, first you do it right, and second time you do it wrong, so just remove second line:

connectionString = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
sqlConnection = new SqlConnection(connectionString); 

ConfigurationManager.AppSettings used to access <appSettings>...</appSettings> section of the config.

Sign up to request clarification or add additional context in comments.

1 Comment

Your are overwriting connectionString by re-assigning it. Remove the 2nd assigment of connectionString and you will be fine.
4

Can you please try

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
  <connectionStrings>
    <add name="dbConn" providerName="System.Data.SqlClient"
          connectionString="Data Source=VANYA\SQLEXPRESS;Initial Catalog=mydatabase;User  Id=sa;Password=123" />
  </connectionStrings>
  <appSettings>
    <add key="dbConn"  value="Data Source=VANYA\SQLEXPRESS;Initial Catalog=mydatabase;User  Id=sa;Password=123" />
  </appSettings>
</configuration>

Then use the second method.

Make sure you select the App.Config file in the solution explorer and in the property window select Copy to Output Directory to Copy Always. Now Build the application and try again.

See the screenshot

Comments

1

You simple define connection string in one class and call that string.....

public class Connection
    {
        /// <summary>
        /// Connection String
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
            }
        }
    }

//Returning connction string
 sqlConnection conn = new SqlConnection(Connection.ConnectionString);  

Comments

1
connectionString=global::myProject.Properties.Settings.Default.myConnectionString

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.