In Page Design
========================================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestReansaction.aspx.cs" Inherits="WebApplication1.TestReansaction" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Button ID="button2" runat="server" Text="btn test 2" OnClick="button2_Click" />
</div>
</form>
</body>
</html>
========================================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestReansaction.aspx.cs" Inherits="WebApplication1.TestReansaction" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Button ID="button2" runat="server" Text="btn test 2" OnClick="button2_Click" />
</div>
</form>
</body>
</html>
=================================
In C# Code
====================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class TestReansaction : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
using (CompanyEntities context = new CompanyEntities())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
EmployeeMaster employee = new EmployeeMaster();
employee.Code = "A0001";
employee.Name = "Jignesh Trivedi";
employee.DepartmentId =2;
context.EmployeeMasters.Add(employee);
context.SaveChanges();
DepartmentMaster dept = new DepartmentMaster();
dept.Code = "DEP000112765776767767676";
dept.Name = "Department 1";
context.DepartmentMasters.Add(dept);
context.SaveChanges();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}
}
}
}