Solved

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

Posted on 2004-08-30
5
226 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

705 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