Winforms Connection Strings from App.config

I have a Winforms application that I am developing in C # that will serve as an interface for a SQL Server 2005 database. I have collapsed the executable onto a test machine and run it. It worked perfectly on the test machine until the last round of changes I made. However, the following exception now appears on the test machine immediately after 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 related to mainMenu_Load is the way the database connection string is called. Previously, I installed a string with a connection string for each form from which I needed to call it, for example:

string conString = "Data Source = SHAREPOINT;Trusted_Connection = yes;" +
                   "database = CustomerDatabase;connection timeout = 15";

As my application grew and I added forms to it, I decided to add an App.config to the project. I defined a 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>

, 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;
}

conString :

PublicMethods.GetConnectionString("conString").ToString()

, . , App.config GetConnectionString. , SQLConnection:

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

. , conString , . , , App.config , .

+3
1

, . , app.config ? , ? , connectionStrings app.config? , app.config ? , "app.config", app.cfg app.cofig( ).

+4

Source: https://habr.com/ru/post/1745001/


All Articles