Shane Duffy

Archive for the ‘VB.NET’ Category

What the Heck is a Regular Expression Anyway?

I’m sure you are familiar with the use of “wildcard” characters for pattern matching. For example, if you want to find all the Microsoft Word files in a Windows directory, you search for “*.doc“, knowing that the asterisk is interpreted as a wildcard that can match any sequence of characters. Regular expressions are just an elaborate extension of this capability.

In writing programs or web pages that manipulate text, it is frequently necessary to locate strings that match complex patterns. Regular expressions were invented to describe such patterns. Thus, a regular expression is just a shorthand code for a pattern. For example, the pattern “\w+” is a concise way to say “match any non-null strings of alphanumeric characters”. The .NET framework provides a powerful class library that makes it easy to include regular expressions in your applications. With this library, you can readily search and replace text, decode complex headers, parse languages, or validate text.

Some Simple Examples

Searching for Elvis

Suppose you spend all your free time scanning documents looking for evidence that Elvis is still alive. You could search with the following regular expression:

1. elvis Find elvis

This is a perfectly valid regular expression that searches for an exact sequence of characters. In .NET, you can easily set options to ignore the case of characters, so this expression will match “Elvis”, “ELVIS”, or “eLvIs”. Unfortunately, it will also match the last five letters of the word “pelvis”. We can improve the expression as follows:

2. \belvis\b Find elvis as a whole word

Now things are getting a little more interesting. The “\b” is a special code that means, “match the position at the beginning or end of any word”. This expression will only match complete words spelled “elvis” with any combination of lower case or capital letters.

Suppose you want to find all lines in which the word “elvis” is followed by the word “alive.” The period or dot “.” is a special code that matches any character other than a newline. The asterisk “*” means repeat the previous term as many times as necessary to guarantee a match. Thus, “.*” means “match any number of characters other than newline”. It is now a simple matter to build an expression that means “search for the word ‘elvis’ followed on the same line by the word ‘alive’.”

3. \belvis\b.*\balive\b Find text with “elvis” followed by “alive”

With just a few special characters we are beginning to build powerful regular expressions and they are already becoming hard for we humans to read.

Let’s try another example.

Determining the Validity of Phone Numbers

Suppose your web page collects a customer’s seven-digit phone number and you want to verify that the phone number is in the correct format, “xxx-xxxx”, where each “x” is a digit. The following expression will search through text looking for such a string:

4. \b\d\d\d-\d\d\d\d Find seven-digit phone number

Each “\d” means “match any single digit”. The “-” has no special meaning and is interpreted literally, matching a hyphen. To avoid the annoying repetition, we can use a shorthand notation that means the same thing:

5. \b\d{3}-\d{4} Find seven-digit phone number a better way

The “{3}” following the “\d” means “repeat the preceding character three times”.

Read the rest of this entry »


Following on from my previous post Getting into Agile Development with .NET.

Atlassian’s professional WIKI solution, it works great with JIRA and provides a really nice WIKI solution. For those who want to travel light with documentation, WIKI’s provide a nice fit for technical documentation, build documentation, internal designs, etc. as they easily accommodate rapid changes, are completely web based and allow for automatic version tracking.

Data Dictionary Creator
One of the principles of Agile is make your code self-documenting. What about making your database self-documenting? You can do this in SQL 2005 in T-SQL by adding meta-data and descriptions to each field in your database. The Data Dictionary Creator tool does the same thing through a nice GUI and allows for export and import of the documentation. You can then document your database and store the meta-data in source safe for version control and safe keeping. In addition, you can export the descriptions to an excel file for instant documentation!

Wisdom Screen Hunter
Not really a development tool, but how many times have you been creating documents and wanted a quick screen capture? Forget using the PrntScrn and fiddling in paint – use this tool. Its free and allows you to capture any arbitrary rectangular area as well as any active window. Simply hit F6 and off you go.

Some tools suggested by my readers.

A plug in for Visual Studio which really helps by reducing the amount of time and hassle of opening another environment or tool to run tests in, Launches the right out of your VS Dev Environment.

