Home All Groups Group Topic Archive Search About

Connection string in app.config for class library

Author
13 Dec 2006 2:00 AM
Richard Bysouth
Hi

My VB solution consists of an EXE project and 2 class libraries (BL and
DAL). I am having problems with getting the correct DB connection string to
be read from my app.config files.
As I understand it, it is best to store these settings in the app.config of
the EXE. However, in my DAL project I am using several DataSets that were
created by dragging items from Server Explorer to the DataSet designer. Each
of these datasets references the connection string set in the DAL's
app.config. This is what I would like to remove - they should be referencing
the connection string in the EXE's app.config. The code that does this is in
the designer.vb files for each dataset, so it's not something I can change,
as changes would be reverted as soon as the file is regenerated.
How can I get around this? Seems like a common scenario to me. Have read
other posts in this group on similar topics but the only solution I found was
to create my own config engine, which seems overkill just for this!

thanks

Richard

-----------
Please provide code examples in VB if possible...thanks!
-----------

Author
13 Dec 2006 5:55 AM
Luke Zhang [MSFT]
Hello Richard,

As I understand, your DAL project is a class library project and has a
app.config like:

<connectionStrings>
        <add name="ClassLibrary1.Properties.Settings.mydbConnectionString"
            connectionString="Data Source=myserver;Initial
Catalog=testdb;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

And you wondering if we can add this connection in the exe project's config.

In fact, if we have such two projects (DAL and EXE), we can just add the
connection string:

  <add name="ClassLibrary1.Properties.Settings.mydbConnectionString"
            connectionString="Data Source=myserver;Initial
Catalog=testdb;Integrated Security=True"
            providerName="System.Data.SqlClient" />

to the exe project's config file's <connectionStrings> section (be careful
of that there is no existing one already there with same name). After we
compile the whole solution and execute from the EXE, the connection string
will be extract from the app.config ( for example, myapp.exe.config). When
a .NET DLL is called by an EXE, it will load it in its own process and all
request to app.config in the DLL will be redirected to EXE's app.config.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Author
13 Dec 2006 8:39 AM
Richard Bysouth
Hi Luke

I tried your suggestion and a number of different variations yet am still
unable to get the solution to build.
In the DAL project I have many errors, all of which are in the
designer-generated code for my datasets. For example:
        <System.Diagnostics.DebuggerNonUserCodeAttribute()>  _
        Private Sub InitConnection()
            Me._connection = New System.Data.SqlClient.SqlConnection
            Me._connection.ConnectionString =
Global.MyAppName.DataAccessLayer.Settings.Default.MyAppNameConnectionString
        End Sub

The
"Global.MyAppName.DataAccessLayer.Settings.Default.MyAppNameConnectionString"
is causing the error "MyAppNameConnectionString is not a member of
MyAppName.DataAccessLayer.Settings". These datasets were all generated when
there was an app.config in the DataAccessLayer project.

To confirm what I have done so far:
1) Removed all entries from the app.config in the DataAccessLayer project
2) Added the following to the <connectionStrings> section of the EXE
project's app.config:
    <add
name="MyAppName.DataAccessLayer.Properties.Settings.MyAppNameConnectionString"
         connectionString="Data
Source=(local)\SQLEXPRESS;AttachDbFilename=C:\Development\MyAppName\data\MyAppName.mdf;Integrated Security=True;"
         providerName="System.Data.SqlClient" />
3) Tried Rebuild Solution. This fails because of the errors listed above.

One other point that may be of relevance is that the root namespace for my
DAL project is MyAppName.DataAccessLayer; for my EXE project it is MyAppName.
I have tried all combinations of these in the app.config!

Any further suggestions?

many thanks

Richard
Author
14 Dec 2006 5:56 AM
Luke Zhang [MSFT]
Hello,

You don't need to remove all entries from the app.config in the
DataAccessLayer project, just leave them there. When you rebuild all
successfully, copy your executable files and DLL files to a folder, and the
app.config of exe project, and then run the executable file to see if it
can load the DLL and if the connection string can be recognized from its
app.config.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Author
15 Dec 2006 2:14 AM
Richard Bysouth
Hi Luke

Thanks - I have now fixed the problem and can connect successfully using the
connection string specified in my app.exe.config file.

In your example above you used
"ClassLibrary1.Properties.Settings.mydbConnectionString" however it seems
that the Properties bit was causing the problem! I removed that so now have
"ClassLibrary1.Settings.mydbConnectionString" and all works perfectly.

Have got a much better understanding of how this all works now!

Thanks very much for your help - a great relief that this is finally working.

Richard
Author
15 Dec 2006 10:17 AM
Luke Zhang [MSFT]
Hi Richard,

Thank you for the confirmation. I am glad to be assistance.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.