Go to DotNetFunda.com
 Online : 1265 |  Welcome, Guest!   Login
 
Home > Articles > Sql Server > How to calculate total at the BackEnd using Trigger?

  • Download the OOPS, ASP.NET and ADO.NET online training sessions videos and related document for FREE, click here.

Submit Article | Articles Home | Search Articles |

How to calculate total at the BackEnd using Trigger?

 Posted on: 8/25/2009 6:14:14 PM by Syedshakeer | Views: 961 | Category: Sql Server | Level: Intermediate | Print Article
In General we take input values what we had entered in textboxes and calculating displaying the result through front end coding.But this may not be secure to your data.so calculation have to be done at the Back end in Sqlserver using Triggers.Trigger will take input of inserted values from a table,after calculating them it insert the result at the particular result column.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

Here i am going to give some input values for calculating the total marks.Below is the image of input values design form

How to create a trigger for insert statement?

Create trigger total_trigger
on stud_marks
for insert
As

--stud_marks is table name

--variables are declaring
Declare @rollno int 
Declare @marks1 int
Declare @marks2 int
Declare @totalmarks int

--storing the inserted record value in a varaible
set @rollno=(select rollno from inserted)
set @marks1=(select marks1 from  inserted)
set @marks2=(select marks2 from  inserted)

--Note above 'inserted' is not a table name.
--calculating the Marks and storing a variable

set @totalmarks= @marks1+@marks2

update stud_marks set total=@totalmarks where rollno=@rollno

Go

Run the above code in SqlQuery Analyzer,it shows the message as

The command(s) completed successfully.

Inserting the Input Values through windows Form:

Below image is the Input Design windows form:

Write the Code in Submit Button as follows:

private void button1_Click(object sender, EventArgs e)

{

SqlConnection conn = new SqlConnection("Data Source=INTHIYAAZ;Initial Catalog=shakeer;User ID=sa;Password=sa");

conn.Open();

SqlCommand cmd = new SqlCommand("insert into stud_marks(marks1,marks2,rollno) values("+txtmarks1 .Text +","+txtmarks2 .Text +","+txtrollno .Text +")", conn);

cmd.ExecuteNonQuery();

//-----------trigger will be fired automatically when the record are inserted

cmd = new SqlCommand("select total from stud_marks where rollno="+txtrollno .Text +"", conn);

SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())

{

txttotal.Text = dr["total"].ToString();

}

}

Output of Totalmarks Image:

when you click on submi tbutton ,first the values will insert on their corresponding colomns.Later Trigger will fired.It calcultes the marks later total marks result will be inserted using update command.

Now open 'stud_marks' Table  and see the output of Image as Below:

 

Thanks For Reading my Article!

Syed Shakeer Hussain


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Bronze
Status: [Member]
Biography:Hi to All ..
I am Working as Software Developer on .NET and MVM of www.dotnetspider.com
 Latest post(s) from Syedshakeer

   ◘ How to get the value of a Hidding column in a Gridview using C# posted on 9/8/2009 6:05:36 PM
   ◘ How to calculate total at the BackEnd using Trigger? posted on 8/25/2009 6:14:14 PM
   ◘ How to display records as First-Next-Previous-Last in a Textboxes using Windows Application? posted on 8/25/2009 6:01:05 PM
   ◘ How to Start Mobile Application on Windows? posted on 8/18/2009 1:00:01 AM
   ◘ Paging for First, Next, Previous and Last in gridview posted on 8/16/2009 12:47:43 PM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)