Guys, thanks for all your helpful posts. I am
relatively new to asp.net, and I have a very similar problem to those described
above...
I am writing a data access layer for a database which will be used in a web
application.
I would like to use Nunit to test the data access layer, and have written a
simple windows application that runs unit tests on a test database.
I am using data sets and table adapters and would like for the table adapters
to be able to use a connection string specified either in the web.config, or
the app.config file, depending on whether the DAL library is being called from
either the web application or the windows test application.
Currently I have focused on trying to get the library dataTable adapters to
pick up a connection string defined in the web.config file.
I have tried Bradley’s
suggestion, but with no luck so far. I'm going crazy here, and would love it if
someone with more experience could help me out...
I have some questions:
In the DAL library, there is the 'Settings.settings' and the 'app.config'
file. Am I correct to think that I should configure my table adapter to
use a connection string from the Settings.settings file - lets call the
connection string 'HappyConnectionString'. then, at runtime, when the
'Settings' class in the DAL library is asked for the value of
'HappyConnectionString', it will somehow overwrite the value of happyConnectionString
with the value it finds in the web.config file, as long as in the web.config
file I have specified the fully qualified name of the connection string?
so in my project, I have the following...
A class library called HappyAppDAL. In this library, there is a folder called
properties, and in the properties folder there is a file called
Settings.settings. I use the designer to make sure there is one entry in
the Settings.settings file. The entry has the following attributes:
Name - HappyConnectionString
Type - (Connection String)
Scope - Application
Value - Data Source=jim\sqlexpress;Initial Catalog=TestDatabase Security=True
Now I have a web project which uses HappyAppDAL. in the web.config file I
have the following section:
<configuration >
<appSettings>
...
</appSettings>
<connectionStrings>
<add
name="HappyAppDAL.My.MySettings.HappyConnectionString"
connectionString="Data Source=jim\sqlexpress;Initial
Catalog=ProductionDatabase;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
When I build the web project and run it, I put a breakpoint just after I have
created an instance of a HappyAppDAL table adapter, I look at the attributes of
the table adapters connection, and am dismayed to see that it is using the
connection string defined in the HappyAppDAL Settings.settings file
(Catalog=TestDatabase), and not the connection string from the web.config file
(Catalog=ProductionDatabase).
In fact – I also put a breakpoint on the generated code in
the file Settings.Designer.cs, which is the get method for the property ‘HappyConnectionString’
– I never seem to hit this breakpoint!!
Can anybody please tell me what i am doing wrong here? is my connection
string name not correct? not fully qualified? Am I supposed to delete the app.config and
Settings.settings file in the HappyAppDAL project??
Any help on this would be greatly appreciated. Sorry for writing a novel,
but I wanted to try to explain myself clearly in the hope that someone might be
able to help me.
Thanks in advance.
Glyn.