Kobinath Ram

Kobinath Ram

  • NA
  • 130
  • 0

bank account to account transaction in c#

May 24 2017 8:14 AM
 
 
 
 
SqlConnection con = new SqlConnection("server=.; database=bank; user id=sa; password=;");
private void button1_Click(object sender, EventArgs e)
{
int fano, tano, bal;
fano = int.Parse(textBox1.Text);
tano = int.Parse(textBox2.Text);
bal = int.Parse(textBox3.Text);
 
 
SqlCommand cmd = con.CreateCommand();
SqlTransaction transaction = null;
try
{
con.Open();
transaction = con.BeginTransaction();
cmd.Transaction = transaction;
cmd.CommandText = "update accountholder set Balance= '"+bal+"'-Balance where acno= '" + textBox1.Text + "'";
cmd.ExecuteNonQuery();
cmd.CommandText = "update accountholder set Balance= '" + bal + "'+ Balance where acno= '" + textBox2.Text + "'";
cmd.ExecuteNonQuery();
cmd.CommandText = "insert into TransferDetails (acno1,acno2,Balance) VALUES (@acno1,@acno2,@Balance)";
cmd.Parameters.AddWithValue("@acno1", textBox1.Text);
cmd.Parameters.AddWithValue("@acno2", textBox2.Text);
cmd.Parameters.AddWithValue("@Balance", textBox3.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("suceess");
transaction.Commit();
}
catch(Exception ex)
{
transaction.Rollback();
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
 
I wanted to transfer money from one account to another. The error occurred is SQL syntax based on where clause.i have atached the source code also. please correct the error 
cmd.CommandText = "update accountholder set Balance= '"+bal+"'-Balance where acno= '" + textBox1.Text + "'";
cmd.ExecuteNonQuery();
cmd.CommandText = "update accountholder set Balance= '" + bal + "'+ Balance where acno= '" + textBox2.Text + "'";
this line were getting error while running the code thanks 

Answers (4)