Solved

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

Posted on 2004-08-30
5
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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