The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:
A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
When a virtual method is invoked, the run-time type of the object is checked for an overriding member in derived classes if not it will use base method.
By default, methods are non-virtual. You cannot override a non-virtual method.
You cannot use the virtual modifier with the static, abstract, private or override modifiers.
It is an error to use the virtual modifier on a static property.
Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax. The main difference between abstract method and virtual method is abstract method doesn't have declaration where as virtual method does.
Here is the example
classFruits
{
publicvirtualvoid Name()
{
// its a generic here it can be any fruit like Apple,Banana,Mango etc.
Console.WriteLine("Iam a fruit");
}
}
classApple : Fruits
{
publicoverridevoid Name()
{
//base.Name(); here using this you can directly use base method
// or you can override it.
Console.WriteLine("Iam a Apple");
}
}
classMango : Fruits
{
publicoverridevoid Name()
{
Console.WriteLine("Iam a Mango");
}
}
classBanana : Fruits
{
publicoverridevoid Name()
{
Console.WriteLine("Iam a Banana");
}
}
Importance of Override Keyword
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.
An override method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method. For information on inheritance, see Inheritance.
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract.
An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier. You cannot use the modifiers new, static, virtual, or abstract to modify an override method. An overriding property declaration must specify the exact same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.
Right click on Solution explorer click on add new item. Here you will find dbml classes add dbml class named as EmployeeDept
Now we need to connect sqlserver through Server Explorer, if you do not find server explorer on left side corner please go to view and select server explorer
right click on dataconnections in Server explorer click on add connection.
you will have a option to choose data source type, for our project we have selected Microsoft SqlServer then click on continue.
Here we need select Server name and Database Name once you have selected Server name and Database Name test connection weather succeed.
Once finished the connection with SqlServer you can get all the tables and Stored procedures.
Just drag and drop Tables and Stored Procedures on the dbml Class Surface.visual studio automatically creates properties for table and methods for stored procedures.
Inserting Record
copy and paste flowing html code in asp page.here i have taken three textboxes, one dropdownlist and one button.