Link to home
Start Free TrialLog in
Avatar of minermadison
minermadison

asked on

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

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.

Avatar of minermadison
minermadison

ASKER

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.
Avatar of Bob Learned
You might want to check in the UserControl, whether you are in design time or run-time.

If Not Me.Design Then
...
EndIf

Bob
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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