Solved

Error loading app.config file settings in a UserControl_Load event during DESIGN TIME!

Posted on 2004-08-30
5
215 Views
Last Modified: 2010-04-23
This is a really weird error, maybe I'm doing something wrong or there is an error in VS. I created a user control that contains many controls, including several dataadapters and a dataset, and many databound controls. In order to connect to the correct database I call a function GetConnectionString() which gets the DB location from the app.config file using System.Configuration.ConfigurationSettings.AppSettings.Item("LocalDB").
GetConnectionString() also checks to make sure it is a valid DB, if it’s not it displays a msgbox, saying the Settings must be changed, then displays the settings form.
This worked well for weeks. Then without changing anything, when I would load a Form in Visual Studio that contained the UserControl I would see my MsgBox appear saying that file location “” was invalid, then the settings form would appear, during DESIGN time!! And the form I was trying to load into VS would just appear as a huge error message, "System.Data.DataException: DataTable must be set prior to using dataview", followed by a stack trace. I can’t even see any of the controls loaded on the form. The program still builds fine and funtions fine during RUN time.

So, for some reason, when I load a form containing my UserControl into VS, it tries to excecute the Load event of the UserControl, which calls GetConenctionString, which tries to get a value from the App.Config file, but instead gets an empty string (only during DESIGN TIME), then, it continues and tries to fill the dataset, create dataviews, and so on. Of course errors are generated and my form explodes!

I was able to fix the problem by removing all error handling from GetConnectionString() and just having the funtion return the database location retrieved from the app.config file, then in my UC I hard-coded my development Database location and use that unless GetConnectionString() return a non empty string.

However I would like to find a way to fix the error rather than this work around. I’m just wondering why VS is executing the Load event of my usercontrol when a Form containing it is loaded (In design time)?? Also, why does System.Configuration.ConfigurationSettings.AppSettings.Item("AnyKey") return an empty string, during design time? I guess I’m not sure where the settings are stored, are they loaded from the app.config xml file into the excexuting assembly or somthing?

I’m kinda of new to all this, so I hope this makes sense. Any help would be greatly appreciated.

I tried to recreate the error without much luck. It's easy to make the msgBoxes and settings form appear by creating a simple usercontrol, and in the UC_load event call GetConnectionString.

    Private Sub ucTesting_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.OleDbConnection1.ConnectionString = GetConnectionString()
    End Sub

Where a simplified version of GetConnectionString

 Function GetConnectionString() As String
        Dim DBLocation As String = System.Configuration.ConfigurationSettings.AppSettings.Item("LocalDB")
        If DBLocation = "" Then
            MsgBox("The database loactions specified in 'System Settings' are incorrect. Please specify the correct loaction for the Database; In System Settings", MsgBoxStyle.Critical, "Cannot Find Database")

'Show settings form
        End If
End Function

This will display the msgBox when a form that contains the usercontrol is viewed during run time. However the form doesn't explod and show a error message, I think that would require a more complicated from, or usercontrol or databinding or somthing.

Thanks again, if anybody has any insight please let me know.

0
Comment
Question by:minermadison
  • 3
  • 2
5 Comments
 
LVL 1

Author Comment

by:minermadison
ID: 11935756
Just another observation. VS always uses rediculous amounts of memory. However, before I started recieving this error, it was about half of what it is now? I'm thinking it's related somhow... Thanks again.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11935848
You might want to check in the UserControl, whether you are in design time or run-time.

If Not Me.Design Then
...
EndIf

Bob
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 350 total points
ID: 11935850
Too fast:

If Not Me.DesignMode Then
   ...
EndIf

Bob
0
 
LVL 1

Author Comment

by:minermadison
ID: 11935952
Bob, that works... Any clue why this is even neccesaary? Why is the Load event being executed during design time? It really suprises me that DesignMode is a property! Thanks Bob.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11936020
I am not sure of the exact answer, but this was true in VB6 also.  The UserControl is instantiated at design time, so that you can do special things, like control that size of the control, show sizing handles, etc.  If it wasn't instantiated, then you couldn't do any of that magic at design time.  I have seen plenty of errors just like you have seen.  Also, note that this property is only useful if the UserControl is hosted on a form.  If it is hosted on another UserControl, then it will always return False, since the system doesn't check up the chain.

Bob
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now