MbUnit over NUnit
Syntax is identical, but MbUnit includes some additional test capabilities. For example the RowTest attribute can be used to run the same test with a variety of test conditions. Repeat can be used for stress testing, and ProcessTestFixture/TestSequence can be used for ordered testing.

NMock is a dynamic mock object library for .NET. Mock objects make it easier to test single components—often single classes—without relying on real implementations of all of the other components. This means we can test just one class, rather than a whole tree of objects, and can pinpoint bugs much more clearly. Mock objects are often used during Test Driven Development.

Thanks mdenomy always good to get some feedback, Please leave your comments and suggestions.

We have a basic single sign-on requirement. We need to be able to allow users to login and pass that login to other distributed systems.

Here are the basic requirements:

1. We want to control the master user profile so that we can have access to the data and integrate it into our offline CRM systems.

2. We have a number of partners who provide hosted applications for us that require authentication. We need to provide a mechanism to have a single sign-on for users that can then be passed to to these partners.

3. The partners may have their own profile requirements that are independent of our needs. For example, let’s assume we have a partner that provides community forums and saves the user’s favourite forums in their profile. Only the partner cares about this information and centralizing it doesn’t add any real value.

4. The solution has to work in a distributed, multi-platform environment. The only access to and from the partners and us is through port 80 using either REST or Web Services calls.

5. The domain may not be the same and therefore you cannot share cookies (see and for a solution based on the assumption that you can simply poke the cookie created by ASP.NET)

6. There needs to be a way to transfer state from one server to another in cases where the user is logged in already. If user logs in Site A and then goes to Site B, they should appear to be still logged in.

So here is the basic design:

Single Sign On Diagram 1

1. User logs in by calling a login web service method (see this example for a web services method implementation of login). User is now authenticated on the main site.
2. User links to partner site which passes a token representation of the state (we could use the SessionID itself in .NET as a token).
3. The partner site receives the token and calls a ValidateToken web service method. This validates the passed in token and provides back the membership information for the current user.
4. If the partner site collects user information, the site can call the Update web service method to update the master profile.
5. If the user wants to link back from the partner site to the main site, the partner site provides a link back to the main site with the token embedded in the query string.
6. The main site receives the token and calls a ValidateToken web service method. This validates the passed in token and provides back the membership information for the current user.

This basic architecture should work with a couple basic caveats:

1. State needs to be managed centrally with the usual challenges of managing session state in a load balanced environment.
2. There needs to be some security around the token validation service to prevent malicious sites from getting access to the service. There are lots of solutions for this including putting the web services under VPN, including a site authentication mechanism in the web service method, filtering by IP, etc.
3. Each link needs to be dynamically re-written to include the token. If there are lots of existing cross-over points then this could be a challenge.
4. The parsing of the token needs to happen on every page request. In .NET this could be done using an HTTP Module as a central parsing code. Alternatively, you could have a redirect page that all pages link to first with the original target url embedded in a query parameter (this is how typical login pages work for example).
5. There needs to be some mechanism for keeping the state alive so that it doesn’t time out. This could again done using an HTTP Module that on every page request does a web service request to validate token to simply keep the state alive.
6. The solution assumes that there is a single backing master profile that distributes profile information. A “peer to peer” type model could be developed but it would be more complicated.

From a look and feel perspective, the solution doesn’t require a single login or registration experience but it probably would make sense. Keep the login and registration forms on the main web site and have them redirect back to the partner site if the request is coming from the partner.

The following is a list of tools for doing agile based development with Microsoft .NET. There are quite a few tools out there that will accelerate your use of continuous integration, test driven development, source control integration, etc.


Best tool for task management. For more details see my review of JIRA from a previous post. It’s bugzilla on steroids and is much better and simpler than most windows based tools. It’s java based, but there is still no reason not to use it for .NET based development.


NUnit is an implementation of the XUNIT framework for .NET. It allows you to create unit test classes that can be run by either a GUI or console application. NUnit will also integrate into your build process so that on each build, you run through your automated tests to make sure they still pass.


