C# and .NET step by step with interview questions
Showing posts with label Validation. Show all posts
Showing posts with label Validation. Show all posts
Monday, May 16, 2011
Friday, April 15, 2011
.NET/ASP.NET Interview Question - Types of Validators in ASP.NET with examples.
Answer:
A validator is a computer program used to check the,validity or syntactical correctness of a fragment of code or text.
For Example:-
Regards,
Please click here to see more .NET/ASP.NET interview questions
A validator is a computer program used to check the,validity or syntactical correctness of a fragment of code or text.
IN ASP.NET there are six different types of Validators.
1. Required Field Validator.
2. Regular Expression Validator.
3. Compare Validator.
4. Range Validator.
5. CustomValidator.
6. Validation Summary
RequiredFieldValidator:-Ensure that the control,has a value to assign in it or user does not skip an entry.3. Compare Validator.
4. Range Validator.
5. CustomValidator.
6. Validation Summary
For Example:-
<asp:Textbox id="txtMobileNumber" runat="server"></asp:Textbox> <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="txtMobileNumber" ErrorMessage="Mobile Number is a required field." ForeColor="Red"> </asp:RequiredFieldValidator>
Regular ExpressionValidator:-Ensure that the ,value of the control matches the expression of validator that is specified. This ,type of validation enables you to check for sequence of character, such as ,e-mail address, telephone number, postal code and so on.
For Example:-<asp:TextBox ID="txtE-Mail" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPassword" ErrorMessage="Please Enter a Valid E-Mail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> // for internet E-Mail. </asp:RegularExpressionValidator>
CompareValidator:-Ensure that the value of one control should be equal to value of another control.which is commonly used in password,less than,greater ,than or equal to.
For Example:-
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox> <asp:TextBox ID="txtConfirmPassword" runat="server"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPassword" ControlToValidate="txtConfirmPassword" ErrorMessage="Password does not match"> </asp:CompareValidator>
RangeValidator:-Ensures that the value of the control is in specified lower and upper boundaries or specified range. You can check ranges within pairs of numbers, alphabetic characters.
For Example:-<asp:TextBox ID="txtAmount" runat="server"></asp:TextBox> <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtPassword" ErrorMessage="RangeValidator" MaximumValue="5000" MinimumValue="100"> </asp:RangeValidator>>
CustomValidator:-This control is used to perform user defined validations.
ValidationSummary:-This validator is used to displays a detailed summary on the validation errors that currently exist.Regards,
Please click here to see more .NET/ASP.NET interview questions
Friday, September 11, 2009
Client side validation using Validation Application blocks
Introduction
By using enterprise validation blocks you can create validation in a dynamic fashion and the validations can be invoked depending on the user logged in. Now let’s understand one of the biggest drawbacks of the validation blocks and let’s see how we can address the same.Please feel free to download my free 500 question and answer videos which covers Design Pattern, UML, Function Points, Enterprise Application Blocks, OOP'S, SDLC, .NET, ASP.NET, SQL Server, WCF, WPF, WWF, SharePoint, LINQ,SilverLight, .NET Best Practices @ these videos http://www.questpond.com
Client side validation using Validation Application blocks
Problem
The biggest drawback with validation application blocks is it does not generate client code. In other way to put is that all validations happen on the server side. From my perspective I think the application block developers have done it right. I mean they have defined a generic server side framework and left the implementation of the client validation to the respective clients like Windows, ASP.NET, etc.Broader Level Solution
So how do we solve the problem? First thing you can Google how much ever you want you will not find an easy way out for this. You need to develop your own adapter. That means you need to develop an adapter which will read the rules from the validation blocks API and create ASP.NET client side validator like required field, not null, etc.If you find a better solution do email me at shiv_koirala@yahoo.com.
The Actual Code
Ok, now we know the problem let’s see how we can solve the same. If you have run through the video given on the top you will understand that all validations are stored in web.config file. So somehow we need to browse through the web.config file to get those validations. Once we get the validation we can then create ASP.NET validators depending on the validations present in web.config file.If you see your web.config which has validators defined using validation application block it would look something as shown below. All validations are enclosed the validation tag. It’s a four step process to browse to get the validation.
- Step 1:- Using the configuration source get the validation settings.
- Step 2:- Using the validation settings get all the ruleset from the validation tag.
- Step 3:- From the rule set get the rule set data collection.
- Step 4:- Browse through the rule set data and generate the corresponding ASP.NET validators.

