0

Reference: Accessing database connection string using app.config in C# winform I am trying to connect my C# file.cs with App.config but I am getting an error

Unhanded exception System.NullReferenceException: Object reference not set to an instance of an object

Here is what I am doing.

App.config

<appSettings>
    <add key="SQLConn" value="Data Source=dbw.xyz.abc.com(just an example);
       Initial catalog=database_name;Integrated Security=True;
       User ID=username;Password=password"/> 
</appSettings>

Code:

SqlConnection SQLConn = new SqlConnection();
SQLConn.ConnectionString = 
       ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;
try
{
     SQLConn.Open();
}
catch (Exception e)
{
    Console.WriteLine("Program aborted: " + e);
}
finally
{
    if (SQLConn.State == ConnectionState.Open)
        SQLConn.Close();
}
Community
  • 1
  • 1
  • What is 'conn' in your finally block? If this code compiles then it is probably the source of your NULL reference exception. I don't see where you set it. Did you mean SQLConn? – n8wrl Mar 28 '14 at 20:36
  • Thank you for noticing that. Yes It is SQLConn. I changed it but still I am getting this error. –  Mar 28 '14 at 20:39
  • 1
    You should wrap your code with a `using` statement, à la: `using (var con = new SqlConnection) { ... }`; see [SqlConnection Class](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx) – László Koller Mar 28 '14 at 20:39
  • I tried that with var statement before which was `var SQLConn = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;` But Error It didn't worked out –  Mar 28 '14 at 21:03

2 Answers2

1

You do realize there is a difference between a connection string and an app setting? Two different API's to read them. Your code reads a connection string, the app config defines an app setting.

The error is in this line (and totally unrelated to sql server, seriously):

configurationManager.ConnectionStrings["SQLConn"].ConnectionString;`

The first part

configurationManager.ConnectionStrings["SQLConn"]

will not return a connection string because you do not define one. You define an AppSetting.

TomTom
  • 61,059
  • 10
  • 88
  • 148
0

Your configuration file is adding the connection string on the appSettings. There is a right place to put connectionStrings, also specify the providerName, for sample:

<connectionStrings>
   <clear/>
   <add name="SQLConn" 
        connectionString="Data Source=dbw.xyz.abc.com(just an example); Initial Catalog=database_name;Integrated Security=True; User ID=username;Password=password" 
        providerName="System.Data.SqlClient" /> 
</connectionStrings>

And you can read it using the ConfigurationManager, for sample:

string connectionString = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;

On the other hand, your code is speficying a value on the AppSettings, it's fine, but not for the connectionStrings. To read some key from AppSettings, use it:

string sqlConn = ConfigurationManager.AppSettings["SQLConn"];
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • It did that but it goes to my `catch (Exception e) { Console.WriteLine("Program aborted: " + e); }` –  Mar 28 '14 at 20:49
  • I also did added `string sqlConn = configurationManager.AppSettings["SQLConn"];` –  Mar 28 '14 at 20:59
  • You need to choice, `connectionStrings` tag or `appSettings`. It does not make sense to have both. `ConfigurationManager` allow you to access both of them, and keep the right with your .config file. – Felipe Oriani Mar 28 '14 at 21:09
  • I know that I tried doing it with both the options but it does not go into my database table –  Mar 28 '14 at 21:24