NANT is a port of the ANT build tool from Java. NANT allows for build scripts based on simple XML files. NANT can do things such as compile code, deploy assemblies, manage directories, start and stop processes, run executables, etc.


NCover is a code coverage tool for .NET. It will instrument your assemblies when they are built to analyze each line of code in your assembly and whether it was actually executed (exercised). If you combine this with NUnit, you can then tell how much your unit tests actually go through every path of code.

dotTrace or Ants Profiler

Either tool will do, but personally I like Ants Profiler better and you can get it as a bundle with Ants Loader for cheaper. Both tools do the same thing – they profile either memory or performance data for your executing code. You can profile an ASP.NET web site, a Winform application or a Windows Service. What you will get in the case of memory profiling is the ability to snapshot at any point of execution a picture of what is in memory. This can help with objects not getting garbage collected properly or where you’re running into memory issues. For performance profiling, the tool will tell you the amount of time each line of code took to execute. This can be useful for performance testing in that you can see the breakdown of time for a method to isolate the particular lines of code that are taking the longest.

Cruise Control .NET

Cruise Control is a port of the java application of the same name. It is a continuous integration server which will fire a build on every check into your source code repository. It can run either a NANT script or an MSBuild script or simply tell Visual Studio 2005 to do a build. It then stores the status of each build in a little intranet site that you can monitor and it can email you notifications as well. This provides an automated way to integrate code and test to make sure it builds and passes all your unit tests.

My Generation

My Generation is a code generator. It ships with a number of templates for generating O/R mapping code for frameworks such as Gentle, NHibernate, DAAB, etc.

TestDriven .NET

This is one of my favourite tools. It allows you to integrate your unit testing right into studio. Want to run one of your NUnit Tests? Just right click on the test code and up comes a Run with option that you can then run with the debugger, NCover, NUnit, etc. The one I use most is run with Debugger as it means you can step through the code hit by each of your unit tests.


Ankh is a subversion add-in for Visual Studio 2005. For those who are using subversion as your source code control system (and I would recommend subversion in general over Visual Source Safe) it allows you to update and commit code from the solution explorer window. My only problem with this tool is that its quite slow especially if you have large projects, so you may find it easier to check in with external tools after a while. But for those who are just getting used to subversion, its a great first step.

Creating Task List Comments in your projects can ensure you to complete the important and esential tasks. This feature can be used to indicate a variety of work to be done at the location marked

The Task List displays comments embedded in your project code that begin with the comment marker for your development language, followed by a default task token such as TODO, HACK, or UNDONE, or a custom comment token.

The number of comments that appear in the Task List changes, depending on the type of project you are working on.
With Visual Basic projects, the Task List displays all of the comments in the project.
With Visual C# and Visual J# projects, the Task List displays only the comments that are found in the files currently opened for edit.
With Visual C++ projects, the Task List displays only the comments that are found in the file currently active in the editor.

To add a comment to the Task List
1. Open a source code file for editing in the Code Editor.

2. Begin a comment on a line of code you want to mark with <marker><token>, where <marker> is the comment marker for your development language, and <token> is the name of a recognized Task List comment token, such as TODO, HACK, or UNDONE, or a custom comment token.

By default, the task window is not visible, so the first thing you must do is make the task list visible. As with most things in Visual Studio, you can achieve this with either a keyboard shortcutt (Ctrl+Alt+K) or by going to View | Other Windows | Task List.
Vs 2005 Comments

As with other Task List entries, you can double-click any comment entry to display the file indicated in the Code Editor and jump to the line of code marked.

Example of TODO Comment:
// TODO: Add references to specific resources here

Caching is a very useful way for you to retain pages or data across HTTP requests, so that you can reuse them without the recreating them.

In 2.0, it makes you easier to implement data caching. SQL cache dependencies are one of 2.0 coolest features, which allows you to create dependencies between cached items and database entities. This enables cached query results to be automatically evicted from the cache if the underlying data changed. This feature applies to SQL Server 7, 2000 and 2005.

