6

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();
}
4
  • Is this ASP.NET, Winforms, or what? Commented May 21, 2014 at 14:48
  • It's winforms, I added the tag. Commented May 21, 2014 at 16:29
  • Are you able to Read connection string value from any other place ? Commented May 22, 2014 at 6:37
  • Yes, everything works fine except for reading the values from a UserControl. In a "normal" form, I can read it perfectly. Commented May 22, 2014 at 8:14

2 Answers 2

3

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();
}  
Sign up to request clarification or add additional context in comments.

Comments

0

I think you just need to tweak how you're fetching your ConnectionString.

using (iDB2Connection cn =  new iDB2Connection(ConfigurationManager.ConnectionStrings["iseries"].ConnectionString)
{
    //get the data and return them    
}

1 Comment

Nope, still getting "Object reference not set to an instance of an object." ConfigurationManager settings are null in a usercontrol for some reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.