Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Monday, April 22, 2013

jQuery Disable Cut Copy Paste Options in Textbox


To disable cut, copy and paste functionality for the Textbox we need to write the code like as shown below


<script type="text/javascript">
$(function() {
$('#txtName').bind("cut copy paste"function(e) {
e.preventDefault();
});
});
</script>
If you want to see it in complete example write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Disable Cut, Copy and Paste Options in textbox using jQuery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$('#txtName').bind("cut copy paste"function(e) {
e.preventDefault();
});
});
</script>
</head>
<body>
<div>
<input type="text" id="txtName" />
</div>
</body>
</html>

Friday, April 19, 2013

How to Insert Values into Identity Column


To insert value in identity column I will explain with one example for that first create one sample table like as shown below


CREATE TABLE UserDtls
 (
 UserId int PRIMARY KEY IDENTITY,
 UserName varchar(120),
 Qualification varchar(50)
 )
Once we create UserDtls insert data like as shown below


INSERT INTO UserDtls(UserId,UserName,Qualification) VALUES(1,'Suresh','B.Tech')
Whenever we run above query we will get error message like as shown below


Msg 544, Level 16, State 1, Line 1
Cannot insert explicit value for identity column in table 'UserDtls' when IDENTITY_INSERT is set to OFF.
Based on above error message we can realize that identity columns won’t allow to insert new values whenIDENTITY_INSERT is OFF .To solve this problem we need to set is ON for that we need to write the code like as shown below


SET IDENTITY_INSERT UserDtls ON
INSERT INTO UserDtls(UserId,UserName,Qualification) VALUES(1,'Suresh','B.Tech')
INSERT INTO UserDtls(UserId,UserName,Qualification) VALUES(2,'Rohini','MSC')
SET IDENTITY_INSERT UserDtls OFF
Once we run above query our Output will be like this

---------------------------------------
(1 row(s) affected)

(1 row(s) affected)
In this way we can insert values to identity columns in SQL Server.

Saturday, April 6, 2013

Difference between Virtual and Abstract keywords in .NET

If we feel that the derived class may or may not override the base class method, then you will define the base class method as virtual. Consider the following example:
namespace Application1
{
public class virtualClass
{
public virtual void virtualMethod(){
Console.WriteLine("Virtual Method..");
}
}
public class derivedClass:virtualClass
{
public override void virtualMethod(){
Console.WriteLine("Overridden..");
}
}
public class testClass 
{
public static void Main()
{
virtualClass obj = new virtualClass();
obj.virtualMethod();
derivedClass dObj = new 
derivedClass();
dObj.virtualMethod();
}
}
}
Output of this code will be:
Virtual Method..
Overridden..
But if you want to enforce that derived class must override the base class method then you will define the base class method as abstract. 
namespace Application1 
{
public abstract class abstractClass
{
public abstract void abstractMethod();
}
public class derivedClass:abstractClass
{
public override void abstractMethod()
{
Console.WriteLine("Overridden..");
}
}
public class testClass
{
public static void Main() 
{
derivedClass obj = new 
derivedClass();
obj.abstractMethod();
}
}
}
Output of this code will be:
Overridden..

2.Virtual methods need not be compulsorily overridden. In the above example if the derived class does not override the method virtualMethod, then again the code will work.
Abstract methods should compulsorily be overridden by the derived class. In the above example, if the derivedClass does not override abstractMethod, then during compilation you will get the following error:
'Application1.derivedClass' does not implement inherited abstract member 'Application1.abstractClass.abstractMethod()'.

3.To define a base class method to be virtual, you need not include any new definition for the base class. In the earlier example, you can see that the class virtualClass just includes an access modifier followed by the class name. No other additional modifiers are required.
If you want to define a method as abstract in the base class then the base class should also be marked as abstract. Consider the following example:
namespace Application1 {
public class abstractClass {
public abstract void abstractMethod();
}
}
In this example, abstractClass has an abstract method. Hence the class in itself has to be marked abstract. But it is not done in the above example. Therefore during compilation, you will end up in the following error:
'Application1.abstractClass.abstractMethod()' is abstract but it is contained in nonabstract class 'Application1.abstractClass'

