instead of insert trigger in sql server on views


Trigger on view using INSTEAD OF INSERT


To Create INSTEAD OF INSERT triggers in sql server first of all we need to create a table (but we create two table) for which we apply trigger and need another table that hold all activity in main table.

--We Create table tblDeparment table

create table tblDeparment(
Id int identity(1,1) primary key not null,
DeparmentName varchar(30) not null,
Location Char(8) not null,
DeparmentHead varchar(20) not null
)

--insert data's into tblDeparment table

Insert into tblDeparment values('IT','London','Carrie')
Insert into tblDeparment values('HR','Mexico','Toy')
Insert into tblDeparment values('Purchasing','Delhi','Ravi')
Insert into tblDeparment values('Finance','New York','Janie')

--Create another table tblEmployee table

create table tblEmployee(
Id int identity(1,1) primary key not null,
Name varchar(30) not null,
Gender Char(8) not null,
DeparmentId int null constraint FK_tblEmployee_DeparmentId foreign key(DeparmentId) references tblDeparment(Id)
)able

--Insert data into tblEmployee table

insert into tblEmployee values('Jane', 'Female', 1) 
insert into tblEmployee values('Levis', 'Male', 3)
insert into tblEmployee values('Lee', 'Female', Null)
insert into tblEmployee values('Rose', 'Female', 1)

Here we create two table “tblDeparment” and “tblEmployee“ which are inter-connected through Foreign key constraint. We will apply trigger for these tables.

Now create Views.
create View VwTblEmployeeJoinWithtblDepartment
as
select tblEmployee.Id, Name, Gender, DeparmentName
from tblEmployee
join tblDeparment
on tblEmployee.DeparmentId = tblDeparment.Id
Now create table that hold activity of main table by user.
create table tblAudit
(
Id int primary key identity(1,1) not null,
AuditData nvarchar(500)
) 
We have tables and views are given below with data.
All-tables-and-views-for-instead-of-insert-trigger-using-views
All table for INSTEAD OF INSERT using Views

Now Insert data into view "VwTblEmployeeJoinWithtblDepartment", but not updatable because affect multiple base table, so use INSTEAD OF INSERT Trigger.
insert into VwTblEmployeeJoinWithtblDepartment values('Jhon', 'Male', 'IT') 
insert-rows-into-views
insert row into views

Now create sql server audit table trigger on view by INSTEAD OF INSERT Trigger on views for “tblEmployee”.
create trigger trtblEmployeeInsteadOfInsertByView
ON VwTblEmployeeJoinWithtblDepartment
INSTEAD OF INSERT
As
 BEGIN
     declare @DepartId int
     select @DepartId =tblDeparment.Id from tblDeparment
     join inserted
     on inserted.DeparmentName = tblDeparment.DeparmentName    --DeparmentName of inserted(here from view) = DeparmentName of tblDeparment
     
     Declare @EmpId int, @EmpName varchar(30), 
             @EmpGender varchar(8), @EmpDepatmentName varchar(20),
             @EmpMessage varchar(200);
     select  @EmpName = I.Name, 
             @EmpGender= I.Gender, @EmpDepatmentName= I.DeparmentName
             from inserted I              --Here inserted table keep data which we want insert through view.
      
       if(@DepartId is Null)
         Begin
           Raiserror('Invalid Department Id', 16, 1);
           Print 'Invalid Department Id' 
           RollBack
         End   
      Else
         Begin
          If(exists(Select Name from tblEmployee where Name = @EmpName))   --If Alredy Name Exists in Cloumn than through message we can also check by UserName, EmailId etc.
             Begin
              SET @EmpMessage  = 'Employee With Name ' + @EmpName + 'alredy Register with this Name'
              Raiserror(@EmpMessage, 16, 1)  --16 seberaty level(there are several seberaty level) means user can't correct or resubmit the query
              RollBack
             End
           Else
             insert into tblEmployee(Name,Gender,DeparmentId)                   
                               values(@EmpName,@EmpGender,@DepartId)
                               
             /*insert into VwTblEmployeeJoinWithtblDepartment values(8,'Jhon3', 'Male3', 'IT')      
             you cant update using view return error is not updatable because the modification affects multiple base tables*/
             
            /*If we commented Above line and than try to insert data into tblEmployee message return
             row affected but basically does not insert data in tblEmployee because of INSTEAD OF TRIGGER 
             and in tblAudit table NULL value will be insert because there is no last generated SCOPE_IDENTITY().
             So if you want to insert than must write insert querry here.*/                               
             select @EmpId=SCOPE_IDENTITY() from tblEmployee;
             insert into tblAudit values('Employee succefully insert with Id ' + CAST(@EmpId as varchar(5)) + 'in trigger in date' + CAST(Getdate() AS nvarchar(30)))
          End
       End
 END

Now Insert data into view "VwTblEmployeeJoinWithtblDepartment" (which is combination of two table “tblemployee” and “tbldepartmet” which is enter connected with foreign key) with Garbage value for “DepartmentName” which is not in “tblDepartment” table so return a message invalid.
insert into VwTblEmployeeJoinWithtblDepartment values(1,'Jhonny', 'Male', 'GarbageValue') --id=1 insert but it is false because view require id and table “tblEployee” has id is identity. so table automatically set increment id.
Inter-rowin-view-with garbage-value
inter row into views with garbage value

Now insert with correct data into “VwTblEmployeeJoinWithtblDepartment”, which is available in “tblDepartment” table.
insert into VwTblEmployeeJoinWithtblDepartment values(1,'Jhonny', 'Male', 'HR') 
audit-data-in-instead-of-insert-trigger
audit data of Instead of Insert using views

Example 2
For more understand about INSTEAD OF INSERT trigger on view, when multiple base table update so we take another trigger example.

First we create another table that hold all activity of main table done by user.
create table tblAuditDetailsBy
(
  Id int not null Primary key identity(1,1),
  EmpId int Not Null,
  EmpName varchar(30) Not Null,
  EmpGender char(8) Not Null,
  EmpDeparmentId int Not Null,
  AuditAction varchar(1000) Not Null,
  ActionTakenBy varchar(50) Not Null,
  AuditTime datetime not null default Getdate(),
  ServerName varchar(100) Not Null,
  ServerInstanceName varchar(100) Not Null
)