SQL cache dependencies are more powerful when paired with SQL Server 2005
– you can set the cache dependencies are more granular

– this is not rely on polling but on query notifications from SQL Server 2005
– this require no preparation of the database

Here are the steps on how to enable the SQL cache dependency on a table.

1. Enable notifications using SQL registration tool
To enable SQL cache dependency, first you have to register the database, tables by using the Sql registration tool (C:\Windows\Microsoft.NET\Framework\v2.0.50727)
At the command prompt, type
aspnet_regsql -S [servername] -E -d [databasename] -ed -et -t [tablename]
-S = Server
-E = TrustedConnection
-d = Database
-ed = Enable caching at database

-et = Enable caching at table
-t = table

other options, -u = username, -p = password

After you run the statement, you hit refresh at your database.
You will notice there is an extra table created to store the cache data and a trigger created for the particular table. When there is changes on the table, the trigger will be fired up and write the changes to the cache table.

2. Register the notification in the Web Configuration
Enable the sql cache dependency at the web configuration file and set the poll time.
sqlCacheDependency enabled=true pollTime=2000>
add name=conn connectionStringName=conn/>

The poll time is specifies how often the application checks to see whether the data has changed.

3. Define SQL Dependency
There are two options to define the SQL dependency
– You can specify on the OutputCache directive
<%@ OutputCache Duration=”10″ VaryByParam=”*”
SqlDependency=”[sqldependencyname]:[tablename]” %>

– or, specify directly on a datasource control
<asp:SqlDataSource EnableCaching=”true” CacheDuration=”Infinite”
SqlCacheDependency=”[sqldependencyname]:[tablename]” />

After you have defined the SQL dependency, any changes that made to the database will be reflected on your web pages immediately though you have set the cache duration on the page.
This can make sure that you users always get the latest data from the server.

4. Notification-based cache invalidation
If you are using SQL Server 2005, you can use the Notification-based cache invalidation.
Unlike polling based validation, no <sqlCacheDependency> needs to be registered in your application’s configuration. Furthermore, no special configuration using the aspnet_regsql.exe tool is needed.

A notification based dependency is configured on the OutputCache directive using the string CommandNotification.
<%@ OutputCache Duration=”10″ VaryByParam=”*” SqlDependency=”CommandNotification %>
This value indicates to ASP.NET that a notification based dependency should be created for the page or datasource control.

System.Data.SqlClient.SqlDependency.Start() method must be called before the first SQL query is executed. This method could be placed in Application_Start() event in global.asax file.

void Application_Start(object sender, EventArgs e)

This option is much more efficient as there is no pollung going on but the acts as client, only if there is changes, will get notified.

You may use SQL profiler to monitor the perfomance of your database to compare before and after you apply this SQL cache dependency.

It is recommended that store your database connection strings in the Web.config file and encrypt the connection strings. In the .NET Framework 2.0, you have the option to enable the configuration encryption in the <protectedData> section in the web.config file.
For example:
<add name=”connectionStrings” provider=”RSAProtectedConfigurationProvider”/>
There are two predefined providers that you can find in the .NET Framework 2.0:
1. DPAPIProtectedConfigurationProvider: It uses the Windows data protection API (DPAPI) to encrypt and decrypt configuration data.
2. RSAProtectedConfigurationProvider: It uses RSA encryption to encrypt and decrypt configuration data

Both the RSA and DPAPI providers are good options for you to encrypt the sensitive data.
However, DPAPI is not recommended to proctect sections in a web farm scenario. This is because the same encrypted web.config file will be deployed to all the machines. What you can do here is to manually encrypt the web.config files on each machine or copy the same container key to the machines.

To create a key container: aspnet_regiis.exe -pc YourContainerName -exp
Export to an XML file: aspnet_regiis.exe -px YourContainerName YourXmlFile.xml
Move the XML file to other machin: aspnet_regiis.exe -pi YourContainerName YourXmFile.xml

Here is an article discussed on the  Encrypting Connection Strings in Web.config.

Subscribe to this blog now!

Top Clicks

  • None
December 2018
« Feb