RunCodes
Programming & Tech

How to Create a Banking Application in C#.NET using SQL Server Database and Visual Studio 2022[WITH SOURCE CODE]

0 278

deposit code:[inside the clicking event of deposit button]

 


SqlConnection con = new SqlConnection("YOUR-CONNECTION STRING");
con.Open();
string depositQuery = "INSERT INTO account_new (name, address,balance, accountno," +
"date, deposit) VALUES (@name, @add,@balance,@acc,@date,@deposit)";
SqlCommand cmd = new SqlCommand(depositQuery, con);
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@add", txtAdd.Text);
cmd.Parameters.AddWithValue("@balance", txtBalance.Text);
cmd.Parameters.AddWithValue("@date", dtDate.Value);
cmd.Parameters.AddWithValue("@deposit", txtBalance.Text);
string accountno = AccountNo();
cmd.Parameters.AddWithValue("@acc", accountno);
int count = cmd.ExecuteNonQuery();
con.Close();
if (count > 0)
{
MessageBox.Show("deposited successfully", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

 

Account generated code:


private string AccountNo()
{
string startWith = "1280";
Random random = new Random();
string gen = random.Next(0, 999999).ToString("D6");
string accountNo = startWith + gen;
return accountNo;
}

 

withdraw balance code[inside the clicking event of withdraw button]


int balance = 0;
SqlConnection con = new SqlConnection("YOUR-CONNECTION-STRING");
con.Open();
string readQuery = "SELECT * FROM account_new WHERE accountno=@acc";
SqlCommand cmd = new SqlCommand(readQuery, con);
cmd.Parameters.AddWithValue("@acc", txtAcc.Text);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
balance = Convert.ToInt32(reader["balance"]);

}
reader.Close();
if (balance > 0 && balance > Convert.ToInt32(txtBalance.Text))
{
int newBalance = balance - int.Parse(txtBalance.Text);
string updateQuery = "UPDATE account_new SET balance=@balance, withdraw=@with" +
" WHERE accountno=@accno";
SqlCommand cmdup = new SqlCommand(updateQuery, con);
cmdup.Parameters.AddWithValue("@balance", newBalance);
cmdup.Parameters.AddWithValue("@with", txtBalance.Text);
cmdup.Parameters.AddWithValue("@accno", txtAcc.Text);
cmdup.ExecuteNonQuery();
con.Close();
MessageBox.Show("Remaining Balance" + newBalance);
}
else
{
MessageBox.Show("insufficient balance", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
else
{
MessageBox.Show("account not found", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

 

show details[inside the clicking event of show button]


SqlConnection con = new SqlConnection("YOUR-CONNECTION-STRING");
con.Open();
string selectQuery = "SELECT * FROM account_new WHERE accountno=@acc";
SqlCommand cmd = new SqlCommand(selectQuery, con);
cmd.Parameters.AddWithValue("@acc", txtAcc.Text);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
txtName.Text = reader["name"].ToString();
txtAdd.Text = reader["address"].ToString();
txtBalance.Text = reader["balance"].ToString();
}
reader.Close();
con.Close();
dtDate.Enabled=false;

 

MORE:

Leave a comment