Monday 29 October 2012

 Click Here to Download ImplementCustomException.zip
You can also called that create user defined exception class in asp.net.
There is a situations where you need to create and handle a custom exception for handling runtime’s exception for application-specific exceptions.
In this example we provide both C#.Net Example and VB.Net Example.

Let's look how this mechanism works.
For that we need to create a serializable class that extends the System.Exception class, and ass some constructors and properties to manipulate the data members.
You can throw custom exception class object exception in Try block and it will handle in catch block. Mark your custom exception class as sealed if you do not wish other exception classes to extend it. You need to Implement three public constructors with specific signature and they call base class constructor.

You also need to make custom exception class serializable so that the runtime can marshal instances of your exception. If your exception class declares custom data members, so you must override the ISerializable. GetObjectData method of the Exception class as well as implement a deserialization constructor with appropriate signature. If your exception class is sealed then mark the deserialization constructor as private, otherwise, mark it as protected. The GetObjectData method and deserialization constructor must call the equivalent base class method to allow the base class to serialize and deserialize its data correctly.

Here is example for this.

Friday 19 October 2012


This is a situations where you have an string array which contains some string element or values and some integer values and we want to parse that integer values from that List or array and retrieve only integer values. We can do with the help of LINQ. We can create an extension method of linq and achieve this.
This LINQ extension method is very useful.

Here is example for this.
In this example we take one List of string object. This List of string object contains both integer and string values.We parse that List of string object and get integer values from that.

In Vb.Net Example we need to add "Module" to create custom LINQ Extension method.

Monday 15 October 2012

Click Here to Download ImplementIComparableInterface.zip

You can compare two object of same class with your predefine rules. You can also say comparison between two objects. 
You can provide compare custom types with your predefined business rules. This also allow to sort collections of instances of those types.
For comparison we implement generic System.IComparable<T> interface. For the comparison of a type based on more than one characteristic, we need to create separate types that implement the generic System.Collections.Generic.IComparer<T> interface.

If you compare two different object of same class with equal (=) operator , at that time this will check only reference location number, this will not check actual fields value property. To avoid this we need to implement IComparable interface and implements it's "CompareTo" Method.

You can call this method like this "X.CompareTo(Y)". X and Y is the object of custom class.
This method return Three possible values.

  • If X value is less than Y value, it will return less than zero (for example, -1).
  • If X value same value as Y value , it will return zero.
  • If X value is greater than Y value, It will return greater than zero (for example, 1).

Here is example for this.

Monday 8 October 2012

We often get requirement to allow only numeric decimal value in textbox for validation purpose, And also we have to specify number of decimal places. We can use ASP.Net RegularExpressionValidator control for this. In this control we can specify regex expression for validation.

The regex expression is "^[+]?[0-9]\d{0,5}(\.\d{1,4})?%?$" .
This regex allow only positive numeric value of maximum six digit and maximum four digit decimal value. The maximum allowed value by this regex is "999999.9999". You can change this accroding to your requirement.

Here is example.

Thursday 4 October 2012

For validation purpose in your project at some places you put text boxes that accept only integer value i.e. 1, 20 , 50, 200 etc... To put validation on that text boxes and allow only integer value you can use ASP.NET Regular Expression Validator Control. In this control we make one regex expression and do validation for that text box.
The regex expression is "^\d+$" .

Here is example.

Monday 1 October 2012

Click Here to Download ImplementICloneableInterface.zip

There is a need where you wan to clone an object for your custom requirement you need to "ICloneable" Interface and Implement Clone() Method, the inbuilt Clone() method does not fulfill custom requirement.
When you assign one value type to another, at that time a copy of the value is created. No link exists between the those two values and changes in one value will not affect the other value.
 However, when we assign one reference type to another (excluding strings), at that time a new copy of the reference type did not created. but , both reference types refer to the same object, and changes into the value of the one object are reflected in other objects.
To create a actual true copy of a reference type object, we must clone the object.
For cloning we are using ICloneable Interface.
We are using "MemberwiseClone" method for shallow copy clone.

