views:

525

answers:

5

Hi,

I've got 3 connectionstrings in web.config, and I used theirs like this:

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SomeName"].ConnectionString))

Every metgod is called by winforms application.

One of webmethods doesn't work properly because it reads only one connectionString:

data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

It's not a connectionString from my web.config .

I invoke every method in the same way.

How it's impossible ??

EDITED:

I use facade: This is structure: WinForms calls WebMethod SaveItem SaveItem calls method on Facade: SaveItemAndDoDatabaseStuff SaveItemAndDoDatabaseStuff does database stuff.

A: 

You should store the connection-strings in the web.config or app.config in whatever project that you are executing.

In your case, you should have the connection-strings in your app.config for your winforms application.

Mickel
No, I use facade: This is structure:WinForms calls WebMethod SaveItemSaveItem calls method on Facade: SaveItemAndDoDatabaseStuffSaveItemAndDoDatabaseStuff does database stuff.Only this method doesn't work.
-1 OP had already mentioned that the DB was used from the server side.
eglasius
+4  A: 

We can't see your web structure, but is it possible that your app isn't configured as an application in IIS, therefore is picking up the master web.config? Which would look exactly like that...

Go into IIS and ensure it is an application (it may have a cog icon).

Marc Gravell
A: 

If your WinForms application talks directly to database X (not via the web service), then the connection string for database X should be in app.config (in the WinForms project).

If your Web Service (as I understand, this includes your facade and your database layer), talks to databases X, Y and Z, then the connection strings for X, Y, and Z need to be in web.config (in the Web Services Project).

Eric Eijkelenboom
A: 

Why not just add a Trace line to print out the configuration file being used just before you ask for the connection string. Simply add the following line:

        System.Diagnostics.Trace.WriteLine(
            System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
            "APP_CONFIG");

Set a breakpoint after this and look at the Output window for a line that starts with "APP_CONFIG:". This will give the full path to the configuration file and allow you to determine where it's being loaded. If you still see a discrepancy between the runtime values and the configuration file then likely something is changing those values at runtime within your application.

csharptest.net
+1  A: 

It's using the default connection string asp.net has (in the machine.config in the .net installation folders).

Do a clear:

<configuration>
  <connectionStrings>
    <clear/>
    ... your connection strings here
  </connectionStrings>
</configuration>

Btw, when you say you are using "SomeName" in the connection string. It isn't surely any random connection string you used, its the default: "LocalSqlServer".

eglasius