0

I have a very basic ASP.NET web application that is connecting to a SQL Server database (I am using SQL Server 2008 Express). The problem I am having it is very odd: if I set the connection string for the SqlConnection object directly in code, the application works fine, but if I move my connection string to the web.config file, the application fails.

I show you both the variable and the connection section in the web.config to see if you detect any error:

In the variable:

// works fine
string cs = "data source=.; database=myDataBase; integrated security=SSPI";
SqlConnection conn = new SqlConnection(cs);

Now in the web.config:

<configuration>
    <connectionStrings>
         <add name="myCS"
              connectionString="data source=.; database=myDataBase; integrated security=SSPI"
              providerName="System.Data.SqlClient" />
    </connectionStrings>

...

// Now, if I do this in C#, it does not work:
string cs = ConfigurationManager.ConnectionStrings["myCS"].ConnectionString;
SqlConnection conn = new SqlConnection(cs);

It tells me that there is already a database with that name. Now, I have read things everywhere. Some guy said that instead of using "database" in the connectionString, I could use "initial catalog" and all would be fine. Well, it wasn't fine... I have the same problem... Does anyone know what I am doing wrong? Why is working in one place and not in another? If the connection string was wrong, it should fail in both places, and it is only crashing in the webConfig file... Thanks very much...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alan Espinet
  • 114
  • 6

1 Answers1

-1

If you are running this from Visual Studio, have you tried stopping the Dev Server (running in your system tray) and trying? the Web.config file is normally cached and could cause issues.

A.J
  • 382
  • 2
  • 6