Now create the INSTEAD OF INSERT Trigger on views for “tblEmployee”.
create trigger trtblEmployeeInsteadOfInsertByDetailView
ON VwTblEmployeeJoinWithtblDepartment
INSTEAD OF INSERT
As
 BEGIN
     Declare @DepartId int
     Select @DepartId= tblDeparment.Id from tblDeparment
     join inserted
     on inserted.DeparmentName = tblDeparment.DeparmentName    
     
     Declare @EmpId int, @EmpName varchar(30), 
             @EmpGender varchar(8), @EmpDepatmentName varchar(20),
             @EmpMessage varchar(200),@AuditAction varchar(2000),
             @ActionTakenBy varchar(100),@AuditTime varchar(50),
             @ServerName varchar(100), @ServerInstanceName Varchar(100);
            
     select  @EmpName = I.Name, 
             @EmpGender= I.Gender, @EmpDepatmentName= I.DeparmentName
            from inserted I      --Here inserted table keep data which we want insert through view.
      if(@DepartId is Null)
         Begin
           Raiserror('Invalid Department Id', 16, 1);
           Print 'Invalid Department Id' 
           RollBack
         End      
      Else
        Begin
          If(exists(Select Name from tblEmployee where Name = @EmpName))   --If Alredy Name Exists in Cloumn than through message we can also check by UserName, EmailId etc.
             Begin
              SET @EmpMessage  = 'Employee With Name ' + @EmpName + 'alredy Register with this Name'
              Raiserror(@EmpMessage, 16, 1);
              PRINT 'INSTEAD OF INSERT Triggers in SQL Server not fire because ' + @EmpMessage
              RollBack
             End
          Else
            Begin
             insert into tblEmployee(Name,Gender,DeparmentId)                   
                               values(@EmpName,@EmpGender,@DepartId)
            /*If we commented Above line and than try to insert data into tblEmployee message return
             row affected but basically does not insert data in tblEmployee because of INSTEAD OF TRIGGER 
             and in tblAudit table NULL value will be insert because there is no last generated SCOPE_IDENTITY().
             So if you want to insert than must write iinsert querry here.*/                               
             select @EmpId=SCOPE_IDENTITY() from tblEmployee;
             SET @AuditAction='Insert Record'
             SET @ActionTakenBy= SYSTEM_USER
             SET @ServerName = CAST(SERVERPROPERTY('ServerName') as varchar(100))
             SET @ServerInstanceName = CAST(SERVERPROPERTY('MachineName') as varchar)
             insert into tblAuditDetailsBy(EmpId, EmpName, EmpGender, EmpDeparmentId, AuditAction,
                                           ActionTakenBy, AuditTime, ServerName, ServerInstanceName)
                                    values(@EmpId,@EmpName, @EmpGender, @DepartId, @AuditAction,
                                           @ActionTakenBy, GETDATE(), @ServerName, @ServerInstanceName)
             PRINT 'Successfully Insert Data with ID=' + @EmpId + CAST(Getdate() AS nvarchar(30));
             PRINT 'Fired the INSTEAD OF INSERT Triggers in SQL Server'
            End
         End
 END

Now Insert data into view “VwTblEmployeeJoinWithtblDepartment” with Garbage value for “DepartmentName” which is not in “tblDepartment” table so return an error message.
insert into “VwTblEmployeeJoinWithtblDepartment” values(1,'Jhonnifer', 'Female', 'GarbageValue') --id=1 insert but it is false because view require id and table “tblEployee” has id is identity. so table automatically set increment id.
--If enter already Exists name than through an error message also.
insert-rows-using-garbage-value-in-views
insert data into views with garbage value

Now insert with correct data, which is available in “tblDepartment” table.
insert into VwTblEmployeeJoinWithtblDepartment values(1,'Jhonny', 'Male', 'HR') 
Now check tables and views.
All-tables-and-views-using-instead-of-insert-trigger
Audit Details when insert data in view using Instead of insert trigger


Sql Server Trigger - Instead of insert trigger


To Create INSTEAD OF INSERT Triggers first of all we need to create a table (but we create two table) for which we apply trigger and need another table that hold all activity in main table.

--We cretate tblDeparment table
create table tblDeparment
(
 Id int identity(1,1) primary key not null,
 DeparmentName varchar(30) not null,
 Location Char(8) not null,
 DeparmentHead varchar(20) not null
)

--Insert data into tblDeparment tables

Insert into tblDeparment values('IT','London','Carrie')
Insert into tblDeparment values('HR','Mexico','Toy')
Insert into tblDeparment values('Purchasing','Delhi','Ravi')
Insert into tblDeparment values('Finance','New York','Janie')

--We cretate tblEmployee table

create table tblEmployee(
Id int identity(1,1) primary key not null,
Name varchar(30) not null,
Gender Char(8) not null,
DeparmentId int null constraint FK_tblEmployee_DeparmentId foreign key(DeparmentId) references tblDeparment(Id)
)

--Insert data into tblEmployee tables

insert into tblEmployee values('Jane', 'Female', 1) 
insert into tblEmployee values('Levis', 'Male', 3)
insert into tblEmployee values('Lee', 'Female', Null)
insert into tblEmployee values('Rose', 'Female', 1)

Here we create two table “tblDeparment” and “tblEmployee “ which are inter-connected through Foreign key constraint.
We will apply trigger for these tables.

Example 1

Now create audit table that hold activity of main table by user.
create table tblAudit
(
Id int primary key identity(1,1) not null,
AuditData nvarchar(500)
) 


We have table are given below with data's.
all-selected-tables-for-instead-of-insert-in-sql-server
we have tables for INSTEAD OF INSERT Trigger

Now create sql server audit table trigger for INSTEAD OF INSERT Trigger on “tblEmployee”.
create trigger trtblEmployeeInsteadOfInsert
ON tblEmployee
INSTEAD OF INSERT
As
 BEGIN
     Declare @EmpId int, @EmpName varchar(30), 
             @EmpGender varchar(8), @EmpDepatmentId int,
             @EmpMessage varchar(200);
     select  @EmpName = I.Name, 
             @EmpGender= I.Gender, @EmpDepatmentId= I.DeparmentId
            from inserted I
      
     If(exists(Select Name from tblEmployee where Name = @EmpName))   --If Alredy Name Exists in Cloumn than through message we can also check by UserName, EmailId etc.
       Begin
           SET @EmpMessage  = 'Employee With Name ' + @EmpName + 'alredy Register with this Name'
           Raiserror(@EmpMessage, 16, 1)
           RollBack
       End
       Else
          Begin
             insert into tblEmployee(Name,Gender,DeparmentId)                   
                               values(@EmpName,@EmpGender,@EmpDepatmentId)
            /*If we commented Above line and than try to insert data into tblEmployee message return
             row affected but basically does not insert data in tblEmployee because of INSTEAD OF TRIGGER 
             and in tblAudit table NULL value will be insert because there is no last generated SCOPE_IDENTITY().
             So if you want to insert than must write iinsert querry here.*/                               
             select @EmpId=SCOPE_IDENTITY() from tblEmployee;
             insert into tblAudit values('Employee succefully insert with Id ' + CAST(@EmpId as varchar(5)) + 'in trigger in date' + CAST(Getdate() AS nvarchar(30)))
          End
 END


Now try to insert data into "tblEmployee".
If we try to insert Name which is already exists than return message Employee with name already exits
insert into tblEmployee values('Jane', 'Male', 4) 
Trigger-fire with-error-when-insert-already-exists-row
INSTEAD OF INSERT Trigger fire if already exists row, than return message

