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;
namespace SqlTransactionClassinAdodotNet
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
SqlCommand comm1, comm2, comm3;
SqlTransaction trans;
SqlDataAdapter adapter1, adapter2;
DataSet ds1, ds2;
string constring = @"Data Source=BHARTIJILEDAR\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=1234";
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection(constring);
conn.Open();
comm1 = new SqlCommand("select userid from userdet", conn);
SqlDataReader reader = comm1.ExecuteReader();
while (reader.Read())
{
cmbuserid.Items.Add(reader[0]);
}
reader.Close();
conn.Close();
}
private void btnshowrecord_Click(object sender, EventArgs e)
{
adapter1 = new SqlDataAdapter("select * from userdet", constring);
adapter2 = new SqlDataAdapter("select * from moneytrans1", constring);
ds1 = new System.Data.DataSet();
adapter1.Fill(ds1);
ds2 = new System.Data.DataSet();
adapter2.Fill(ds2);
dgvforuserdet.DataSource = ds1.Tables[0];
dgvformoneytrans1.DataSource = ds2.Tables[0];
}
private void btnok_Click(object sender, EventArgs e)
{
if (txtamount.Text == "" || cmbuserid.Text == "")
{
MessageBox.Show("Plz, Write Amout");
}
else
{
conn = new SqlConnection(constring);
conn.Open();
comm2 = new SqlCommand("insert into moneytrans1 values(" + cmbuserid.SelectedItem.ToString() + "," + txtamount.Text + ")", conn);
comm3 = new SqlCommand("update userdet set amount=amount-'" + txtamount.Text + "' where userid="+ cmbuserid.SelectedItem.ToString()+" ", conn);
trans = conn.BeginTransaction();
comm2.Transaction = trans;
comm3.Transaction = trans;
try
{
comm2.ExecuteNonQuery();
comm3.ExecuteNonQuery();
trans.Commit();
MessageBox.Show("Transaction Complted. ");
}
catch (Exception)
{
trans.Rollback();
MessageBox.Show("Transaction Failed..");
}
}
conn.Close();
}
}
}
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;
namespace SqlTransactionClassinAdodotNet
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
SqlCommand comm1, comm2, comm3;
SqlTransaction trans;
SqlDataAdapter adapter1, adapter2;
DataSet ds1, ds2;
string constring = @"Data Source=BHARTIJILEDAR\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=1234";
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection(constring);
conn.Open();
comm1 = new SqlCommand("select userid from userdet", conn);
SqlDataReader reader = comm1.ExecuteReader();
while (reader.Read())
{
cmbuserid.Items.Add(reader[0]);
}
reader.Close();
conn.Close();
}
private void btnshowrecord_Click(object sender, EventArgs e)
{
adapter1 = new SqlDataAdapter("select * from userdet", constring);
adapter2 = new SqlDataAdapter("select * from moneytrans1", constring);
ds1 = new System.Data.DataSet();
adapter1.Fill(ds1);
ds2 = new System.Data.DataSet();
adapter2.Fill(ds2);
dgvforuserdet.DataSource = ds1.Tables[0];
dgvformoneytrans1.DataSource = ds2.Tables[0];
}
private void btnok_Click(object sender, EventArgs e)
{
if (txtamount.Text == "" || cmbuserid.Text == "")
{
MessageBox.Show("Plz, Write Amout");
}
else
{
conn = new SqlConnection(constring);
conn.Open();
comm2 = new SqlCommand("insert into moneytrans1 values(" + cmbuserid.SelectedItem.ToString() + "," + txtamount.Text + ")", conn);
comm3 = new SqlCommand("update userdet set amount=amount-'" + txtamount.Text + "' where userid="+ cmbuserid.SelectedItem.ToString()+" ", conn);
trans = conn.BeginTransaction();
comm2.Transaction = trans;
comm3.Transaction = trans;
try
{
comm2.ExecuteNonQuery();
comm3.ExecuteNonQuery();
trans.Commit();
MessageBox.Show("Transaction Complted. ");
}
catch (Exception)
{
trans.Rollback();
MessageBox.Show("Transaction Failed..");
}
}
conn.Close();
}
}
}