views:

744

answers:

1

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.

+2  A: 

Ok, stupid questions. Are you sure you added the connection string to the app.config on the test machine? And are you sure you spelled it correctly? Are you sure you put the connectionStrings section in the correct place in the app.config? And are you sure you put the app.config in the correct location? Are you sure you spelled "app.config" correctly, and not app.cfg or app.cofig (i've fat fingered that one before)

Mystere Man
No, not a stupid question. I've never used App.config before. I was under the impression that App.config was embedded in the executable, but your answer made me realize that I was mistaken. I moved the config file into the folder with the executable and all is well. Thanks for your help!
Geo Ego
But you shouldn't need to move "app.config" into your program directory - Visual Studio should create a "yourappname.exe.config" already in your output directory anyway - "app.config" is only a system placeholder that will be turned into your app's own config
marc_s
Sadly, app.configs cannot be embedded. I wish they could, that would be useful in some situations. Although you could have your code copy an embedded app.config to a file in the local directory on start if it doesn't exist, if you wanted to simply ship a single exe.
Mystere Man
It did create it. Before I had added app.config, I was simply copying and pasting the executable to a separate machine in order to test it. I followed the same procedure after I added the configuration file, so while VS had put it in the output directory, I hadn't added it to the test machine.
Geo Ego
@Mystere: I don't mind that the app has a separate config file. I'm going to create an installer soon anyway, and this app is strictly for internal use by about a half dozen people. Thanks for the suggestion, though. I'm going to look into copying the config to a local directory as you've suggested.
Geo Ego