Winforms connection strings from App.config
- by Geo Ego
I have a Winforms app that I am developing in C# that will serve as a frontend for a SQL Server 2005 database. I rolled the executable out to a test machine and ran it. It worked perfectly fine on the test machine up until the last round of changes that I made. However, now on the test machine, it throws the following exception immediately upon opening:
System.NullReferenceException: Object reference not set to an instance of an object.
at PSRD_Specs_Database_Administrat.mainMenu.mainMenu_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
The only thing that I changed in this version that pertains to mainMenu_Load is the way that the connection string to the database is called. Previously, I had set a string with the connection string on every form that I needed to call it from, like:
string conString = "Data Source = SHAREPOINT;Trusted_Connection = yes;" +
"database = CustomerDatabase;connection timeout = 15";
As my app grew and I added forms to it, I decided to add an App.config to the project. I defined the connection string in it:
<connectionStrings>
<add name="conString"
providerName="System.Data.SqlClient"
connectionString="Data Source = SHAREPOINT;Trusted_Connection = yes;database = CustomerDatabase;connection timeout = 15" />
</connectionStrings>
I then created a static string that would return the conString:
public static string GetConnectionString(string conName)
{
string strReturn = string.Empty;
if (!(string.IsNullOrEmpty(conName)))
{
strReturn = ConfigurationManager.ConnectionStrings[conName].ConnectionString;
}
else
{
strReturn = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
}
return strReturn;
}
I removed the conString variable and now call the connection string like so:
PublicMethods.GetConnectionString("conString").ToString()
It appears that this is giving me the error. I changed these instances to directly call the connection string from App.config without using GetConnectionString. For instance, in a SQLConnection:
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
This also threw the exception. However, when I went back to using the conString variable on each form, I had no issues. What I don't understand is why all three methods work fine on my development machine, while using the App.config directly or via the static string I created throw exceptions.