In the final step we browse through the validator collection and generate the ASP.NET validators. You can use the type name to see which validator type it is. Using the ‘
There are ranges of different validators provided in validation application blocks. Depending on the type you can create the corresponding type of ASP.NET validators at the client side. You can also generate Windows validators accordingly.
RangeValidatorData’ you can get the details of the validator data and you can generate the ASP.NET validators accordingly.
There are ranges of different validators provided in validation application blocks. Depending on the type you can create the corresponding type of ASP.NET validators at the client side. You can also generate Windows validators accordingly.
16 steps to write flexible business validation in C# using Validation Blocks
Introduction
Validation is one of the most important parts in any software project. Building flexible business validation is every one’s dream. Rather than writing frameworks from scratch to do these things, Microsoft validation blocks makes it a breeze. In this article we will discuss how validation application blocks help us to build flexible validations using validation application blocks. Its just a simple sixteen step process to put our business validation in action using validation blocks.Please feel free to download my free 500 question and answer videos which covers Design Pattern, UML, Function Points, Enterprise ApplicationBlocks, OOP'S, SDLC, .NET, ASP.NET, SQL Server, WCF, WPF, WWF, SharePoint, LINQ,SilverLight, .NET Best Practices @ these videos http://www.questpond.com
16 steps to write flexible business validation in C# using Validation Blocks
- The problem
- The solution: - validation application blocks
No business can work without validation. Software’s are made to make business automated so validation forms the core aspect for any software. Almost 80% projects in .NET implement validation as shown in figure below.
Figure: - Validation implemented
Now we can consume the class in the ASPX UI as shown in figure ‘Business object consumed’. We have created the object, set the properties and in case there is error we have caught the same and displayed it in a label using try catch exception blocks.
Figure :- Business object consumedThis implementation looks good to some extent as we have put the business validation in the class and decoupled the UI from any business validation changes.
Let’s list down the common problems associated with the above methodology:-
Tangled validation: - If the validation rules changes we need to compile the whole class. As the entity and the validation layer are entangled in one class, changes in validation leads to compilation of the whole class. So out first goal should be decouple this validation logic from the entity class. When we say entity class we are referring to the ‘ClsCustomer’ class.
Figure :- Tangled validation
Figure :- Decoupling validations from the entity classNo need of compiling: - Validations are volatile entity. They change from time to time. If we can change validation constraints without compiling will lead to better maintenance of the system.
One go validation and results: - The second issue is validations are executed in every set of the property and errors are displayed one by one. It would be great if we can validate all the validation once and display all the errors once.
All these things can be achieved by using Microsoft validation application blocks.
The solution: - validation application blocks
Step 1:- Download the Enterprise library 4.0 from here.Step 2:- Once you install it you should see the same in programs – Microsoft patterns and practices.
Before we move ahead to understand how validation application blocks works. We will first walkthrough fundamentally how validation application blocks decouples the validation from the entity classes. Validation application blocks stands in between the entity class the validation. Validations are stored in configuration files like web.config or app.config depending whether it’s a web application or a windows application. So the validation blocks reads from the configuration file the rules and applies it over the entity class. We can maintain these validations using the enterprise configuration screen provided by Microsoft blocks.
Figure: - Decoupling validations from entity classesNow that we have understood how validation blocks will work. We will run through a example practically to understand the concept.
Step 3:- Once you have installed the block, click on Enterprise library configuration UI to define validation on the configuration file
Figure :- Loading Enterprise configuration screenStep 4:- Click on open and browse to your web.config file and click the open button.
Figure :- Open the web.config fileStep 5:- Once you have opened the web.config file you should see a tree like structure as shown in figure ‘Add validation block’. Right click on the main tree and add the validation application block to your web.config file.

Figure :- Add validation application block
Step 6:- Once you have added the validation application block you should see a validation application block node. Right click on the node , select new and click the type menu.
Figure: - Add the assembly typeStep 7:- Once you have clicked on the type menu you should see a dialog box as shown in figure ‘Load assembly’.
Figure: - Load assembly Step 8:- Click on load assembly and browse to the assembly DLL to which you want to apply validations.
Figure: - Browse the assemblyStep 9 :- Select the class in the assembly as shown in figure select class.
Figure :- Select class
Figure: - Choose membersStep 11:- Once you have clicked the choose members menu you will be popped up with a property selector box as shown in figure ‘Select properties’. In the customer class we have two properties on which we want to apply validations one is the customer name and the other is the customer code. So we will select both the properties and click ok.
Figure: - Choose propertiesStep 12:- Once you have selected the properties you can see both the properties in the UI. To apply validation right click on let’s say customer code property. You will now see list of validations which you can apply on the property. This tutorial will not go in details about all the validations. If you are interested in the same you can read about it more on http://msdn.microsoft.com/en-us/library/cc309336.aspx . For the current scenario we will select range validator.
Figure: - Select range validator
Figure: - Range validator propertiesStep 14 :- In the default rule select the rule which you have just made. If it’s none validations will not fire.
Figure: - Select rule
If you are done just save everything and watch your web.config which is now modified with the validation tags. You will see both the validation i.e. customer name and customer code in XML format inside the <validation> tag. The best part of the config file is now you can change the validation in the config file itself.You guessed right… no recompiling , no installation and completely decoupled.
Figure: - Web.config with validation tagsStep 15:- Ok, now that we are done with putting the validation in the web.config file. Its time to write the code which will read from the config file and apply to our entity class. So the first thing we need to reference the validation application block in our assembly references and include the name spaces in our clsCustomer class.

Figure: - Import the validation namespace
Step 16:- Once you are done with adding the namespaces its time to fire the validation. Below is the code snippet which shows how the validation application block fires the validation using the web.config file. First step create the customer object and set the property. In the second step we tell to the validation static class to validate the object. The validation static class returns validation results. This validation results are nothing but error failures in case the validation fails. In the final step we have looped through the validation results to display the error.
Figure: - The validation code
Ok, now that we are done its time to see the action. You can see in the UI we have just set empty properties and it showed me both the business validations errors from the collection.
Figure :- Validation in action
Subscribe to:
Posts (Atom)

