Tuesday 17 January 2012

At a time insert Data in two table with the help of Transction and Class File Methods

SourceFile Code
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Transction.aspx.cs" Inherits="Transction" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Label ID="Label1" runat="server"
            style="z-index: 1; left: 53px; top: 54px; position: absolute" Text="Roll"></asp:Label>
        <asp:Label ID="Label2" runat="server"
            style="z-index: 1; left: 44px; top: 83px; position: absolute" Text="Name"></asp:Label>
        <asp:Label ID="Label3" runat="server"
            style="z-index: 1; left: 40px; top: 120px; position: absolute"
            Text="Address"></asp:Label>
   
    <asp:TextBox ID="txtname" runat="server"
        style="z-index: 1; left: 105px; top: 97px; position: absolute"></asp:TextBox>
    <asp:TextBox ID="txtroll" runat="server"
        style="z-index: 1; left: 98px; top: 55px; position: absolute; right: 1069px;"></asp:TextBox>
   
    </div>
    <asp:TextBox ID="txtaddress" runat="server"
        style="z-index: 1; left: 99px; top: 118px; position: absolute"></asp:TextBox>
    <asp:Button ID="btnsave" runat="server"
        style="z-index: 1; left: 106px; top: 167px; position: absolute"
        Text="Save" onclick="btnsave_Click" />
    <p>
  
    </p>
    </form>
</body>
</html>
ClassFileCoading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
    private SqlConnection con;
    public Class1()
    {
        con = new SqlConnection(@"server=Satya\12;database=linku;uid=li;pwd=satya1");
    }
    public void ExecuteQuery(string sp,Hashtable ht)
    {
        try
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            SqlCommand cmd = new SqlCommand(sp, con);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (DictionaryEntry p in ht)
            {
                cmd.Parameters.Add("@" + (string)p.Key, p.Value);
            }
            cmd.ExecuteNonQuery();
            con.Close();
            cmd.Dispose();
        }
        catch (Exception ex)
        {
            string errMsg = ex.Message;
        }
        finally
        {

            if (this.con.State == ConnectionState.Open)
                con.Close();
            if (this.con != null) this.con = null;
            if (this.con != null) this.con = null;
        }
    }
}
C# Coading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;



public partial class Transction : System.Web.UI.Page
{
    Class1 obj = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        obj = new Class1();
        Hashtable ht = new Hashtable();
        ht.Add("roll", txtroll.Text);
        ht.Add("Name ", txtname.Text);
        ht.Add("address", txtaddress.Text);
     
       obj.ExecuteQuery("TrDemo", ht);
    }
}
Table Design
      Demo1 :-
 
Demo2 :-


Database StoreProcedure

Createprocedure TrDemo
(@roll int,
@name varchar(50),
@address varchar(50)
)
as
BEGIN
BEGIN TRY
BEGIN TRANSACTION
Insert into Demo1 Values(@roll,@name)
Insert into Demo3 Values(@roll,@name,@address)
print 'success'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
PRINT 'ERROR'
END CATCH
END

Design


No comments:

Post a Comment