4. Virtual method can have a method body. In the earlier example, virtualMethod of virtualClass has method definition like any of the other methods that you define and it is perfectly legal.
Abstract methods have only the signature. It cannot have method body. However the abstract class can include non-abstract methods and these methods can have method body defined. Consider the following example:
namespace Application1 {
public abstract class abstractClass {
public abstract void abstractMethod(){
Console.WriteLine("Abstract 
method..");
}
}
}
Here you are trying to include method body for an abstract method. This is not permissible. Hence during compilation you will end up in the following error:
"'Application1.abstractClass.abstractMethod()' cannot declare a body because it is marked abstract"

5. Class containing virtual method can be instantiated. Here is an example:
namespace Application1
{
public class virtualClass
{
public virtual void virtualMethod()
{
Console.WriteLine("Virtual Method..");
}
}
Public class testClass
{
public static void Main() 
{
virtualClass obj = new virtualClass();
obj.virtualMethod();
}
}
}
Output of this code will be:
Virtual Method…
Class containing abstract method cannot be instantiated. It can only be inherited. Consider the following example:
namespace Application1
{
public abstract class abstractClass
{
public abstract void abstractMethod();
}
public class testClass {
public static void Main() {
abstractClass obj = new abstractClass();
}
}
}
Here you are trying to create an instance of abstract class. This is not possible. Hence during compilation you will end up in the following error:
"cannot create an instance of the abstract class or interface Application1.abstractClass"

6. Not just methods, virtual modifier can also be used with properties.
Apart from class and method, the modifer abstract can be associated with properties, events and indexers.

7.There is a restriction when using virtual modifer. You cannot use this modifer along with static or abstract or override modifiers.
Abstract modifiers also have a restriction. They cannot be used along with static or virtual or override modifiers.

Thursday, March 21, 2013

ASP.Net Page Life Cycle

The general Page Life Cycle Stages are:
1. Page request - Here Page is requested by the user.
2. Start – Sets the properties such as Request, Response, IsPostBack and UICulture.
3. Page initialization - Controls on the page are available and each control's UniqueID property is set.
4. Load – Controls are loaded here if it is a PostBack request.
5. Validation – Sets the IsValid property.
6. Postback Event handling – Event handlers will be called if it is PostBack.
6. Rendering - the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream of the page's Response property.
7. Unload - Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded.

Common life cycle Events:
1. PreInit – Start event of the Page life cycle and here every page controls are initialized.
2. Init – This is used to read or initialize control properties.
3. InitComplete – Used for processing tasks that require all initialization be complete.
4. PreLoad - This is used before Perform any processing that should occur before Load.
5. Load - Use the OnLoad event method to set properties in controls and establish database connections.
6. Control Event - These are control specific events such as – Button Click, DropDownIndexChanged etc.
7. Load Complete - This event is used for performing those tasks which require load has been completed.
8. PreRender - In this event page insures that all child controls are created.
9. SaveStateComplete - This event occurs after viewstate encoded and saved for the page and for all controls.
10. Render – This is not an event. The page instance calls this method to output the control’s markup, after this event any changes to the page or controls are ignored.
11. Unload – Nothing but cleanup task like closing the files, database connections etc.,

This table shows stages and corresponding Events

Stage Events/Method
Initialization of the page Page_Init
Loading of the View State LoadViewState
processing of the Post back data LoadPostData
Loading of Page Page_Load
Notification of PostBack RaisePostDataChangedEvent
Handling of PostBack Event RaisePostBackEvent
Pre Rendering of Page Page_PreRender
Saving of view state SaveViewState
Rendering of Page Page_Render
Unloading of the Page Page_UnLoad

Monday, March 18, 2013

Seriaization


Object Serialization is a process through which an object's state is transformed into some serial data format, such as XML or binary format, in order to be stored for some later use. In other words, the object is "dehydrated" and put away until we need to use it again.

Some good uses for serialization/deserialization include: 
  1. Storing user preferences in an object.
  2. Maintaining security information across pages and applications.
  3. Modification of XML documents without using the DOM.
  4. Passing an object from on application to another.
  5. Passing an object from one domain to another.
  6. Passing an object through a firewall as an XML string. 

class Person
{
    private String personName;
    private Int32 personAge;

    public String Name
    {
        get
        {
            return personName;
        }
        set
        {
            personName = value;
        }
    }

    public Int32 Age
    {
        get
        {
            return personAge;
        }
        set
        {
            personAge = value;
        }
    }
}
Person oPerson = new Person();
oPerson.Name = "Sandeep Rauniyar";
oPerson.Age = 25;

we wanted to save a copy of this object just as it is at this very moment. We could serialize it as an XML document that would look something like this:

   
     Sandeep Rauniyar


     25