Error loading app.config file settings in a UserControl_Load event during DESIGN TIME!
Posted on 2004-08-30
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()
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
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.