Here is example:
In this example we take one "Books" class object and implement "ICloneable"  Interface on that. After that we create one object of "Books" class and set its property after that we create another "Books" object and set our original object by "Clone" Method. You can see in output that If we change in either property of object that object value does not affected earch others.
If you remove the ICloneable Interface on class and directly assign one object to another object and making change in one object's property It will automatically change others property.

C# Examples :
    public class Books : ICloneable
    {
        public string Title { get; set; }
        public string ISBN { get; set; }
        public DateTime ReleaseDate { get; set; }
        public int Pages { get; set; }
        public int PublisherId { get; set; }

        public object Clone()
        {
            return MemberwiseClone();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Books objBook = new Books { Title = "ASP.NET", ISBN = "asp1", ReleaseDate = DateTime.Parse("11/11/2010"), Pages = 200, PublisherId = 1 };

        Books objTmp = (Books) objBook.Clone();
        Response.Write("<b>Original Book Object:</b> ");
        Response.Write("</br>Title : " + objBook.Title);
        Response.Write("</br>ISBN : " + objBook.ISBN);
        Response.Write("</br>ReleaseDate : " + objBook.ReleaseDate);
        Response.Write("</br>Pages : " + objBook.Pages);
        Response.Write("</br></br>");
        ////Make change in copy object
        objTmp.Title = "C#";
        Response.Write("</br><b>After Change In Copy Object Book Object Unaffected:</b>");
        Response.Write("</br>Title : " + objBook.Title);
        Response.Write("</br>ISBN : " + objBook.ISBN);
        Response.Write("</br>ReleaseDate : " + objBook.ReleaseDate);
        Response.Write("</br>Pages : " + objBook.Pages);
        Response.Write("</br></br>");

        objBook.Title = "SQL";

        ////Clone object
        
        Response.Write("</br><b>Clone Object:</b>");
        Response.Write("</br>Title : " + objTmp.Title);
        Response.Write("</br>ISBN : " + objTmp.ISBN);
        Response.Write("</br>ReleaseDate : " + objTmp.ReleaseDate);
        Response.Write("</br>Pages : " + objTmp.Pages);
        Response.Write("</br></br>");

       
    }

VB.net Examples :
    Public Class Books
        Implements ICloneable

        Public Property Title() As String
        Public Property ISBN As String
        Public Property ReleaseDate As Date
        Public Property Pages As Integer
        Public Property PublisherId As Integer

        Public Function Clone() As Object Implements ICloneable.Clone
            Return MemberwiseClone()
        End Function
    End Class

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim objBook As Books = New Books With {.Title = "ASP.NET", .ISBN = "asp1", .ReleaseDate = DateTime.Parse("11/11/2010"), .Pages = 200, .PublisherId = 1}

        Dim objTmp As Books = CType(objBook.Clone(), Books)
        Response.Write("<b>Original Book Object:</b> ")
        Response.Write("</br>Title : " & objBook.Title)
        Response.Write("</br>ISBN : " & objBook.ISBN)
        Response.Write("</br>ReleaseDate : " & objBook.ReleaseDate)
        Response.Write("</br>Pages : " & objBook.Pages)
        Response.Write("</br></br>")
        '//Make change in copy object
        objTmp.Title = "C#"
        Response.Write("</br><b>After Change In Copy Object Book Object Unaffected:</b>")
        Response.Write("</br>Title : " & objBook.Title)
        Response.Write("</br>ISBN : " & objBook.ISBN)
        Response.Write("</br>ReleaseDate : " & objBook.ReleaseDate)
        Response.Write("</br>Pages : " & objBook.Pages)
        Response.Write("</br></br>")

        objBook.Title = "SQL"

        '//Clone object

        Response.Write("</br><b>Clone Object:</b>")
        Response.Write("</br>Title : " & objTmp.Title)
        Response.Write("</br>ISBN : " & objTmp.ISBN)
        Response.Write("</br>ReleaseDate : " & objTmp.ReleaseDate)
        Response.Write("</br>Pages : " & objTmp.Pages)
        Response.Write("</br></br>")

    End Sub

Output :


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.