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

Reporting Services Startup Slow

Unlike other ASP.NET application, the reporting services that comes with the SQL Server always startup slow. I google it a while and comes to an article on how to make it faster. Below is the section explaining why it is slow and how to make it faster:

Other things that cause additional overhead when Reporting Services starts up:

  1. Every time the SRS 2005 web service loads, it also has to read and decrypt the rsreportserver.config file
  2. Since there are no connections in the connection pool, we have to physically open up a socket connection between the two servers, plus log into the database instance
  3. The web service has to make RPC calls into the Windows Service to get the symmetric encryption key

This explanation is to try and provide an overview of some of the things that are going on during Reporting Services initial start up.  Remember, most of these things probably do not happen in traditional web applications. Again, all of this is completely expected behavior.

As discussed earlier, if this behavior causes some business issues, you could consider modifying the recycle options on your IIS process.  You can either increase the recycle time (causing them to be recycled less frequently) or schedule the recycle to occur at a non-peak time.  You could then combine this second option with a “ping” process that hits the process shortly after the recycle.  This will “wake” the ReportServer processes so that your initial customer doesn’t see the initialization time.  If you combine these options with turning off the idle worker process shutdown, you can significantly minimize the instances where a user would run into the startup delay.

Good reference for Regular Expression

http://www.gnosis.cx/publish/programming/regular_expressions.html

Working with email addresses in SQL Server

Below is an example of adding email address constraint in the database field.

ALTER TABLE [dbo].[Subscribers]
WITH CHECK ADD
CONSTRAINT [EmailValidator]
CHECK
(
CHARINDEX(' ',LTRIM(RTRIM([Email]))) = 0 -- No embedded spaces in email
AND LEFT(LTRIM([Email]),1) <> '@'
-- '@' can't be the first character of an email address
AND RIGHT(RTRIM([Email]),1) <> '.'
-- '.' can't be the last character of an email address
AND CHARINDEX('.',[Email],CHARINDEX('@',[Email])) - CHARINDEX('@',[Email]) > 1
-- There must be a '.' after '@'
AND LEN(LTRIM(RTRIM([Email]))) - LEN(REPLACE(LTRIM(RTRIM([Email])),'@','')) = 1 
-- Only one '@' sign is allowed
AND CHARINDEX('.',REVERSE(LTRIM(RTRIM([Email])))) >= 3 
-- Domain name should end with at least 2 character extension
AND (CHARINDEX('.@',[Email]) = 0 AND CHARINDEX('..',[Email]) = 0) 
-- can't have patterns like '.@' and '..'
)
GO

To check invalid email address in the database field, use the code below.


SELECT * FROM Subscribers
WHERE NOT
(
CHARINDEX(' ',LTRIM(RTRIM([Email]))) = 0
AND LEFT(LTRIM([Email]),1) <> '@'
AND RIGHT(RTRIM([Email]),1) <> '.'
AND CHARINDEX('.',[Email],CHARINDEX('@',[Email])) - CHARINDEX('@',[Email]) > 1
AND LEN(LTRIM(RTRIM([Email]))) - LEN(REPLACE(LTRIM(RTRIM([Email])),'@','')) = 1
AND CHARINDEX('.',REVERSE(LTRIM(RTRIM([Email])))) >= 3
AND (CHARINDEX('.@',[Email]) = 0 AND CHARINDEX('..',[Email]) = 0)
)

Reference:
http://vyaskn.tripod.com/handling_email_addresses_in_sql_server.htm

See actual ASP Error database connection not defined

ASP Error:

Database Results Wizard Error
The operation failed. If this continues, please contact your server administrator.

To see the actual error, edit the "_fpclass\fdbdrgn1.inc" file and set the "fp_DEBUG = True".

ASP Error:

Database Results Wizard Error
The database connection named 'HRcontacts' is undefined.

Solution: I fixed this by adding the website to the app pool in IIS 6

PHP error No input file specified.

I received this error when changing the home virtual directory in IIS 6. The default location was at C:\inetpub\wwwroot and I changed it to E:\. This cause the No input file specified error. To fix it, I open the php.ini file and change the value of the doc_root variable. In my case, I set doc_root = E:\

Restart the server and all the php website work again.

ASP website not running in Windows Server 2003 IIS6

I just moved some ASP websites to a new installed Windows Server 2003 with IIS 6 installed. When I visit the website, it gave me a 404 error which mean files not found. This is the most common problem with a new site under IIS 6.0. By default, IIS 6.0 is only allowed to serve static content. No ASP, no ASP.NET… so calls to ASP pages will actually trigger a 404 error.

This fix this:
1.    Open IIS Manager, expand the master server node (that is, the Servername node), and then select the Web service extensions node.
2.    In the right pane of IIS Manager, right-click the extension that you want to enable. In this example, this is Active Server Pages.
3.    Click to select the Allow check box.

Internet Guest Account in Windows security

The internet guest account for IIS is IUSR_MACHINENAME where the machine name depends on the name of the computer. Giving the read/write permission of this account for different files or folders is important.

Active Server Pages, ASP 0131 Disallowed Parent Path

Internet Information Services 6.0

1. Open the Internet Services Manager in the Microsoft Management Console (MMC).
2. Right-click on your Default Web Site and select Properties.
3. Click the Home Directory tab.
4. Click the Configuration button.
5. Click the App Options tab.
6. Click to select the Enable Parent Paths checkbox.
7. Click the OK button until you return to the MMC.

Internet Information Services 7.0

1. Start Internet Services Manager.
2. Click Default Web Site, and then click Properties.
3. Double-click ASP in the Features pane.
4. Expand Behavior.
5. Click Enable Parent Paths.
6. Click True for Enable Parent Paths.
7. Click Apply.

CAUSE

loadTOCNode(1, ’cause’);

This is caused by disabling ASP’s “parent paths” for a Web site or application while using relative parent paths in an include statement.

RESOLUTION

loadTOCNode(1, ‘resolution’);

The best solution to the problem is to use absolute virtual paths from the root of the Web site instead of relative paths.

For example, if you use an include file named “mycode.inc” at the root of your server, the virtual path would be “/mycode.inc.” If you use the same include file in a virtual directory named “/includes” on your server, the virtual path would be “/includes/mycode.inc.”

Allow anonmymous access in IIS 6

How to configure Authentication in IIS

1. Start IIS Manager or open the IIS snap-in.
2. Expand Server_name, where Server_name is the name of the server, and then expand Web Sites.
3. In the console tree, right-click the Web site, virtual directory, or file for which you want to configure authentication, and then click Properties.
4. Click the Directory Security or File Security tab (as appropriate), and then under Anonymous and access control, click Edit.
5. Click to select the check box next to the authentication method or methods that you want to use, and then click OK.

Resource: http://support.microsoft.com/kb/324274