44

I've never used the "appSettings" before. How do you configure this in C# to use with a SqlConnection, this is what I use for the "ConnectionStrings"

SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

And this is what I have for the "appSettings"

SqlConnection con = new SqlConnection();
con = ConfigurationManager.AppSettings("ConnectionString");

but it is not working.

wonea
  • 4,783
  • 17
  • 86
  • 139
jorame
  • 2,147
  • 12
  • 41
  • 58

4 Answers4

43

Your web.config file should have this structure:

<configuration>
    <connectionStrings>
        <add name="MyConnectionString" connectionString="..." />
    </connectionStrings>
</configuration>

Then, to create a SQL connection using the connection string named MyConnectionString:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);

If you'd prefer to keep your connection strings in the AppSettings section of your configuration file, it would look like this:

<configuration>
    <appSettings>
        <add key="MyConnectionString" value="..." />
    </appSettings>
</configuration>

And then your SqlConnection constructor would look like this:

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MyConnectionString"]);
Justin Rusbatch
  • 3,992
  • 2
  • 25
  • 43
  • In case of AppSettings in configuration file , what is the value? – Sahil Feb 14 '14 at 05:49
  • @Sahil The value is the connection string itself. IMO, if you're using ASP.NET with a `web.config`, it's better to use the `connectionStrings` element for connection strings, since it was designed specifically for them. The `appSettings` element is primarily for custom app settings that are unique to your app, and have no .NET (or ASP.NET) equivalents, like a list of your clients' email addresses, or the tagline and copyright info for your cat blog. – jpaugh May 31 '17 at 15:29
24

ConfigurationManager.AppSettings is actually a property, so you need to use square brackets.

Overall, here's what you need to do:

SqlConnection con= new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);

The problem is that you tried to set con to a string, which is not correct. You have to either pass it to the constructor or set con.ConnectionString property.

serge
  • 13,940
  • 35
  • 121
  • 205
Anton Vidishchev
  • 1,374
  • 12
  • 17
  • well, i'm trying to use a DSN for my connection. I'm trying to connect to a DB2 DB sitting on an AS400. I know that if I use the connectionstring in the .aspx page it works but everytime i try to use it in code behind i get the Keyword not supported message when trying to ru the app. – jorame Aug 15 '11 at 20:07
  • Then you should use OleDbConnection instead: SqlConnection is for SQL Server direct access only. – Anton Vidishchev Aug 15 '11 at 20:08
  • Check out [this](http://msdn.microsoft.com/en-us/library/ms254500.aspx) article. OdbcConnection or OleDbConnection is going to work: depending on your driver. – Anton Vidishchev Aug 15 '11 at 20:10
  • Should I use the connectionstring or the appsetting with this?? – jorame Aug 15 '11 at 20:14
  • 1
    ConnectionString is the right thing, but it's gonna work with AppSettings as well. What you need to do is a) distinguish whether you use OleDB or ODBC provider and instantiate appropriate DBConnection, b) Form a correct connection string (use the article I mentioned) – Anton Vidishchev Aug 15 '11 at 20:18
  • Try this `SqlConnection con = new SqlConnection("DSN=" + ConfigurationManager.AppSettings["ConnectionString"]);` – kttii Jan 18 '17 at 19:31
0

you should use []

var x = ConfigurationManager.AppSettings["APIKey"];
adt
  • 4,320
  • 5
  • 35
  • 54
0

\if what you have posted is exactly what you are using then your problem is a bit obvious. Now assuming in your web.config you have you connection string defined like this

 <add name="SiteSqlServer" connectionString="Data Source=(local);Initial Catalog=some_db;User ID=sa;Password=uvx8Pytec" providerName="System.Data.SqlClient" />

In your code you should use the value in the name attribute to refer to the connection string you want (you could actually define several connection strings to different databases), so you would have

 con.ConnectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
Obi
  • 3,091
  • 4
  • 34
  • 56
  • 3
    Why was this downvoted? It gives the same solution as Justin's which is the accepted answer...at least they could have left a comment to say why kmt – Obi Mar 21 '13 at 16:54
  • I'm downvoting this because it assumes you are using web.config, not app.config. ConnectionStrings is only available with web.config. – J. Polfer May 17 '16 at 18:18
  • A comment to request clarification of the answer would have sufficed, trigger happy down voting neither educates anyone nor improves the quality of the answer – Obi May 17 '16 at 18:21
  • 1
    Your answer is great Obi, I just used it, but there are way too many snobs here who will downvote anything if they disagree with the smallest idea. They are like football (soccer) liniers, will raise the flag at any possible moment they can, right or wrong. Thanks! – Ricardo Olivo Poletti Nov 21 '16 at 17:15