Archive for August, 2008|Monthly archive page

Encrypt DB Connection String

The web.config file of the website contains the user name and password to the SQL Server database. When uploading a website to another server, whoever has access to the server can read the login info for the database server. We can encrypt this connection string info in the web.config file.

Create a page with two buttons. One to encrypt and one to decrypt.

Protected Sub Button_Encrypt_Click(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub

Protected Sub Button_Decrypt_Click(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub

Protected Sub EncryptConfig(ByVal bEncrypt As Boolean)
Dim path = “/SCRP”

Dim config As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(path)
Dim appSettings As ConfigurationSection = config.GetSection(“connectionStrings”)

If bEncrypt Then
End If

End Sub

Other thoughts:
Maybe it’s better to compiled the whole website into .dll files…



the menu control in ASP.NET

To use sitemap file as the datasource for the the horizontal menu control,

  1. Put the SiteMapDataSource into the aspx page.
  2. Drag the menu control to the aspx page.
  3. Change its datasource to the SiteMapDataSource.
  4. Change the Orientation property of the menu to Horizontal.
  5. Change the StaticDisplayLevels property to 2, so that it will display the first 2 levels of the SiteMap. Default is one which is useless in my opinion if you are using a SiteMap file because it only shows the “Home” button or the first node in your SiteMap file.

Hyperlink MailTo Syntax

The MailTo command can do more than enter a single e-mail address in the “Send To” field while activating your e-mail program. It can also:

Feature Syntax
Address message to multiple recipients ,   (comma separating e-mail addresses)
Add entry in the “Subject” field subject=Subject Field Text
Add entry in the “Copy To” or “CC” field cc=id@internet.node
Add entry in the “Blind Copy To” or “BCC” field bcc=id@internet.node
Add entry in the “Body” field body=Your message here
Within the body use “%0A” for a new line,
use “%0A%0A” for a new line preceded by a blank line (paragraph),
see example below.

<a href=” from MailTo Syntax Page? world%0Afrom NYCharles”>


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.

Scalar-valued Functions in SQL Server

The new reporting features in SQL Server 2005 is very useful to generate a report, but putting the logic in the report is not as easy as in a regular aspx page. For example, I want to display some text in a new column depending on the values stored in the other columns in the database. I want it to be sortable in the report as well. This is where the Scalar-valued Functions in SQL Server comes in. Instead putting the logic in the report, it is much better putting it in the SQL Server.

Scalar-valued Functions in SQL Server allows DBA to create function to be called in views or stored procedures. The function can contains very complex logic to determine the output. The example below determines the text to be returned depends on the values of the fields. If season=summer and cloud=none, then it’s sunny.

USE [DB_Name]

ALTER FUNCTION [dbo].[svf_Weather]
@Weather_Date smalldatetime — input parameter

RETURNS varchar(20)

DECLARE @Weather varchar(20), @Season varchar(20), @Cloud varchar(20)

— Assigned values to variables
SELECT @Season = [Season], @Cloud = [Cloud]
FROM [DB_Name].[dbo].[qry_view_or_table]
WHERE [Weather_Date] = @Weather_Date

— The logic to determine the returned text
SELECT @Weather = CASE
WHEN @Season = ‘summer’ AND @Cloud = ‘none’ THEN ‘sunny’
WHEN @Season = ‘winter’ AND @Cloud = ‘cloudy’ THEN ‘freezing’
ELSE ‘others’
RETURN @Weather

Redirect if there is no PreviousPage

An online form has been broken down into several steps. Each step is a single aspx page. Sometimes user goes directly into a step in the middle.

To prevent visitor to go to a page directly, below is the code to check to see if there is no previouspage and redirect it to the default.aspx

In VB:
If IsNothing(PreviousPage) Then
Response.Redirect("Default.aspx" )
End If

In C#:
if (PreviousPage == null)

Set the DefaultFocus or DefaultButton in a Page Based on a Master Page

When user type in the keyword in the search textbox and press the Enter key on keyboard, the webpage just do a simple postback. It doesn’t run the query until the user hit the “Search” button.

Use defaultfocus and defaultbutton.
defaultfocus puts the mouse cursor into the control when the page loads
defaultbutton cause the button click event, without it, it just cause a pagepost back

Because of the Master page, UniqueID and ClientID is necessary for this to work properly.

protected void Page_Load(object sender, EventArgs e)
Page.Form.DefaultButton = this.Button_Continue.UniqueID;
Page.Form.DefaultFocus = this.TextBox_PN.ClientID;

Detect IE 5 or higher

I used this code to alert user to use IE 5 or higher to browse the website.

In the HTML head:
<script type=”text/javascript” language=”JavaScript”>
function detectBrowser()
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
if ((browser!=”Microsoft Internet Explorer” ) || (version<4))
alert(“Please use Internet Explorer to continue”);

In the HTML body:
<body onload=”detectBrowser()”>

No javascript detection message

Below is the html code that I have been using for many website to tell the visitor to enable the Javascript. Any code between the <noscript> tag will only be displayed if user’s browser doesn’t support Javascript. It will put the message fixed at the top of the website. The message will take about 3.5em, that is why I have the top margin in the <noscript> tags.

In the aspx page:
<style type=”text/css”>
body { margin-top:3.5em; }
<div id=”noscript-warning” style=””> This website works best with JavaScript enabled </div>

In the css file:
#noscript-warning {
padding:10px 0;

Add dll file to Visual Studio project

To add a .dll file into the project, right-click anywhere in the Solution Explorer and choose “Add Reference…”. Click the Browse tab and choose the .dll file. Go to View->Object Explorer to see all the function that comes with the .dll file.

Right click the Toolbox section, click “Choose Items…”. Click the browse button and choose the dll file. Click OK. The dll object should appears in the toolbox now. Just drag and drop the object into the website and configure its properties.