Tuesday, 27 November 2012

Bank Transaction Using Database in Windows Application

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace bankdetails
{
public partial class Form1 : Form
{
int acc, amt,age,i=-1;
string name, branch, z;
OleDbConnection dc = new OleDbConnection();
OleDbCommand dcmd = new OleDbCommand();
OleDbDataReader dr;
public Form1()
{
InitializeComponent();
}

private void CLEAR_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
errorProvider1.Clear();

}

private void Form1_Load(object sender, EventArgs e)
{
dc.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;" + "Data Source=E:\\bank.mdb";
dcmd.Connection = dc;
dc.Open();
dcmd.CommandText="SELECT acc FROM bank";
dr = dcmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr.GetInt16(0));
}
dr.Close();
}

private void SAVE_Click(object sender, EventArgs e)
{
if (textBox3.Text == "")
{
errorProvider1.SetError(textBox3, "fill it");
}
else
{

acc = Convert.ToInt16(textBox1.Text);
name = textBox2.Text;
age = Convert.ToInt16(textBox3.Text);
branch = textBox4.Text;
amt = Convert.ToInt16(textBox5.Text);
dcmd.CommandText = "INSERT INTO bank VALUES(" + acc + ",'" + name + "'," + age + ",'" + branch + "'," + amt + ")";
dr = dcmd.ExecuteReader();
dr.Close();
MessageBox.Show("Record Inserted Successfully");
}
}

private void DELETE_Click(object sender, EventArgs e)
{
z = comboBox1.SelectedItem.ToString();
dcmd.CommandText = "DELETE * FROM bank WHERE acc=" + z + "";
dr = dcmd.ExecuteReader();
dr.Close();
MessageBox.Show("Record Deleted");
}

private void UPDATE_Click(object sender, EventArgs e)
{
z = comboBox1.SelectedItem.ToString();
//dr.Read();
acc = Convert.ToInt16(textBox1.Text);
name = textBox2.Text;
age = Convert.ToInt16(textBox3.Text);
branch = textBox4.Text;
amt =Convert.ToInt16(textBox5.Text);
dcmd.CommandText = "UPDATE bank SET acc='" + acc + "',name='" + name + "',age=" + age + ",branch='" + branch + "',amt=" + amt + " WHERE acc=" + z + "";
dr = dcmd.ExecuteReader();
dr.Close();
MessageBox.Show("RECORD UPDATED SUCESSFULLY");
}

private void SEARCH_Click(object sender, EventArgs e)
{
z = comboBox1.SelectedItem.ToString();
dcmd.CommandText = "SELECT * FROM bank WHERE acc=" + z + "";
dr = dcmd.ExecuteReader();
dr.Read();
acc = dr.GetInt16(0);
name = dr.GetString(1);
age = dr.GetInt16(2);
branch = dr.GetString(3);
amt = dr.GetInt16(4);
textBox1.Text = acc.ToString();
textBox2.Text = name;
textBox3.Text = age.ToString();
textBox4.Text = branch;
textBox5.Text = amt.ToString();
dr.Close();


}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = comboBox1.SelectedItem.ToString();
dcmd.CommandText = "select * from bank where acc=" + s + "";
dr = dcmd.ExecuteReader();
while (dr.Read())
{
i++;
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = dr.GetInt16(0);
dataGridView1.Rows[i].Cells[1].Value = dr.GetString(1);
dataGridView1.Rows[i].Cells[2].Value = dr.GetInt16(2);
dataGridView1.Rows[i].Cells[3].Value = dr.GetString(3);
dataGridView1.Rows[i].Cells[4].Value = dr.GetInt16(4);
}
dr.Close();

}
}
}

No comments:

Post a Comment