ASP.NET: Format Phone Numbers using RegEx

Monday, July 13, 2009

If you have a database having phone numbers in various formats, you would want to display them in a consistent format. A solution to display phone numbers in a standard format without worrying about the format in the database is to use regular expressions to format the phone numbers.

For implementing this into ASP.NET pages create a class in the App_Code folder of your web application. Create a static(C#) or shared(VB.NET) method in this class which gets the value of the phone number field as object to handle null values of phone numbers in the database. The code for this function is given below:

// C#
public class CommonFunctions
    public static string FormatPhone(object phoneNo)
        string formattedPhone = phoneNo.ToString;
        // Extracting numbers from the string
        formattedPhone = Regex.Replace(formattedPhone, "[^0-9]", "");
        // The format is in third parameter of the replace function
        formattedPhone = Regex.Replace(formattedPhone, "(\\d{3})(\\d{3})(\\d{4})", "($1) $2-$3");
        return formattedPhone;
Public Class CommonFunctions

Shared Function FormatPhone(ByVal phoneNo As Object) As String
Dim formattedPhone As String = phoneNo.ToString
' Extracting numbers from the string
formattedPhone = Regex.Replace(formattedPhone, "[^0-9]", "")
' The format is in third parameter of the replace function
formattedPhone = Regex.Replace(formattedPhone, "(\d{3})(\d{3})(\d{4})", "($1) $2-$3")
Return formattedPhone
End Function

End Class

The above function first abstracts all the digits from the phone number by using the "[^0-9]" regular expression; all characters which are not numbers are replaced by "". Then the phone numbers are broken into three groups using "(\d{3})(\d{3})(\d{4})" and the groups are then displayed as "($1) $2-$3". Thus, a phone number such as 123.456-7890 will first be converted into 1234567890 and then formatted as (123) 456-7890. You can modify the display format by changing the second replace function.

You can use this method from any ASP.NET data bound field as follows:

<%# CommonFunctions.FormatPhone(Eval("Phone")) %>

Though this example shows the use of this function for ASP.NET pages, it can be used for any .NET based application.

Free Remote Login Tools

Thursday, July 09, 2009

Remote login tools allow you to connect to your computer remotely. You get complete control over your desktop and you can work as if you were working on your own machine. The Windows standard "Remote Desktop Connection" tool is one of the best methods to connect to your computer remotely. But its difficult to use when your computer does not has a static IP address, is connected over a LAN or with a DSL modem which uses NAT.

Here are some tools which I've used for remotely connecting to my computer and should be helpful for others:

Live Mesh:
Live Mesh is one of the best free tools, I've come across. It has great remote desktop capabilities with features similar to the Remote Desktop connection. You would need the Mesh software to be installed on both machine and logged in with a live id. Live Mesh has many more capabilities apart from remote connection. It has a fabulous file synchronization service with 5GB of free storage space. I have been using it for the past 6 months and it has been really superb. Highly recommended if you work at multiple locations. (Also see an earlier post: Mesh up your Data on Live Mesh.)

TeamViewer is a simple tool which allows you to remotely connect to a computer and requires a combination of auto-generated id and passcode. You don't even need to install the tool, you can just run the program by selecting the appropriate option in the setup exe. Anyone having access to the id and passcode can run team viewer and remotely access the computer. This tool is good if you need a temporary access to a computer. I prefer this tool for troubleshooting someone's computer. It gives you the complete desktop access of the machine by sending snapshots of the workspace. It supports Windows and Mac.

These free tools are great alternatives to the paid remote desktop softwares such as GoToMyPC and other VPN based tools. There are many more free tools available, feel free to post a comment about them.

.NET: Convert Image Formats Programmatically

Tuesday, July 07, 2009

Using the .NET framework, you can easily convert images from one format to another with just a few lines of code. The System.Drawing namespace has the functions which help you to load and then save the image in the desired format. The formats supported by the ImageFormat class are: Bmp, Emf, Exif, Gif, Guid, Icon, Jpeg, MemoryBmp, Png, Tiff, Wmf. You can convert an image to any of these formats.

Here is a simple console application which reads all files in a given directory and converts them into .jpg images. It includes the System.IO namespace to read the files in the directory and the System.Drawing class for image functions. You would need to add a reference to System.Drawing class in the Console application. Also, make sure you only have images in the directory or specify a check in the code to read only image files.

// C#
using System.IO;
using System.Drawing;

static class Module1
    public static void Main()
        DirectoryInfo dir = new DirectoryInfo("C:\\something");
        foreach (FileInfo img in dir.GetFiles) {
            Image _image = Image.FromFile("C:\\something\\" + img.Name);
            _image.Save(img.Name, Imaging.ImageFormat.Jpeg);
Imports System.IO
Imports System.Drawing

Module Module1

Sub Main()
Dim dir As New DirectoryInfo("C:\something")

For Each img As FileInfo In dir.GetFiles
Dim _image As Image = Image.FromFile("C:\something\" & img.Name)
_image.Save(img.Name, Imaging.ImageFormat.Jpeg)
End Sub

End Module

The output files would be written into your 'bin' directory if you execute this code from Visual Studio. If you need to change the output path, you can do so by adding a Path to the Save method.