Wednesday, 1 February 2012

Transaction Example.[How user can pay money one account to another account]

Source 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>
   
        FROM<asp:Label ID="Label3" runat="server"
            style="z-index: 1; left: 5px; top: 81px; position: absolute"
            Text="Amount"></asp:Label>
   
        <asp:DropDownList ID="drpfrom" runat="server" AutoPostBack="True"
            onselectedindexchanged="drpfrom_SelectedIndexChanged">
        </asp:DropDownList>
   
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   
    </div>
    <asp:TextBox ID="txtamount" runat="server"
        style="z-index: 1; left: 58px; top: 84px; position: absolute"></asp:TextBox>
    <asp:Button ID="btnsave" runat="server"
        style="z-index: 1; left: 199px; top: 79px; position: absolute"
        Text="Save" onclick="btnsave_Click" />
    <asp:DropDownList ID="drpto" runat="server"
        style="position:absolute; top: 43px; left: 57px;" AutoPostBack="True"
        onselectedindexchanged="drpto_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:Label ID="To" runat="server"
            style="z-index: 1; left: 8px; top: 45px; position: absolute"
        Text="To A/c"></asp:Label>
    <asp:TextBox ID="TextBox2" runat="server"
        style="position:absolute; top: 40px; left: 135px;"></asp:TextBox>
    <p>
  
    </p>
    <asp:GridView ID="GridView1" runat="server"
        style="position:absolute; top: 199px; left: 196px;">
    </asp:GridView>
    <p>
        &nbsp;</p>
    <p>
    <asp:Label ID="Label4" runat="server" Text="Label"
            style="position:absolute; top: 158px; left: 67px;"></asp:Label>
    </p>
    </form>
</body>
</html>
Design Example



Runtime Design
 
 
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;
using System.Data;
using System.Data.SqlClient;


public partial class Transction : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=Satya\\S12;database=linku;uid=li;pwd=4555");
    SqlDataAdapter da;
    DataTable dt;
    Class1 obj = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
        Label4.Visible = false;
        if(!Page.IsPostBack)
        {
            FillDrp();
            fillGrid();
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        obj = new Class1();
        Hashtable ht = new Hashtable();
        ht.Add("FromAcno", drpfrom.SelectedValue);
        ht.Add("Toacno ", drpto.SelectedValue);
        ht.Add("amount", txtamount.Text);
     
       obj.ExecuteQuery("TrDemo1", ht);
       Label4.Visible = true;
       Label4.Text="success";
       fillGrid();
     
    }
    public void FillDrp()
    {
        da = new SqlDataAdapter("Select * from Fromac", con);
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        dt = new DataTable();
        da.Fill(dt);
        drpfrom.DataSource = dt;
        drpto.DataSource = dt;
        drpfrom.DataTextField = "FromAcno";
        drpfrom.DataValueField = "FromAcno";
        drpfrom.DataBind();
        drpto.DataTextField = "FromAcno";
        drpto.DataValueField = "FromAcno";
        drpto.DataBind();
    }
    public void fillGrid()
    {
        da = new SqlDataAdapter("Select * from Fromac", con);
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
    public void FillNamebyACNO()
    {
        da = new SqlDataAdapter("Select Name from Fromac where FromAcno='"+drpfrom.SelectedValue+"'", con);
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        dt = new DataTable();
        da.Fill(dt);
        TextBox1.Text = dt.Rows[0]["Name"].ToString();
        //TextBox2.Text = dt.Rows[0]["Name"].ToString();
    }
    public void FillNametoACNO()
    {
        da = new SqlDataAdapter("Select Name from Fromac where FromAcno='" + drpto.SelectedValue + "'", con);
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        dt = new DataTable();
        da.Fill(dt);
      
        TextBox2.Text = dt.Rows[0]["Name"].ToString();
    }
    protected void drpfrom_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillNamebyACNO();

    }
    protected void drpto_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillNametoACNO();
    }
}

Class File Coadiong
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/aa;database=linku;uid=li;pwd=li123");
    }
    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;
        }
    }
}
 Storeprocedure

Create procedure TrDemo1
(
@FromAcno int,

@amount decimal(18,2),
@Toacno int
)
as
BEGIN
BEGIN TRY
BEGIN TRANSACTION
Update Fromac set amount=(amount-@amount) where FromAcno=@FromAcno
Update Fromac set amount=(amount+@amount) where FromAcno=@Toacno
print 'success'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
PRINT 'ERROR'
END CATCH
END
Database Table





No comments:

Post a Comment