SQL ConnectionString in web.config and global.asax implications
- by rlb.usa
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.