Now we try to insert Name which is not already exists return message succefully insert.
insert into tblEmployee values('Jhon', 'Male', 4)
Now check data from tables.
inseted-row-in-audit-table-in-trigger
Row insert into Audit table when fire INSTEAD OF INSERT Trigger
Example 1

For more understand about INSTEAD OF INSERT Trigger we take another ms sql trigger example. First we create another audit table that hold all activity of main table done by user.
create table tblAuditDetailsBy
(
  Id int not null Primary key identity(1,1),
  EmpId int Not Null,
  EmpName varchar(30) Not Null,
  EmpGender char(8) Not Null,
  EmpDeparmentId int Not Null,
  AuditAction varchar(1000) Not Null,
  ActionTakenBy varchar(50) Not Null,
  AuditTime datetime not null default Getdate(),
  ServerName varchar(100) Not Null,
  ServerInstanceName varchar(100) Not Null
)


Now create the INSTEAD OF INSERT Trigger for “tblEmployee”.
create trigger trtblEmployeeInsteadOfInsertByDetail
ON tblEmployee
INSTEAD OF INSERT
As
 BEGIN
     Declare @EmpId int, @EmpName varchar(30), 
             @EmpGender varchar(8), @EmpDepatmentId int,
             @EmpMessage varchar(200),@AuditAction varchar(2000),
             @ActionTakenBy varchar(100),@AuditTime varchar(50),
             @ServerName varchar(100), @ServerInstanceName Varchar(100);
             
     select  @EmpName = I.Name, 
             @EmpGender= I.Gender, @EmpDepatmentId= I.DeparmentId
            from inserted I
      
     If(exists(Select Name from tblEmployee where Name = @EmpName))   --If Alredy Name Exists in Cloumn than through message we can also check by UserName, EmailId etc.
       Begin
           SET @EmpMessage  = 'Employee With Name ' + @EmpName + 'alredy Register with this Name'
           Raiserror(@EmpMessage, 16, 1);
           PRINT 'INSTEAD OF INSERT Triggers in SQL Server not fire because ' + @EmpMessage
           RollBack
       End
       Else
          Begin
             insert into tblEmployee(Name,Gender,DeparmentId)                   
                               values(@EmpName,@EmpGender,@EmpDepatmentId)
            /*If we commented Above line and than try to insert data into tblEmployee message return
             row affected but basically does not insert data in tblEmployee because of INSTEAD OF TRIGGER 
             and in tblAudit table NULL value will be insert because there is no last generated SCOPE_IDENTITY().
             So if you want to insert than must write iinsert querry here.*/                               
             select @EmpId=SCOPE_IDENTITY() from tblEmployee;
             SET @AuditAction='Insert Record'
             SET @ActionTakenBy= SYSTEM_USER
             SET @ServerName = CAST(SERVERPROPERTY('ServerName') as varchar(100))
             SET @ServerInstanceName = CAST(SERVERPROPERTY('MachineName') as varchar)
             insert into tblAuditDetailsBy(EmpId, EmpName, EmpGender, EmpDeparmentId, AuditAction,
                                           ActionTakenBy, AuditTime, ServerName, ServerInstanceName)
                                    values(@EmpId,@EmpName, @EmpGender, @EmpDepatmentId, @AuditAction,
                                           @ActionTakenBy, GETDATE(), @ServerName, @ServerInstanceName)
             PRINT 'Successfully Insert Data with ID=' + CAST(@EmpId as varchar(5)) + CAST(Getdate() AS nvarchar(30));
             PRINT 'Fired the INSTEAD OF INSERT Triggers in SQL Server'
          End
 END


Now try to insert data into "tblEmployee".
Now we try to insert Name which is not already exists return message successfully insert.
insert into tblEmployee values('Jhon', 'Male', 4) 

Instead-of-insert-trigger-fire-when-insert-row
INSTEAD OF INSERT Trigger fire when insert row
Now check data from database triggers in sql tables.

All table when fire instead of insert tables
Insert row into audit table when fire INSTEAD OF INSERT Trigger

AFTER UPDATE Trigger in sql server

AFTER UPDATE Trigger


To Create AFTER Triggers first of all we need to create a table (but we create two table) for which we apply trigger and need another table that hold all activity in main table.

--We create tblDeparment table below

create table tblDeparment
(
Id int identity(1,1) primary key not null,
DeparmentName varchar(30) not null,
Location Char(8) not null,
DeparmentHead varchar(20) not null
)

--We insert following data's into tblDeparment below

Insert into tblDeparment values('IT','London','Carrie')
Insert into tblDeparment values('HR','Mexico','Toy')
Insert into tblDeparment values('Purchasing','Delhi','Ravi')
Insert into tblDeparment values('Finance','New York','Janie')

--We create tblEmployee table below
create table tblEmployee
(
Id int identity(1,1) primary key not null,
Name varchar(30) not null,
Gender Char(8) not null,
DeparmentId int null constraint FK_tblEmployee_DeparmentId foreign key(DeparmentId) references tblDeparment(Id)    --foreign key
)

--We insert following data's into tblEmployee below

insert into tblEmployee values('Jane', 'Female', 1) 
insert into tblEmployee values('Levis', 'Male', 3)
insert into tblEmployee values('Lee', 'Female', Null)
insert into tblEmployee values('Rose', 'Female', 1)


Here we create two table “tblDeparment” and “tblEmployee “ which are inter-connected through Foreign key constraint.
We will apply trigger for these tables.

Now create table that hold activity of main table by user.
create table tblAudit
(
Id int primary key identity(1,1) not null,
AuditData nvarchar(500)
) 
We have tables are given below with datas.
All tables are selected
Available Tables are selected

Now create the AFTER UPDATE Trigger for “tblEmployee”.
Create trigger trtblEmployeeForUpdate
on tblEmployee                                   
For UPDATE                                        --for Update trigger                                   
As
Begin
    Declare @Id int                                          --Id not change because of primary key
    Select @Id = Id from inserted                            --inserted table use by SQL Server to keep copy of Row which just insert into actual table, the structure of the inserted table is identical to the structure of the actual table
    insert into tblAudit values('An Exiting Employee With Id = ' + cast(@Id as varchar(10)) + 'is Update at' + cast(GETDATE() as nvarchar(20))) --Foreign Key

--CAST is used to convert int type Id to nvarchar that concatenate with "New Employee with Id"
End
Now try to Update data from “tblEmployee” than "trtblEmployeeForDelete" trigger automatically fire and data insert into "tblAudit" and also show in message for two row affected.
delete from tblEmployee where Id=4
Update the table in trigger
After Update trigger update the table
 Now check data from tables.
Tables after update in after update trigger
Select table After update trigger

Example 2

