Friday, July 18, 2008

I needed to validate a form for numeric and integer fields. The C-Sharp Corner seems to cover Regular Expressions pretty well in this article.

Essentially, I created a new class named RegularExpressions and added the following:

using System.Text.RegularExpressions;

// Function to test for Positive Integers.
public bool IsNaturalNumber(String strNumber)
Regex objNotNaturalPattern=
new Regex("[^0-9]");
Regex objNaturalPattern=
new Regex("0*[1-9][0-9]*");
return !objNotNaturalPattern.IsMatch(strNumber) &&
// Function to test for Positive Integers with zero inclusive
public bool IsWholeNumber(String strNumber)
Regex objNotWholePattern=
new Regex("[^0-9]");
return !objNotWholePattern.IsMatch(strNumber);
// Function to Test for Integers both Positive & Negative
public bool IsInteger(String strNumber)
Regex objNotIntPattern=
new Regex("[^0-9-]");
Regex objIntPattern=
new Regex("^-[0-9]+$|^[0-9]+$");
return !objNotIntPattern.IsMatch(strNumber) && objIntPattern.IsMatch(strNumber);
// Function to Test for Positive Number both Integer & Real
public bool IsPositiveNumber(String strNumber)
Regex objNotPositivePattern=
new Regex("[^0-9.]");
Regex objPositivePattern=
new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
Regex objTwoDotPattern=
new Regex("[0-9]*[.][0-9]*[.][0-9]*");
return !objNotPositivePattern.IsMatch(strNumber) &&
objPositivePattern.IsMatch(strNumber) &&
// Function to test whether the string is valid number or not
public bool IsNumber(String strNumber)
Regex objNotNumberPattern=
new Regex("[^0-9.-]");
Regex objTwoDotPattern=
new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern=
new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
Regex objNumberPattern =
new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
// Function To test for Alphabets.
public bool IsAlpha(String strToCheck)
Regex objAlphaPattern=
new Regex("[^a-zA-Z]");
return !objAlphaPattern.IsMatch(strToCheck);
// Function to Check for AlphaNumeric.
public bool IsAlphaNumeric(String strToCheck)
Regex objAlphaNumericPattern=
new Regex("[^a-zA-Z0-9]");
return !objAlphaNumericPattern.IsMatch(strToCheck);

Instantiate the class in the form with the textboxes, and there you have it.

Friday, July 11, 2008

I was installing a web application today, and came across a strange problem. ASPX .Net 2.0 wouldn't start. Luckily, a google search of "error 0x80131902" brought me to "Jonathan's Blog", which explained how to fix the issue.

Essentially, all that was needed was the following steps (quoted from Jonathan's article):

  1. With a command window, get to the latest version of .net under
  2. C:\Windows\Microsoft.Net\Framework\
  3. Now run the following command: "net stop w3svc" to stop web services.
  4. Then use "aspnet_regiis.exe -ua" to uninstall all instances of ASP.NET from IIS.
  5. Follow with "aspnet_regiis.exe -i" to install ASP.NET into IIS.
  6. Now restart web services with "net start w3svc".
One small caveat I'd like to add here... You actually need to run the aspnet_regiis.exe from its containing folder, which for me was C:\Windows\Microsoft.Net\Framework\v2.0.50727.

Tuesday, July 1, 2008

Visual Studio 2008 has this nifty tool for scripting databases. If you have an existing data connection in your Server Explorer, you can right-click on a database, select "Publish to Provider", and voila, you have your entire database scripted to a file. Now, you may think "Hey, I can run this on another server, and it will allow me to move this database quickly and easily." However, you would only be half-right.

Don't forget to edit that script such that the Login Name, User Name, and Database (or server) roles are set within the script.

This basically translates to using the following (changing the appropriate key words)

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'DATABASE NAME')


****** Object: User [USERNAME] Script Date: 06/30/2008 10:48:21 ******

* Set up default database for user *
sp_defaultdb 'USERNAME' , [DATABASENAME]
sp_changegroup 'DATABASE ROLE', 'USERNAME'