SQL ConnectionString in global.asax overridden by web.config

Posted by rlb.usa on Stack Overflow See other posts from Stack Overflow or by rlb.usa
Published on 2010-03-23T19:22:38Z Indexed on 2010/03/23 19:33 UTC
Read the original article Hit count: 358

This is going to sound very odd, but I have a web.config like this:

  <connectionStrings>
    <remove name="LocalSqlServer"/>
     <add name="LocalSqlServer" connectionString="Data Source=BACKUPDB;..." providerName="System.Data.SqlClient"/> 
  </connectionStrings>

And a global.asax like this:

void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started
    if (Application["con"] == null || Application["con"] == "")
    {
        Application["con"] = "Data Source=PRODUCTIONDB;..."; 
    }
}

And EVERYWHERE in my code, I reference my ConnectionStrings like this:

SqlConnection con = new SqlConnection(Convert.ToString(HttpContext.Current.Application["con"]));

However, I see that everything I do inside this application goes to BACKUP db instead of PRODUCTIONDB. What is going on, how could this happen, and why? It doesn't make any sense to me, and it got me into a lot of trouble. We use LocalSqlServer string for FormsAuthentication.

© Stack Overflow or respective owner

Related posts about connection-string

Related posts about ASP.NET