In previous tutorials tutorial ,i have already explained transactions in sql server.But now i am going to explain transactions in real asp.net application.We can provide a save point to divide a transaction into sub logical transaction,Means if there are set of instruction in a transaction which must be executed and a set of statement will be executed or Rollback on dependent on some condition.Here we can provide a save point to those statement which must be committed.
There are some steps to implement this concepts on website which are given below:-
Step 1:- First open your visual studio -->File-->New-->website-->select ASP.NET Empty website -->OK-->open solution explorer -->Add a Web Form-->drag and drop Label ,Text Box and Button control from toolBox as shown below:-
There are some steps to implement this concepts on website which are given below:-
Step 1:- First open your visual studio -->File-->New-->website-->select ASP.NET Empty website -->OK-->open solution explorer -->Add a Web Form-->drag and drop Label ,Text Box and Button control from toolBox as shown below:-
Step 2:-Now open your sql server -->create a students table with three column(name,age,location).
Step 3:-Now Double click on insert button -->write the c# codes as given below:-
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Transaction_app : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = TextBox7.Text;
SqlTransaction sqt = null;
try
{
SqlConnection con = new SqlConnection("data source=Ramashanker-PC;Integrated Security=Yes;Database=master");
con.Open();
sqt = con.BeginTransaction();
SqlCommand cmd = new SqlCommand("insert into students values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);
cmd.Transaction=sqt;
cmd.ExecuteNonQuery();
sqt.Save("my_trans");
SqlCommand cmd2 = new SqlCommand("insert into students values('" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')", con);
cmd2.Transaction = sqt;
cmd2.ExecuteNonQuery();
if(str!="yes")
{
sqt.Rollback("my_trans");
}
sqt.Commit();
con.Close();
}
catch(Exception ex)
{
sqt.Rollback();
Label1.Text=ex.Message;
}
}
}
Step 4:- Now Run the application(press F5)-->Enter the required field details and press Insert button as shown below:-
Step 5:- Now open your sql server -->you will see that both transactions data have saved in students table.see it:-
Note:- When we Enter No instead of yes then only first transaction values are saved in students tables.
For More
- Connect database using oleDb
- How to implement Ajax based web services in asp.net
- How to use caching in asp.net application
- View state and hidden fields controls in asp.net
- How to send mail from asp.net web site
- File and file info class in c#
- Generic collection in c#
- Method hiding in c#
Download
System.NullReferenceException: Object reference not set to an instance of an object. for my_trans
ReplyDeletehi rajasekhar , check your each text box values correctly,means you are entered correct text box in coding part.
Deletewhat my_trans did here... and where it was declared
Delete