UserControl中的ConfigurationManager连接字符串为null [英] ConfigurationManager connection string is null in UserControl
问题描述
我有一个通过使用像这样一个数据层中获取数据的表格应用程序:
公开数据表的get(查询字符串,ArrayList的参数= NULL)
{使用
(iDB2Connection CN =新iDB2Connection(ConfigurationManager.ConnectionStrings [i系列]。的ToString()))
{
//获取数据并返回它们
}
}
我有拿到形式数据和这工作得很好。
不过,我创建了一个用户控件它通过这种方法,当我跑我的项目,但其中包含的用户控件的形式,工作正常获取数据。抛出一个设计师异常
为了防止加载设计之前可能的数据丢失,以下错误的
必须解决:
块引用>
我发现错误位于从连接字符串的检索
<的appSettings>
。
它抛出一个NullPointerException异常。
但只有在设计模式。当我不理它,一切工作正常,但是,我想知道如何解决这个
为什么我的
<的appSettings>
空访问他们时,通过我的用户?
更新1
看来我的用户无法识别
<的appSettings方式>
所有
当我把这段代码在我的用户Load事件,我得到一个nullreference为好。
私人无效SelectUser_Load(对象发件人,EventArgs五)
{
txtLocation.Text = ConfigurationManager.AppSettings [所在地]的ToString()。
}
解决方案我找到了解决办法,在的designMode用户控件已经执行中的Load事件的代码。
,因为在app.config不使用的designMode它没有找到,因此无法加载。
所以我做了它周围的一个小检查,检查中的designMode与否:布尔的designMode =(LicenseManager有。 UsageMode == LicenseUsageMode.Designtime);
如果(的designMode)
{
线位置= ConfigurationManager.AppSettings [所在地]的ToString()。
}
I have an application which gets data to the forms through a datalayer which uses something like this:
public DataTable get(String query, ArrayList parameters = null) { using (iDB2Connection cn = new iDB2Connection(ConfigurationManager.ConnectionStrings["iseries"].ToString())) { // get the data and return them } }
I have forms which get data and this works fine.
However, I created a UserControl which gets data through this method which works fine when I run my project, however, the form which contains the UserControl throws a designer exception.
"To prevent possible data loss before loading the designer, the following errors must be resolved: "
I found that the error is located at the retrieval of the connection string from the
<appSettings>
.It throws a nullpointerexception.
But only in design mode. When I ignore it, everything works fine, however, I would like to know how to resolve this.
Why are my
<appSettings>
null when accessing them through my UserControl?UPDATE 1
It seems my UserControl doesn't recognize the
<appSettings>
at all.When I put this code in my UserControl Load event I get a nullreference as well.
private void SelectUser_Load(object sender, EventArgs e) { txtLocation.Text = ConfigurationManager.AppSettings["location"].ToString(); }
解决方案I found the solution, in designmode the usercontrol already executes the code in the Load-event. Because the App.config isn't available in designmode it isn't found and therefore not loaded. So I made a little check around it to check if in designmode or not:
bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime); if (designMode) { string location = ConfigurationManager.AppSettings["location"].ToString(); }
这篇关于UserControl中的ConfigurationManager连接字符串为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!