For more understand about AFTER DELETE Trigger we write above Query by different way when more than one update Query execute.
create trigger trtblEmployeeForUpdateByComprision
On tblEmployee
For Update                          --for Update trigger
As
Begin
    Declare @Id int                 --Declare variable Id Not Change because it is Primary key
    Declare @Old_Name nvarchar(30)
    Declare @Old_Gender nvarchar(8)
    declare @Old_DepartmentId int
    
    Declare @New_Name nvarchar(30)
    Declare @New_Gender nvarchar(8)
    declare @New_DepartmentId int
    
    declare @AuditDetails nvarchar(2000)
    
    Select * into #tblTemptblEmployee from inserted               --select data from inserted table (New data which want to update) and insert into temporary table "#tblTemptblEmployee"
    
    while(Exists (Select Id from #tblTemptblEmployee))            --check condition, Id is available or not for which we want to update
    Begin
        Set @AuditDetails = ''
        
        Select Top 1 @Id = Id,                          -- if there are many row in temporary table for updatation, than select top 1 row by id and put into @Id variable
               @New_Name =Name, @New_Gender = Gender,   --since inserted table contain new data, so take data from trigger inserted table(which are transfer into temporary table) and put int @New_Name variable, similarly all
               @New_DepartmentId = DeparmentId
        From #tblTemptblEmployee                        -- select from from temporary table new value and assign to variables
        
        Select @Old_Name = Name, @Old_Gender = Gender,
               @Old_DepartmentId = DeparmentId
        From deleted Where Id= @Id                       --select from deleted table
        
        Set @AuditDetails= 'Employee With Id = ' + Cast(@Id as nvarchar(5)) + 'Changed '        --CAST is used to convert int type Id to nvarchar that concatenate with "New Employee with Id"
        
        If(@Old_Name <> @New_Name)                                                              --check old name not equal to new name
        Set @AuditDetails= @AuditDetails + 'Name From' + @Old_Name + 'To ' + @New_Name          
        
        If(@Old_Gender <> @New_Gender)
        set @AuditDetails = @AuditDetails + ' New Gender ' + @Old_Gender + 'To ' + @New_Gender
        
        If(@Old_DepartmentId <> @New_DepartmentId)
        set @AuditDetails = @AuditDetails + ' New DeparmentId ' + Cast(@Old_DepartmentId as nvarchar(5)) + 'To ' + Cast(@New_DepartmentId as nvarchar(5)) 
        
        insert Into tblAudit values(@AuditDetails)
        delete from #tblTemptblEmployee where Id = @Id         --delete the data from temporary table otherwise it become infinite loop
    End
End 

Now try to Update data from “tblEmployee” than "trtblEmployeeForUpdateByComprision" trigger automatically fire and data insert into "tblAudit" and also show in message for no of row affected.
Update tblEmployee set Name='Rosery' where id=2
After updating check data from tables
selected tables after update the trigger
Select tables after update the trigger

Now Update two row simultaneously.
update tblEmployee set Name='Rose Dosan1', Gender='Male' where id =2
update tblEmployee set Name='Rose Dosan4', Gender='Female' where id =4
After updating check data again from tables
Select tables after update trigger
After Update trigger in Sql Server

Now Update By IN
update tblEmployee set DeparmentId=4 where id IN(1,3)
Now check data from tables.
Select table after update
Select Table After update
Example 3

For more understand about AFTER Update Trigger we take another trigger example.
First we create another table that hold all activity of main table done by user.

create table tblAuditDetailsBy
(
  Id int not null Primary key identity(1,1),
  EmpId int Not Null,
  EmpOldName varchar(30) Not Null,
  EmpNewName varchar(30) Not Null,
  EmpOldGender char(8) Not Null,
  EmpNewGender char(8) Not Null,
  EmpOldDeparmentId int Not Null,
  EmpNewDeparmentId int Not Null,
  AuditAction varchar(1000) Not Null,
  ActionTakenBy varchar(50) Not Null,
  AuditTime datetime not null default Getdate(),
  ServerName varchar(100) Not Null,
  ServerInstanceName varchar(100) Not Null
)

Now create AFTER UPDATE Trigger.
CREATE TRIGGER trtblEmployeeForAfterUpdate
On tblEmployee
AFTER UPDATE                               -- We can use AFTER instead of FOR, in above Query we use FOR
AS
 Begin
 declare @EmpId int;            --Declare variable Id Not Change because it is Primary key
 
 declare @EmpOldName varchar(30);
 declare @EmpOldGender varchar(8);
 declare @EmpOldDepartmentId int;
 
 declare @EmpNewName varchar(30);
 declare @EmpNewGender varchar(8);
 declare @EmpNewDepartmentId int;
 
 declare @EmpAuditAction varchar(1000);
 declare @ActionTakenBy varchar(100);
    declare @ServerName varchar(100);
    declare @ServerInstanceName varchar(100);
    
    set @EmpAuditAction='Update Record ';
 select @EmpId=I.Id from inserted I;         --select value from inserted table(update ) and assign to @EmpId
  
 select @EmpNewName=I.Name,               --select value from inserted table(which we want to update) and assign to @EmpName
        @EmpNewGender=I.Gender,
        @EmpNewDepartmentId=I.DeparmentId from inserted I; 
 
 select @EmpOldName=D.Name,                    --select value from deleted table(old value) and assign to @EmpId
        @EmpOldGender=D.Gender,
        @EmpOldDepartmentId=D.DeparmentId from deleted D;
 
 If(@EmpNewName <> @EmpOldName)    -- We can use also If Update(Name), Name is tblEmployee ColumnName
 Begin  
   set @EmpAuditAction = @EmpAuditAction + ' Name from ' + @EmpOldName + ' to ' + @EmpNewName + ', ' 
 End
 If(@EmpNewGender <> @EmpOldGender)   
 set @EmpAuditAction = @EmpAuditAction + ' Gender from ' + @EmpOldGender + 'to ' + @EmpNewGender + ', '
 If(@EmpNewDepartmentId <> @EmpOldDepartmentId)   
 set @EmpAuditAction = @EmpAuditAction + ' Department Id from ' + CAST(@EmpOldDepartmentId as varchar(5)) + ' to ' + CAST(@EmpNewDepartmentId as varchar(5))
 
 
 set @ActionTakenBy = SYSTEM_USER;
 set @ServerName = CAST( SERVERPROPERTY('ServerName') AS VARCHAR(50));          -- select @@SERVERNAME return server name
 set @ServerInstanceName = CAST( SERVERPROPERTY('MachineName') AS VARCHAR(50));    --SELECT @@servicename return current machine name
 
 insert into tblAuditDetailsBy (EmpId, EmpOldName, EmpNewName, EmpOldGender, EmpNewGender,
                                 EmpOldDeparmentId, EmpNewDeparmentId, AuditAction,
                                ActionTakenBy, AuditTime,ServerName,ServerInstanceName)
                         values(@EmpId,@EmpOldName, @EmpNewName, @EmpOldGender, @EmpNewGender,
                                @EmpOldDepartmentId, @EmpNewDepartmentId, @EmpAuditAction,
                                @ActionTakenBy,GETDATE(), @ServerName, @ServerInstanceName)

 PRINT 'Successfully Fired the AFTER Update Triggers in SQL Server'
End

Now try to update data from "tblEmployee" than "trtblEmployeeForAfterUpdate" trigger automatically fire and data insert into "tblAuditDetailsBy" and also show in message row affected.
update tblEmployee set Name='John', Gender='Male' where id =1
update tblEmployee set Name='Leeky', Gender='Female', DeparmentId=2 where id =2
update the trigger
Update after update trigger
Now Check data from tables.
select table after update table
Select table aftre update trigger fire

AFTER DELETE Trigger in SQL Server with examples


AFTER DELETE Trigger


To Create AFTER Triggers first of all we need to create a table (but we create two table) for which we apply trigger and need another table that hold all activity in main table.
--We Create below tbldepartment Table

create table tblDeparment
(
 Id int identity(1,1) primary key not null,
 DeparmentName varchar(30) not null,
 Location Char(8) not null,
 DeparmentHead varchar(20) not null
)

--We Insert followings datas in tblDepartment

Insert into tblDeparment values('IT','London','Carrie')
Insert into tblDeparment values('HR','Mexico','Toy')
Insert into tblDeparment values('Purchasing','Delhi','Ravi')
Insert into tblDeparment values('Finance','New York','Janie')

--We create another table tblEmployee

create table tblEmployee
(
 Id int identity(1,1) primary key not null,
 Name varchar(30) not null,
 Gender Char(8) not null,
 DeparmentId int null constraint FK_tblEmployee_DeparmentId foreign key(DeparmentId) references tblDeparment(Id)
)

--insert data into table tblEmployee
insert into tblEmployee values('Jane', 'Female', 1) 
insert into tblEmployee values('Levis', 'Male', 3)
insert into tblEmployee values('Lee', 'Female', Null)
insert into tblEmployee values('Rose', 'Female', 1)
Here we create two table “tblDeparment” and “tblEmployee“  which are inter-connected through Foreign key constraint.
Now we will apply trigger for these tables.

Example 1
First of all we need to create table that hold activity in main table by user.

create table tblAudit
(
Id int primary key identity(1,1) not null,
AuditData nvarchar(500)     --keep all activity in his cloumn
) 
Now we have tables are given below with data.

select All tables tblDeparment tblEmployee tblAudit

Now create the AFTER DELETE Trigger for “tblEmployee”.
Create trigger trtblEmployeeForDelete
on tblEmployee
For Delete
As
Begin
    Declare @Id int
    Select @Id = Id from deleted         --Deleted table use by SQL Server to keep copy of Row which just delete from actual table, the structure of the deleted table is identical to the structure of the actual table
    insert into tblAudit values('An Exiting Employee With Id = ' + cast(@Id as varchar(10)) + 'is Deleted at' + cast(GETDATE() as nvarchar(20)))

--CAST is used to convert int type Id to nvarchar that concatenate with "New Employee with Id"
End


Now try to delete data from “tblEmployee” than "trtblEmployeeForDelete" trigger automatically fire and data insert into "tblAudit" and also show in message for affected rows.
delete from tblEmployee where Id=4

Delete row from tblEmployee

Now check data from tables.
Show all tables

Example 2
For more understand about AFTER DELETE Trigger we take another trigger example.
First we create another table that hold all activity of main table done by user.
Create table tblAuditDetailsBy
(
  Id int not null Primary key identity(1,1),
  EmpId int Not Null,
  EmpName varchar(30) Not Null,
  Gender char(8) Not Null,
  DeparmentId int Not Null,
  AuditAction varchar(30) Not Null,
  ActionTakenBy varchar(50) Not Null,
  AuditTime datetime not null default Getdate(),
  ServerName varchar(100) Not Null,
  ServerInstanceName varchar(100) Not Null
)

Now create AFTER DELETE Trigger.
CREATE TRIGGER trtblEmployeeForAfterDelete
On tblEmployee
AFTER DELETE                 -- We can use AFTER instead of FOR, in above Query we use FOR
AS
 Begin
 declare @EmpId int;        --Declare variables
 declare @EmpName varchar(30);
 declare @EmpGender varchar(8);
 declare @EmpDepartmentId int;
 declare @EmpAuditAction varchar(100);
 declare @ActionTakenBy varchar(100)
 declare @ServerName varchar(100);
 declare @ServerInstanceName varchar(100);
    
 select @EmpId=D.Id from deleted D;     --Select Id from deleted and assign to @EmpId. (D is alias name for deleted table)
 select @EmpName=D.Name from deleted D; 
 select @EmpGender=D.Gender from deleted D;
 select @EmpDepartmentId=D.DeparmentId from deleted D; 
 set @EmpAuditAction='Deleted Record ';
 set @ActionTakenBy = SYSTEM_USER;
 set @ServerName = CAST( SERVERPROPERTY('ServerName') AS VARCHAR(50));          -- select @@SERVERNAME, return server name
 set @ServerInstanceName = CAST( SERVERPROPERTY('MachineName') AS VARCHAR(50));    --SELECT @@servicename, return current machine name
 
 insert into tblAuditDetailsBy (EmpId, EmpName, Gender, DeparmentId, AuditAction,
                                ActionTakenBy, AuditTime,ServerName,ServerInstanceName)
                         values(@EmpId,@EmpName,@EmpGender,@EmpDepartmentId,@EmpAuditAction,
                                @ActionTakenBy,GETDATE(), @ServerName, @ServerInstanceName)

 PRINT 'Successfully Fired the AFTER DELETE Triggers in SQL Server'
End

Now try to delete data from "tblEmployee" than "trtblEmployeeForAfterInsert" trigger automatically fire and data insert into "tblAuditDetailsBy" and also show in message for two row affected.
delete from tblEmployee where Id=2


Now Check data's from tables

AFTER INSERT Trigger in SQL Server with examples


AFTER INSERT Trigger

AFTER INSERT Trigger fire after the triggering action Insert but before competition to the databases. AFTER Insert Trigger has the ability that it can rollback modification action or statements.

To Create AFTER INSERT Trigger first of all we need to create a table (but we create two tables) for which we apply Trigger and need another table that hold all activity in main table.

--We create below tblDeparment table

create table tblDeparment
(
 Id int identity(1,1) primary key not null,
 DeparmentName varchar(30) not null,
 Location Char(8) not null,
 DeparmentHead varchar(20) not null
)

--We insert following data into tblDeparment table below

Insert into tblDeparment values('IT','London','Carrie')
Insert into tblDeparment values('HR','Mexico','Toy')
Insert into tblDeparment values('Purchasing','Delhi','Ravi')
Insert into tblDeparment values('Finance','New York','Janie')

--We create another table tblEmployee 

create table tblEmployee
(
 Id int identity(1,1) primary key not null,
 Name varchar(30) not null,
 Gender Char(8) not null,
 DeparmentId int null constraint FK_tblEmployee_DeparmentId foreign key(DeparmentId) references tblDeparment(Id)   --foreign key 
)

--We insert following data into tblEmployee table below

insert into tblEmployee values('Jane', 'Female', 1) 
insert into tblEmployee values('Levis', 'Male', 3)
insert into tblEmployee values('Lee', 'Female', Null)
insert into tblEmployee values('Rose', 'Female', 1)

Here we create two table "tblDeparment" and "tblEmployee" which are inter-connected through Foreign key constraint.
We will apply trigger for these tables.

Inserted and deleted

Before understand trigger we need to understand "inserted" and "deleted" tables which are the special type of the tables use by the trigger and these tables only available in the context of the trigger.

Whenever insert or delete data into or from the table SQL Server manage these tables which retain copy of data which you insert or delete into or from the table.

So when insert or delete data into or from any table SQL Server behind the seen create a table called "inserted" and "deleted" in memory and copy of the row mainted in the inserted and deleted table, and this inserted table can be access inside the context of the trigger.

So structure of "inserted" and "deleted" table identical to the structure or the main table.
If you try to access outside the trigger it will show an error.
--this Trigger only for understand inserted and deleted tables, how usefull for trigger
Create trigger trtblEmployee_ForUnderstand_Inserted_DeletedTable    --"trtblEmployee_ForUnderstand_Inserted_DeletedTable" is a trigger name, tr is use before trigger name for name convention
on tblEmployee                    --create trigger on "tblEmployee"
For update                        --create trigger for update activity
as 
Begin
Select * from inserted            --retain copy of data which you insert into the "tblEmployee".
select * from deleted             --retain copy of data which you delete from the "tblEmployee".          
End


Now create table that hold activity of main table by user.
create table tblAudit
(
Id int primary key identity(1,1) not null,
AuditData nvarchar(500)
) 

We have table are given below with data.
table of tblEmployee tblDeparment and tblAudit

Now create the AFTER INSERT Trigger for "tblEmployee".
Create trigger trtblEmployeeForInsert
On tblEmployee
For Insert
As 
Begin
Declare @Id int                         --Declare variable
Select @Id=Id from inserted             --select Id from Inserted table which copy of actual insert data of actaual table and pass to @Id
Insert into tblAudit values('New Employee with Id=' + CAST(@Id as nvarchar(10))+ 'is added at='+ CAST(GETDATE() as nvarchar(20)) )   --insert into tblAudit 

--CAST is used to convert int type Id to nvarchar that concatenate with "New Employee with Id"
End

Now insert data into “tblEmployee” than "trtblEmployeeForInsert" trigger automatically fire and data insert into "tblAudit" and also show in message for two row affected.
insert into tblEmployee values('Collie', 'Male', 2)
insert data into tblEmployee

Now Check data from tables.
data of tbleEmployee tblDepartment and tblAudit

Above Query save data in "trtblEmployeeForInsert" trigger with id and date.


EXAMPLE 2
For more understand about AFTER INSERT Trigger we create take another trigger example.
First we create another table that hold all activity of main table done by user
Create table tblAuditDetailsBy
(
  Id int not null Primary key identity(1,1),
  EmpId int Not Null,
  EmpName varchar(30) Not Null,
  Gender char(8) Not Null,
  DeparmentId int Not Null,
  AuditAction varchar(30) Not Null,
  ActionTakenBy varchar(50) Not Null,
  AuditTime datetime not null default Getdate(),
  ServerName varchar(100) Not Null,
  ServerInstanceName varchar(100) Not Null
)
Now create AFTER INSERT trigger
CREATE TRIGGER trtblEmployeeForAfterInsert
On tblEmployee
AFTER INSERT                 -- We can use AFTER instead of FOR, in above Query we use FOR
AS
 Begin
 declare @EmpId int;
 declare @EmpName varchar(30);
 declare @EmpGender varchar(8);
 declare @EmpDepartmentId int;
 declare @EmpAuditAction varchar(100);
 declare @ActionTakenBy varchar(100);
        declare @ServerName varchar(100);
        declare @ServerInstanceName varchar(100);
    
 select @EmpId=I.Id from inserted I; 
 select @EmpName=I.Name from inserted I; 
 select @EmpGender=I.Gender from inserted I;
 select @EmpDepartmentId=I.DeparmentId from inserted I; 
 set @EmpAuditAction='Inserted Record ';
 set @ActionTakenBy = SYSTEM_USER;
 set @ServerName = CAST( SERVERPROPERTY('ServerName') AS VARCHAR(50));          -- select @@SERVERNAME return server name
 set @ServerInstanceName = CAST( SERVERPROPERTY('MachineName') AS VARCHAR(50));    --SELECT @@servicename return current machine name
 
 insert into tblAuditDetailsBy (EmpId, EmpName, Gender, DeparmentId, AuditAction,
                                ActionTakenBy, AuditTime,ServerName,ServerInstanceName)
                         values(@EmpId,@EmpName,@EmpGender,@EmpDepartmentId,@EmpAuditAction,
                                @ActionTakenBy,GETDATE(), @ServerName, @ServerInstanceName)

 PRINT 'Successfully Fired the AFTER INSERT Triggers in SQL Server'
End
Now insert data into “tblEmployee” than "trtblEmployeeForAfterInsert" trigger automatically fire and data insert into "tblAuditDetailsBy" and also show in message for two row affected.
insert into tblEmployee values('Janiffer', 'Female', 3)
insert data into tblEmployee

Now check data from tables
select data of trigger

Triggers and Types in sql server


What is trigger

Triggers is a special type of database operation (In many way it is similar to a stored procedure) which execute automatically when perform DML (INSERT, UPDATE, DELETE) operation on the table or views or perform DDL (CREATE, AKTER, DROP) operation on Table, Function, Index, Store Procedure or a database operation (LOGON, LOGOFF).
Each table or views has its own triggers and you can't execute trigger explicitly.

Type of Trigger in sql


AFTER Triggers (For Triggers)

AFTER Triggers fire after the triggering action (Insert, Update, delete) but before the statement's competition to the databases. AFTER triggers has the ability that it can rollback modification action or statements.
e.g. If we insert a row into a table and a trigger associated with that insert event on the table will fire only after the row passes perfectly (with primary key, constraints etc.). If insertion fails due to any reason, then SQL Server will not fire the AFTER Trigger.

Note:-
1. AFTER trigger can only use with table not views.
2. A single AFTER trigger can use with single table.
3. The text, ntext, and image columns cannot be referenced in the AFTER trigger logic.

INSTEAD OF Triggers

INSTEAD OF Triggers fire instead of any of the Insert, Update or Delete operations i.e this trigger fire before SQL Server starts the execution of the action. If We have an INSTEAD OF insert/update/delete trigger on a table that successfully executed but does not include the actual insert/update/delet to the table.
e.g. If we apply an INSTEAD OF trigger on a table for the delete operation, and we try to delete rows from the table, they will not actually get deleted.

Note:-
1. We can define an INSTEAD OF trigger on a view. When multiple base table need to update than use view in trigger.
2. INSTEAD OF trigger replace the actions of the original data modification that fired the trigger.
3. Constraint(Foreign key, check, unique, primary) happens after the INSTEAD OF trigger fires.

DDL Trigger

DDL Trigger fire in response to DDL Events (when Create, Alter, Drop (Table, Function, Index, Store Procedure)) then corresponding event fired.
e.g. when you create a table using create DDL statement, associate event is "create_table", that event is raised. And you have trigger that associate with that event than, when you create a table that associate table is fired.
Similarly when drop a store procedure than "Drop_storeprocedure" is fire, when create a function DDL function event is raised.

DDL Triggers can be DATABASE scoped (CREATE TABLE, CREATE PROCEDURE, CREATE FUNCTION, ALTER TABLE, ALTER PROCEDURE, ALTER FUNCTION etc.) or SERVER scoped (CREATE DATABASE, CREATE LOGIN, GRANT_SERVER, ALTER DATABASE, ALTER LOGIN etc.).

Use of DDL Trigger

If you want to execute some code in response to a specific DDL Event To prevent certain changes to your database schema. Audit the changes that the users are making to the database structure.

Note:-
1. We can use only FOR/AFTER clause in DDL Triggers not INSTEAD OF clause.
2. List of all list of DDL events

LOGON Trigger

Logon Trigger fire when login event occurs. LOGON Trigger fire after authentication phase of logging finishes, but before the user session is established. This Trigger control server sessions such as
1. Tracking login activity.
2. Restricting Login to SQL Server.
3. Limiting number of session for specific login.

CLR Trigger

CLR Trigger allows for the database objects (such as trigger both DDL and DML) to be coded in .NET using c#, VB. CLR Trigger is based on CLR. CLR introduce with SQL Server 2008

Differences between C++ and C# with examples


Main method

In C++, The first character of the main() function must be small
e.g.
   int main () 
{
   int a = 100;
   int b = 200;
}
In C#, The first character of the Main() function must be capitalized and should return either int or void
e.g. 
static int Main(string[] args)
{
  int a = 110;
   int b = 510;
}

Environment

C++ was designed to be a low-level platform-neutral object-oriented programming language.
C# was designed to be a higher-level component-oriented language.

Namespace/HeaderFile

C++ support #include
C# does not support #include, it support using (not same as #include, both are different)

Compilation

C++ code compiles into assembly language.
C# compiles into Intermediate language (IL) which converted into executable code through the process called Just-In-Time compilation.

Faster

C++ codes not compiled into a bytecode, they generate machine code, so its faster.
C# programs are compiled down to a bytecode which causes a JIT to occur when the programs are executed which takes time i.e it is slow.

Pointer/delegates

C++ has the concept of function pointers.
void main () {
   int  value = 2;   // actual variable.
   int  *p;        // pointer variable 
   p = &value;       // store address of value in pointer variable
   cout << "Value of value variable: ";
   cout << value << endl;
   cout << "Address stored in p pointer variable: ";
   cout << p << endl;
   cout << "Value of *p variable: ";
   cout << *p << endl;
}
C# does not have the concept of function pointers. C# has a similar concept called Delegates.
public delegate int DE(int a);
public class C1
{
  public int p(int x)
  {
    return x;
  }
}
class Program
{
static void Main()
{
 C1 c11 = new C1();
 DE d1 = c1.p;
 int i = d1(10);
 Console.WriteLine(i);
  }
}
 

Variable

In C++, uninitialized variables undetected thus result is unpredictable output.
C# checks for uninitialized variables and gives error message at compile time.

Memory manegement:-

In C++, the memory that is allocated in the heap dynamically has to be explicitly deleted.
In C#, memory management is automatically handled by garbage collector.

Exception:-

In C++, The exception can throw by any class.
in C#, The exception can only throw by a class that is derived from the System.Exception class.

Finally Block

C++ does not have finally block exception handling mechanism.
e.g
try 
{
  // code here
}
catch (int ex1) 
{ 
cout << "int exception"; 
}
catch (char ex2) 
{ 
cout << "char exception"; 
}
catch (...) 
{ 
cout << "default exception"; 
}
C# has finally block in exception handling mechanism which contains code that is always executed at the end of try block.
e.g
try 
{
  // code here
}
catch (ApiConnectionSecurityException ex1) 
{ 
Console.WriteLine("Exception caught: {0}", ex1) //using console application
}
catch DivideByZeroException ex2) 
{ 
lblMsg.Text= ex2.Message;
}
catch (Exception ex3) 
{ 
lblMsg.Text= ex2.Message;
}
Note:- The finally block is executed as soon as control leaves a catch or try block, and typically contains cleanup code for resources allocated in the try block, C# allow order of catch blocks correctly.

Access modifiers

in C++ supports three access modifiers public, private, protected.
in C# supports five access modifiers public, private, protected, internal and protected internal.

By Class

In C++, the end of the class definition has a closing brace followed by a semicolon.
class ClassCplus
    {
        static void Main()
        {
           
        }
    };      // close with semicolon
In C#, the end of the class definition has a closing brace alone.
class ClassCsharp
    {
        static void Main()
        {
           
        }
    }      // not close with semicolon


By Struct

C++ struts behave like classes except that the default access is public instead of private.
C# struts can contain only value types The struts is sealed and it cannot have a default no-argument constructor.

By Foreach

C++ does not contain foreach statement.
C# supports foreach statement.

class ForEachSample
    {
        static void Main()
        {
            int[] ListOfArrays = new int[] { 0, 1, 23, 30, 4, 33, 54, 71, 8, 19 };
            foreach (int num in ListOfArrays)
            {
                System.Console.WriteLine(num);
            }
            System.Console.ReadLine();
        }
    }

By Switch

When break statement is not present after statement in switch case than fall through go to next case (in both case code present or not in case statement).
int main () {
   char Security= 'Z';

   switch(Security) {
      case 'A' :
         cout << "For P.M." << endl; 
         break;               
      case 'C' :                //break statement is not present, switch case than fall through go to next case
      case 'D' :               
         cout << "For D.M" << endl;
         break;
      case 'Z' :
         cout << "For President"<< endl;
         break;
      default :
        cout << "Invalid Security" << endl;
   }
   cout << "Your Security is " << Security << endl;
   return 0;
}
In C++ Switch Statement, the test variable cannot be a string.
int main()
{
  switch(std::string("MaaShardaTechnology")) //Compilation error - switch expression of type illegal 
  {
   case "Opt":
  }
}
//Compilation error Because  C/C++ doesn't really support strings as a type.
//It does support the idea of a constant char array but it doesn't really fully understand the notion of a string.
When break statement is not present after statement in switch case than fall through not go to next case (in both case code present or not in case statement).
class Program
   {
      static void Main()
      {
         string Security= 'Z-Type';
         switch(Security) 
          {
             case 'A_Type' :
                   Console.WriteLine(For P.M.);
                   break;               
             case 'C_Type' :                //break statement is not present, switch case than not go to next case
             case 'D_Type' :               
                   Console.WriteLine(For D.M);
                   break;
             case 'Z_Type' :
                   Console.WriteLine(For President);
                   break;
             default :
                   Console.WriteLine(Invalid Security);
     }
              Console.WriteLine(Your Security is {0}, Security);
              Console.ReadLine();
          }
   }
In C# Switch Statement, the test variable can be a string. above example

By Inheritance

C++ supports multiple inheritance.
C# does not supports multiple inheritance.

By Macros

C++ supports macros.
#include 

#define MAX(X,Y) (((X)>(Y)) ? X : Y)

int main () {
   int i, j;
   i = 510;
   j = 205;
   cout <<"The Maximum value is " << MAX(i, j) << endl;
   return 0;
}
But, C# does not supports macros because One of our main design goals for C# is to keep the code very readable

By Type-Safe

C++ is not type-safe.
C# is language type-safe.

By Arrays

C++ arrays are of value type.
C# arrays are of reference type. The tokens "[]" appear following the array type in C#.

By bit Field

C++ supports bit fields.
#include 
#include 

struct {
   unsigned int i;
   unsigned int j;
} s1;

struct {
   unsigned int i: 1;
   unsigned int j: 1;
} s2;
 
int main( ) {

   printf( "Memory size occupied by s1 : %d\n", sizeof(s1));
   printf( "Memory size occupied by s2 : %d\n", sizeof(s2));
   return 0;
}
But, C# does not supports bit fields.

By Data Type

long data type is 32 bits in c++.
Type Bit Repersent Range
long 8bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
long data type is 64 bits in c#.
Type Bit Repersent Range
long 64-bit signed integer type -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807



What is C# - and its features


What is c# (C Sharp)


C# is new computer programming language which is simple because it based on C++ and Java, which is developed by Microsoft Corporation and submitted to the ECMA for standardization and announced to the public in June 2000 with the introduction of .NET.

C# is the only language designed for the .Net Framework.

C# is a fully object-oriented language (i.e support all the three tenets of object-oriented systems 1. Encapsulation 2.Inheritance 3. Polymorphism) that means C# code can be reusable, during execution the assembly is loaded into the CLR.

C# has automatic garbage collection and type safety that enables developers to build a variety of secure and robust applications.

C# use to create Windows client applications, XML Web services, distributed components, client‐server applications, database applications and much more.

Evaluation of Csharp (C#)

C# features:-

  • In microsoft c#, everything is an Object.
  • C# syntax improve many of the complexities of C++ and provides powerful features such as nullable value types, enumerations, delegates, lambda expressions and direct memory access, which are not found in Java.
  • C# simplifies c++ by eliminating operator like "->", "::", pointers(*).
  • C# does not support default arguments.
  • C# does not support the typed of statement.
  • In C#, structs are of value type.
  • C# does not separate class definition from implementation, classes are define and implemented in the same place and therefor there is no need for header file.
  • In C#, Data Types belong to either value type (which is created in the stack) or reference type(which are created in the heap).
  • All the data types in C# are inherited from the object super class, therefore all data type are object type.
  • Abstract of C# cannot be implemented.
  • In C#, a class can inherit implementation from one base class only.
  • In C#, a class or an interface can implement multiple interfaces.
  • Using the new modifier to explicitly hide an inherited member.
  • Calling the overridden base class members from derived classes.
  • C# supports additional c# operators such as "is" and "typeof".
  • In C# use of the extern keyword.
  • It has a huge standard library which is easy to use.
  • It allows for both managed and native code blocks.
  • C# declare null as keyword.
  • We can create object in c# using "new" keyword.
  • C# does not provide any defaults for constructors.
  • In C#, can't access static member via an objects.

What is ASP.NET Web Site Administration Tool and Find its location

ASP.NET Web Site Administration Tool

ASP.NET Website Administration tool is a utility provided along with Microsoft Visual Studio Microsoft Visual Studio which helps you manage user authentication in your Web sites.

Features of ASP.NET Web Site administration tool is available in System.Web.Security namespace which can access by programmatically.

You can become ASP.NET membership with ASP.NET Forms authentication or with ASP.NET, login control. By Using ASP.NET membership we can create new users and we can authenticate to users who visit your site.

Find or location of ASP.NET Web Site Administration Tool

You can find ASP.NET Web Site Administration Tool By many ways...
Find or Location of ASP.NET Web Site Administration Tool
Find or Location of ASP.NET Web Site Administration Tool


First look of ASP.NET Web Site Administration Tool

Web Site Administration Tool First Look
Web Site Administration Tool First Look


If click any tab security, application or provider before become the member of ASP.NET Web Site Administration than show message...
There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store. The following message may help in diagnosing the problem: Unable to connect to SQL Server database.

Web Site Administration Tool error Unable to connect to SQL Server database.
Web Site Administration Tool error Unable to connect to SQL Server database.

What is .Net Framework - and its components - CLR, CTS, CLS, JIT, Class library, MSIL (IL)


Origin of .net technology


OLE(Object Linking and Embedding) Technology :-

It is a technology developed by Microsoft and use to embed documents from one application into another application and manipulate object in another application. For developers, it brought OLE Control Extension (OCX).

COM(Component object Model) Technology :-

Each component and model developed and tested independently, and then integrated it and tested in the system, this technology is called COM.
Distribute development across multiple department which enhances software maintainability and reduce the complexity of software.

.Net technology :-

COM provide binary mechanism for intermodule communication is replaced by an intermediate language(IL). IL allow cross language integration, that enable us to developed web application easily.
Three Generation Of Component Model
Three Generation of Component Model

what is asp.net framework

.NET Framework support ASP.NET, as well as Windows Forms development
The .NET Framework consists of three main parts:

.Net framework provide tools for managing user and application interface which enable user to develop desktop and web applications using variety of language.
Windows froms
console Applications
Web Form
Web Service
Architecture Of .Net Framework
Architecture Of .Net Framework

Componenets of .Net Framework

Common language runtime (CLR):-

CLR is a runtime environment in which programs written in c# and other .Net language. i.e CLR is responsible for managing the execution of the code compiled for the .Net platform. it also support cross language interoperability
Component of CLR

Service provide by CLR

.Net framework class library

.Net provide library of base class which help to support the efforts of developers by providing base classes from which developers can inherit. Much of the functionality in the base class reside in the namespace called System.

Common Type system (CTS)

Using CTS, .net framework support multiple language. Basically CTS support Varity of Types and operation of many language, so there is no need type conversion while calling one language from another.

Common language specification (CLS)

CLS is a subset of Common Type system (CTS) that helps third-party compiler designers and library builders and language supporting the CLS can use each other class library.

MSIL (Microsoft intermediate language) or IL (intermediate language)

When compile a programme written in CLS-Compliant language, the source code is compiled into MSIL or IL.

Mange Code

when CLR satisfy the Code at runtime in order to execute forward to managed code. The Compiler that compatible to the >net platform generate managed code.

JIT (Just in Time)

It is used to convert common intermediate language into native code (Machine code).

Different type Of JIT

Execution of programme in CLR and behave of its components
Execution of programme in CLR and behave of its components

Key Points

C#, VB, J# generate only Intermediate language where as c++ generate Intermediate language and native code Native code not store permanently, Every time native code generate whenever execute the programme