Archive for the ‘Shortcut’ Category

Use different connection strings depends on current URL

I manage two servers at work. One for development and the other one is production server. I used to have two different web.config file and have different connection string to the database in them. But what I should do is write an if statement to decide what to use depends on what the current URL is.

In <appSettings> tag of the “web.config” file, add the following:

<appSettings>
<add key=”Staging” value=”http://my-staging-server.com&#8221; />
<add key=”DevelopmentConnectionString” value=”server=Dev-DB01; database=DBSample;Integrated Security=SSPI”/>
<add key=”ProductionConnectionString” value=”server=Dev-DB01; database=DBSample;Integrated Security=SSPI”/>
</appSettings>

Create a Global Application Class file and name it “Global.asax“. In the file, add the following:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
If (Request.Url.Host = ConfigurationManager.AppSettings(“Staging”)) Then
Context.Items.Add(“ConnectionString”,ConfigurationManager.AppSettings( “DevelopmentConnectionString”))
Else
context.Items.Add(“ConnectionString”,ConfigurationManager.AppSettings( “ProductionConnectionString”))
End If
End Sub

Advertisements

Put code into readable format using Visual Studio

In visual studio, there is a quick way to indent and put code into readable format. In source view, Edit->Advanced->Format Document or simply use the shortcut, press Ctrl+K first, then Ctrl+D.