Monday 3 August 2015

SQL Transaction in ASP.Net

  1. transaction is a single unit of work. If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database. If a transactionencounters errors and must be canceled or rolled back, then all of the data modifications are erased.








Full coding :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Transactions_in_ADO.NET
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {
            string sourceCS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection sourceCon = new SqlConnection(sourceCS))
            {
                SqlCommand cmd = new SqlCommand("Select * from CustomerDetails where AccountNumber=@1 and pin=@2", sourceCon);
                sourceCon.Open();
                cmd.Parameters.AddWithValue("@1", textBox2.Text);
                cmd.Parameters.AddWithValue("@2", textBox11.Text);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    textBox9.Text = dr["CustomerName"].ToString();
                    textBox4.Text = dr["Balance"].ToString();
                    getcustomerdetails(textBox2.Text.ToString(), textBox11.Text.ToString(), "CustomerTwo");
                }

                else
                {
                    MessageBox.Show("Invalid Login Details !!");
                }
            }
        }



        private void button6_Click(object sender, EventArgs e)
        {
            string sourceCS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection sourceCon = new SqlConnection(sourceCS))
            {
                SqlCommand cmd = new SqlCommand("Select * from CustomerDetails where AccountNumber=@1 and pin=@2", sourceCon);
                sourceCon.Open();
                cmd.Parameters.AddWithValue("@1", textBox1.Text);
                cmd.Parameters.AddWithValue("@2", textBox10.Text);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    textBox8.Text = dr["CustomerName"].ToString();
                    textBox3.Text = dr["Balance"].ToString();
                    getcustomerdetails(textBox1.Text.ToString(), textBox10.Text.ToString(), "CustomerOne");
                }
                else
                {
                    MessageBox.Show("Invalid Login Details !!");
                }
            }
        }

        private void getcustomerdetails(string a, string b, string customer)
        {
            try
            {

                string sourceCS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                using (SqlConnection sourceCon = new SqlConnection(sourceCS))
                {
                    dataGridView1.Visible = true;
                    string datagrid = "Select * from CustomerDetails where AccountNumber=" + a + " and pin=" + b + "";
                    SqlDataAdapter da = new SqlDataAdapter(datagrid, sourceCS);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    if (customer == "CustomerOne")
                    {
                        dataGridView1.DataSource = ds.Tables[0];
                    }
                    else
                    {
                        dataGridView2.DataSource = ds.Tables[0];
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);


            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string sourceCS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                using (SqlConnection con = new SqlConnection(sourceCS))
                {
                    con.Open();

                    int a = Convert.ToInt32(textBox3.Text);
                    int b = Convert.ToInt32(textBox4.Text);
                    int c = Convert.ToInt32(textBox6.Text);
                    if (c <= a)
                    {
                        int d = (a - c);
                        int g = (b + c);
                        textBox5.Text = Convert.ToString(d);
                        textBox7.Text = Convert.ToString(g);
                        SqlCommand cmd1 = new SqlCommand("update CustomerDetails set balance=" + textBox5.Text + " where AccountNumber=" + textBox1.Text + "", con);
                        SqlCommand cmd2 = new SqlCommand("update CustomerDetails set balance=" + textBox7.Text + " where AccountNumber=" + textBox2.Text + "", con);
                        cmd1.ExecuteNonQuery();
                        cmd2.ExecuteNonQuery();
                        con.Close();

                        MessageBox.Show("Amount Transfered Successfully");
                    }
                    else
                    {
                        MessageBox.Show("Please Enter Valid Amount");
                    }

                    clear();
                    bal1();
                    bal2();
                    //  currenttbl();
                    currenttbl1();
                    currenttbl2();
                }
            }
            catch
            {
                MessageBox.Show("Transaction Failed");
                clear();
                bal1();
                bal2();
                //currenttbl();
                currenttbl1();
            }
        }

        private void currenttbl1()
        {
            try
            {

                SqlConnection con = new SqlConnection("Data Source=GOKUL-PC\\GOKULSQL;Initial Catalog=SqlServerTutorial;Integrated Security=True");
                con.Open();
                dataGridView1.Visible = true;

                string datagrid = "select * from CustomerDetails where AccountNumber=" + textBox1.Text.ToString() + "";
                SqlDataAdapter da = new SqlDataAdapter(datagrid, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];

                //comboBox2.Text = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);


            }
        }

        private void currenttbl2()
        {
            try
            {

                SqlConnection con = new SqlConnection("Data Source=GOKUL-PC\\GOKULSQL;Initial Catalog=SqlServerTutorial;Integrated Security=True");
                con.Open();
                dataGridView1.Visible = true;

                string datagrid = "select * from CustomerDetails where AccountNumber=" + textBox2.Text.ToString() + "";
                SqlDataAdapter da = new SqlDataAdapter(datagrid, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridView2.DataSource = ds.Tables[0];

                //comboBox2.Text = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);


            }
        }

        private void bal2()
        {
            string sourceCS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(sourceCS))
            {
                con.Open();
                {
                    SqlCommand cmd = new SqlCommand("select balance from CustomerDetails where AccountNumber = " + textBox2.Text + " ", con);
                    SqlDataReader dr;
                    dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        textBox4.Text = dr[0].ToString();


                        con.Close();

                    }
                    else
                    {

                        // Button1.Enabled = false;
                    }
                }
            }
        }

        private void bal1()
        {
            string sourceCS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(sourceCS))
            {
                con.Open();
                {
                    SqlCommand cmd = new SqlCommand("select balance from CustomerDetails where AccountNumber = " + textBox1.Text + " ", con);
                    SqlDataReader dr;
                    dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        textBox3.Text = dr[0].ToString();


                        con.Close();

                    }
                    else
                    {

                        // Button1.Enabled = false;
                    }
                }
            }
        }


        private void clear()
        {
            textBox3.Text = "";
            textBox4.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=GOKUL-PC\GOKULSQL;database=SqlServerTutorial;Integrated Security=True");
            con.Open();
            SqlTransaction tran = con.BeginTransaction();
            try
            {


                int a = Convert.ToInt32(textBox3.Text);
                int b = Convert.ToInt32(textBox4.Text);
                int c = Convert.ToInt32(textBox6.Text);
                if (c <= a)
                {
                    int d = (a - c);
                    int g = (b + c);
                    textBox5.Text = Convert.ToString(d);
                    textBox7.Text = Convert.ToString(g);
                    SqlCommand cmd1 = new SqlCommand("update CustomerDetails set balance=" + textBox5.Text + " where AccountNumber=" + textBox1.Text + "", con, tran);
                    SqlCommand cmd2 = new SqlCommand("update CustomerDetails set balance=" + textBox7.Text + " where AccountNumber=" + textBox2.Text + "", con, tran);
                    cmd1.ExecuteNonQuery();
                    cmd2.ExecuteNonQuery();
                    tran.Commit();
                    con.Close();

                    MessageBox.Show("Amount TRANSFERED");
                }
                else
                {
                    tran.Rollback();
                    MessageBox.Show("Please Enter Valid Amount");
                }

            }
            catch (Exception)
            {
                tran.Rollback();
                MessageBox.Show("Transaction Declined amd RolledBack");
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
            clear();
            bal1();
            bal2();
            currenttbl1();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=GOKUL-PC\GOKULSQL;database=SqlServerTutorial;Integrated Security=True");
            con.Open();
            SqlTransaction tran = con.BeginTransaction();
            try
            {


                int a = Convert.ToInt32(textBox3.Text);
                int b = Convert.ToInt32(textBox4.Text);
                int c = Convert.ToInt32(textBox6.Text);
                if (c <= a)
                {
                    int d = (a - c);
                    int g = (b + c);
                    textBox5.Text = Convert.ToString(d);
                    textBox7.Text = Convert.ToString(g);
                    SqlCommand cmd1 = new SqlCommand("update CustomerDetails set balance=" + textBox5.Text + " where AccountNumber=" + textBox1.Text + "", con, tran);
                    SqlCommand cmd2 = new SqlCommand("update CustomerDetails set =" + textBox7.Text + " where AccountNumber=" + textBox2.Text + "", con, tran);
                    cmd1.ExecuteNonQuery();
                    cmd2.ExecuteNonQuery();
                    tran.Commit();
                    con.Close();

                    MessageBox.Show("Amount TRNSFERED");
                }
                else
                {
                    tran.Rollback();
                    MessageBox.Show("Please Enter Valid Amount");
                }

            }
            catch (Exception)
            {
                tran.Rollback();
                MessageBox.Show("Transaction Declined amd RolledBack");
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
            clear();
            bal1();
            bal2();
            // currenttbl();
            currenttbl1();
        }
    }
}

No comments:

Post a Comment