Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Frieds,

Here i have Written one Trigger Concept in SqlServer and I want to Know how

to Call the Triggers in c# with Asp.net

My Triggers in Sql server:
SQL
ALTER trigger tr_insert
    on SchoolDetailsInfo
    after insert
    as
    begin
    
    declare @intSchoolId int= null
    declare @SchoolName varchar(30)=null
    declare @PrincipleName varchar(30)=null
    declare @LandlineNo varchar(30)=null
    declare @MobileNo varchar(30)=null
    declare @Address varchar(30)=null
    declare @DateofBirth varchar(30)=null
    declare @Flag varchar = null
    
    select @intSchoolId=intSchoolId from inserted
    select @SchoolName =strSchoolName from inserted
    select @PrincipleName=strPrincipleName from inserted
    select @LandlineNo =strLandLine from inserted
    select @MobileNo=strMobileNo from inserted
    select @Address =strAddress from inserted
    select @DateofBirth =intdate from inserted
    
    update SchoolDetailsInfo
    
    set strSchoolName='anil' where intSchoolId!=@intSchoolId
    end



So now i want to call the Trigger in asp.net with c#



Regards,

Anilkumar.D
Posted
Updated 25-Aug-11 20:02pm
v2

You cannot call the trigger directly from any code. The trigger is executed by the database when the event for which it is created happens. In your case, since it is an AFTER INSERT trigger, it will be executed after an insert happens on the table in which the trigger is created. You can insert data into the table using ADO.NET to run the trigger code.
C#
SqlCommand comm = new SqlCommand("INSERT INTO Table1 (Col1, Col2, Col3) VALUES (@Col1, @Col2, @Col3)", connection);
comm.Parameters.AddWithValue("@Col1", Col1Value);
comm.Parameters.AddWithValue("@Col2", Col2Value);
comm.Parameters.AddWithValue("@Col3", Col3Value);
comm.ExecuteNonQuery();
 
Share this answer
 
Comments
Anil Honey 206 26-Aug-11 2:07am    
Thanks Shameel ThankYou VeryMuch.
Suresh Suthar 26-Aug-11 2:07am    
Very nice answer. 5.
Anil Honey 206 26-Aug-11 2:13am    
Its Ok But i want to See where the Trigger Fires when iam inserting the Data Just now only i executed My Code.But iam not Getting the Difference.I want the Drifference of Triggers.


Regards,

Anilkumar.D
[no name] 26-Aug-11 2:40am    
You can log events to a table inside your trigger to check if the trigger has run or not.
You can not use or call trigger using C#.
Trigger automatically executed in response to certain events on a particular table or view in a database.
 
Share this answer
 
Comments
Anil Honey 206 26-Aug-11 2:17am    
SO I Should Put any Condiction while Iam Creating Trigger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900