UserControl中的ConfigurationManager连接字符串为null

编程入门 行业动态 更新时间:2024-10-28 02:23:24
本文介绍了UserControl中的ConfigurationManager连接字符串为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个通过使用像这样一个数据层中获取数据的表格应用程序:

公开数据表的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

本文发布于:2023-11-05 19:50:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561685.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   UserControl   ConfigurationManager   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!