在Windows窗体用户控件中使用连接字符串时发生错误

时间:2018-08-27 11:43:23

标签: c# nullreferenceexception windows-forms-designer

需要连接到数据库并在用户控制器的Load事件中获取一些值。但是,每次将用户控件拖放到Windows窗体时,都会收到空引用错误。似乎Visual Studio无法在设计时初始化连接字符串(根据堆栈跟踪)。连接字符串已在app.config文件中定义。到目前为止,我搜索了此错误,并尝试添加以下代码块,但无法正常工作。

    if(System.ComponentModel.LicenseManager.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
{
     _strcon = ConfigurationManager.ConnectionStrings["defaultConnection"].ConnectionString;
}

连接字符串

<add name="defaultConnection"
providerName="System.Data.SqlClient" connectionString="Dbhostname;Initial Catalog=employee;User ID=sa;Password=password;Enlist=False;MultipleActiveResultSets=true;" />

没有任何编译时错误或运行时错误。错误是Windows窗体设计器未加载

enter image description here

0 个答案:

没有答案