Re: DynamicProperties connection string not working in inherited form in design mode

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Christian Kuendig (kuendigc*removethis*_at_spectraweb.ch)
Date: 02/28/04


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=&quot;(local)&quot;;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



Relevant Pages

  • Re: Connection string issue when in a separate web.config
    ... permissions for the Secure folder. ... the indexer on .ConnectionStrings expects the name of the connection string, ... the Website->Asp.Net Configuration option in Visual Studio. ...
    (microsoft.public.dotnet.security)
  • Re: Connection string issue when in a separate web.config
    ... the indexer on .ConnectionStrings expects the name of the connection string, ... I have a App_Code\DAL folder. ... the Website->Asp.Net Configuration option in Visual Studio. ... The section enables configuration ...
    (microsoft.public.dotnet.security)
  • Re: Connection string issue when in a separate web.config
    ... permissions for the Secure folder. ... the indexer on .ConnectionStrings expects the name of the connection string, ... the Website->Asp.Net Configuration option in Visual Studio. ...
    (microsoft.public.dotnet.security)
  • Re: Configuration for DLL
    ... The classes in the DLL will be used by VBScripts. ... How can I get it to look at the installation directory? ... assembly is called "MyConnection.dll" create a configuration ... Add your connection string to it: ...
    (microsoft.public.dotnet.general)
  • Re: Security in windows forms apps
    ... On another note since this is an internal app I am not too worried about it ... You need to ensure that your not setting these values at design time. ... I have encrypted the connection string in the app.config file, ... rename the exe to .txt. So it will now be app.txt. ...
    (microsoft.public.dotnet.languages.csharp)