Re: DynamicProperties connection string not working in inherited form in design mode
From: Christian Kuendig (kuendigc*removethis*_at_spectraweb.ch)
Date: 02/28/04
- Next message: Christian Kuendig: "Re: CodeDom: how to create the code for ResourceManager"
- Previous message: Günter Schwaiger: "Re: CodeDom: how to create the code for ResourceManager"
- In reply to: Spencer Quinn: "DynamicProperties connection string not working in inherited form in design mode"
- Next in thread: Spencer Quinn: "Re: DynamicProperties connection string not working in inherited form in design mode"
- Reply: Spencer Quinn: "Re: DynamicProperties connection string not working in inherited form in design mode"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 28 Feb 2004 17:56:18 +0100
Hi Spencer,
I suppose you get the error because your super class (Form1) get's actually
executed with the designer and that one just can't find that configuration
file.
Why don't you set up the connection string with the value from the
configuration file after the InitializeComponent has been called. When it's
inside the Ctor it probably doesn't help much, but then you could at least
insert a try - catch around it...
An other attempt could be to set up the connection string in the OnFormLoad
and check whether your in design mode or not.
And last but not least, I would consider to have something like a Data
Access layer that abstracts the data access from your UI.
regards
Christian
"Spencer Quinn" <anonymous@discussions.microsoft.com> wrote in message
news:26DD4AEE-BB36-4686-A795-20CDC857C483@microsoft.com...
> I am experiencing a problem using the dynamicproperties connection string
that is defined in a base form and is intended to be used by derived forms.
The message I get when attempting to open Form2 (derived form from Form1
which contains the SQLConnection object) is:
> An exception occurred while trying to create an instance of
WindowsApplication1.Form1. The exception was "The key
'SqlConnection1.ConnectionString' does not exist in the appSettings
configuration section.".
>
> Exact steps to recreate this problem:
> 1. Open VisualStudio.NET 2003
> 2. Click File
> 3. Click New solution
> 4. Leave Solution1 as default name, on D:5. Click OK
> 6. Go to solution explorer tab (Ctrl-Alt-L)
> 7. Right click on Solution1
> 8. Select Add
> 9. Select New Project
> 10. Choose Visual Basic Projects
> 11. Choose Windows Application - Allow Name to default to
WindowsApplication1, location to default to D:\Solution1
> 12. Click OK
> 13. Open Form1.vb in design mode
> 14. Open the toolbox (Ctrl-Alt-X) and open the Data group
> 15. Drag an SQLConnection to the form and allow it to be named
SQLConnection1
> 16. Select SQLConnection1 on Form1
> 17. Go to Properties window by pressing F4
> 18. Change modifiers from Friend to Protected (after all, we want to
inherit it)
> 19. Place cursor in Connection string and press dropdown arrow
> 20. Select <New connection.>
> 21. Type (local) or a server instance for the server name
> 22. Choose authentication method - in this example I am choosing Windows
NT Authentication, but you chould just as easily choose SQL Server
authentication
> 23. Select the Northwind database from the server, click Test connection
to ensure it works
> 24. Click OK to Test connection succeeded
> 25. Click OK for database connection screen
> 26. You should now have a string similar to the following in your
connection string property:
> workstation id=xxxxxxxx;packet size=4096;integrated security=SSPI;data
source="(local)";persist security info=False;initial catalog=Northwind
> 27. Expand dynamic properties in properties window
> 28. Place cursor in dynamic properties, connection string
> 29. Click ellipses (.) button
> 30. Check Map property to a key in configuration file, leave key at
SQLConnection1.ConnectionString
> 31. Click OK
> 32. Switch to solution explorer (Ctrl-Alt-L)
> 33. You should now have an app.config file
> 34. Double click on it and ensure you have the following section:
> <appSettings><!-- User application and configured property settings go
here.--><!-- Example: <add key="settingName"
value="settingValue"/> --><add key="SqlConnection1.ConnectionString"
value="workstation id=xxxxxx;packet size=4096;integrated security=SSPI;data
source="(local)";persist security info=False;initial
catalog=Northwind" /></appSettings>
>
> All seems correct to this point. Now I want to inherit Form1 and the
SQLConnection1 object for all of my forms - seems to make sense to me to
want to do this since all my forms will be using the same database.
>
> 35. Build the application (required before I inherit from Form1)
> 36. In solution explorer, right click on WindowsApplication1
> 37. Select Add
> 38. Select Add Inherited From
> 39. Allow it to be named Form2.vb
> 40. Click Open
> 41. Allow Form1 to be selected
> 42. Click OK
> 43. You should now receive the message in the design window:
> An exception occurred while trying to create an instance of
WindowsApplication1.Form1. The exception was "The key
'SqlConnection1.ConnectionString' does not exist in the appSettings
configuration section.".
> 44. Open app.config and verify the connection string exists as documented
in #34 above.
>
> The only way I know to fix this is to make Form1 use a static connection.
I have even tried reading with the System.Configuration.AppSettingsReader,
but I get the same error as in 43. This only happens in design mode, not
while executing.
>
> Any help is appreciated.
> Spencer
- Next message: Christian Kuendig: "Re: CodeDom: how to create the code for ResourceManager"
- Previous message: Günter Schwaiger: "Re: CodeDom: how to create the code for ResourceManager"
- In reply to: Spencer Quinn: "DynamicProperties connection string not working in inherited form in design mode"
- Next in thread: Spencer Quinn: "Re: DynamicProperties connection string not working in inherited form in design mode"
- Reply: Spencer Quinn: "Re: DynamicProperties connection string not working in inherited form in design mode"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|