Professional Documents
Culture Documents
Laboratory Activity Add Edit Delete Search
Laboratory Activity Add Edit Delete Search
Laboratory Activity Add Edit Delete Search
2. Create a GUI on your C# programming language and place the following objects.
namespace customerdb
{
public partial class main_form : Form
{
}
private void search_Id() {
listView1.Items.Clear();
MySqlCommand src_id_Cmd = new MySqlCommand("SELECT * FROM `customer_table`
WHERE customer_id like '"+txtsrc.Text+"%'", connect);
try
{
MySqlDataAdapter src_id_Adt = new MySqlDataAdapter(src_id_Cmd);
DataTable src_id_tbl = new DataTable();
src_id_Adt.Fill(src_id_tbl);
foreach(DataRow customer in src_id_tbl.Rows)
{
ListViewItem item = new
ListViewItem(customer["costumer_no"].ToString());
item.SubItems.Add(customer["customer_id"].ToString());
item.SubItems.Add(customer["lastname"].ToString());
item.SubItems.Add(customer["firsname"].ToString());
item.SubItems.Add(customer["mname"].ToString());
item.SubItems.Add(customer["barangay"].ToString());
item.SubItems.Add(customer["municipality"].ToString());
item.SubItems.Add(customer["province"].ToString());
item.SubItems.Add(customer["sex"].ToString());
item.SubItems.Add(customer["cred_status"].ToString());
item.SubItems.Add(customer["contact_no"].ToString());
item.SubItems.Add(customer["transac_Date"].ToString());
listView1.Items.Add(item);
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.ExitThread();
}
}
private void search_Lname()
{
listView1.Items.Clear();
MySqlCommand src_id_Cmd = new MySqlCommand("SELECT * FROM `customer_table`
WHERE lastname like '" + txtsrc.Text + "%'", connect);
try
{
MySqlDataAdapter src_id_Adt = new MySqlDataAdapter(src_id_Cmd);
DataTable src_id_tbl = new DataTable();
src_id_Adt.Fill(src_id_tbl);
foreach (DataRow customer in src_id_tbl.Rows)
{
ListViewItem item = new
ListViewItem(customer["costumer_no"].ToString());
item.SubItems.Add(customer["customer_id"].ToString());
item.SubItems.Add(customer["lastname"].ToString());
item.SubItems.Add(customer["firsname"].ToString());
item.SubItems.Add(customer["mname"].ToString());
item.SubItems.Add(customer["barangay"].ToString());
item.SubItems.Add(customer["municipality"].ToString());
item.SubItems.Add(customer["province"].ToString());
item.SubItems.Add(customer["sex"].ToString());
item.SubItems.Add(customer["cred_status"].ToString());
item.SubItems.Add(customer["contact_no"].ToString());
item.SubItems.Add(customer["transac_Date"].ToString());
listView1.Items.Add(item);
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.ExitThread();
}
}
private void search_Municipal()
{
listView1.Items.Clear();
MySqlCommand src_id_Cmd = new MySqlCommand("SELECT * FROM `customer_table`
WHERE municipality like '" + txtsrc.Text + "%'", connect);
try
{
MySqlDataAdapter src_id_Adt = new MySqlDataAdapter(src_id_Cmd);
DataTable src_id_tbl = new DataTable();
src_id_Adt.Fill(src_id_tbl);
foreach (DataRow customer in src_id_tbl.Rows)
{
ListViewItem item = new
ListViewItem(customer["costumer_no"].ToString());
item.SubItems.Add(customer["customer_id"].ToString());
item.SubItems.Add(customer["lastname"].ToString());
item.SubItems.Add(customer["firsname"].ToString());
item.SubItems.Add(customer["mname"].ToString());
item.SubItems.Add(customer["barangay"].ToString());
item.SubItems.Add(customer["municipality"].ToString());
item.SubItems.Add(customer["province"].ToString());
item.SubItems.Add(customer["sex"].ToString());
item.SubItems.Add(customer["cred_status"].ToString());
item.SubItems.Add(customer["contact_no"].ToString());
item.SubItems.Add(customer["transac_Date"].ToString());
listView1.Items.Add(item);
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.ExitThread();
}
}
private void label11_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you want to exit?",
"Logout", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Form1 frm_login = new Form1();
frm_login.Show();
this.Hide();
}
}
//BUTTON ADD
private void button3_Click(object sender, EventArgs e)
{
string id = txt_Id.Text;
string lname = txtlname.Text;
string fname = txtfname.Text;
string mname = txtmname.Text;
string brgy = txtbrgy.Text;
string mun = txtmun.Text;
string prov = txtprov.Text;
string sex = cboSex.Text;
string cred_sts = cboCredit.Text;
string contact = txtCon.Text;
string date = txtDate.Value.Date.ToString("yyyMMdd");
if (id == "" || lname == "" || fname == "" || mname == "" || brgy == "" ||
mun == "" || prov == "" || sex == "" || cred_sts == "" || contact == "" || date == "")
{
MessageBox.Show("You must fill in all the fields!", "error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
try
{
reg.insert_Customer(id, lname, fname, mname, brgy, mun, prov, sex,
cred_sts, contact, date);
MessageBox.Show("Customer succesfully saved!", "Succesfull",
MessageBoxButtons.OK, MessageBoxIcon.Information);
show_Customer();
refresh();
total_Record();
customer_Id();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
if(listView1.SelectedItems.Count > 0)
{
ListViewItem customer = listView1.SelectedItems[0];
lblcustNo.Text = customer.SubItems[0].Text;
txt_Id.Text = customer.SubItems[1].Text;
txtlname.Text = customer.SubItems[2].Text;
txtfname.Text = customer.SubItems[3].Text;
txtmname.Text = customer.SubItems[4].Text;
txtbrgy.Text = customer.SubItems[5].Text;
txtmun.Text = customer.SubItems[6].Text;
txtprov.Text = customer.SubItems[7].Text;
cboSex.Text = customer.SubItems[8].Text;
cboCredit.Text = customer.SubItems[9].Text;
txtCon.Text = customer.SubItems[10].Text;
}
}
//BUTTON REFRESH
private void button2_Click(object sender, EventArgs e)
{
refresh();
total_Record();
show_Customer();
}
//BUTTON UPDATE
private void button4_Click(object sender, EventArgs e)
{
string id = txt_Id.Text;
string lname = txtlname.Text;
string fname = txtfname.Text;
string mname = txtmname.Text;
string brgy = txtbrgy.Text;
string mun = txtmun.Text;
string prov = txtprov.Text;
string sex = cboSex.Text;
string cred_sts = cboCredit.Text;
string contact = txtCon.Text;
string date = txtDate.Value.Date.ToString("yyyMMdd");
string no = lblcustNo.Text;
if (id == "" || lname == "" || fname == "" || mname == "" || brgy == "" ||
mun == "" || prov == "" || sex == "" || cred_sts == "" || contact == "" || date == "")
{
MessageBox.Show("You must fill in all the fields!", "error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
try
{
reg.update_Customer(id, lname, fname, mname, brgy, mun, prov, sex,
cred_sts, contact, date, no);
MessageBox.Show("Customer succesfully Updated!", "Succesfull",
MessageBoxButtons.OK, MessageBoxIcon.Information);
refresh();
show_Customer();
total_Record();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
//BUTTON REMOVE
private void button5_Click(object sender, EventArgs e)
{
string no = lblcustNo.Text;
string id = txt_Id.Text;
string lname = txtlname.Text;
string fname = txtfname.Text;
string mname = txtmname.Text;
string brgy = txtbrgy.Text;
string mun = txtmun.Text;
string prov = txtprov.Text;
string sex = cboSex.Text;
string cred_sts = cboCredit.Text;
string contact = txtCon.Text;
string date = txtDate.Value.Date.ToString("yyyMMdd");
if (id == "" || lname == "" || fname == "" || mname == "" || brgy == "" ||
mun == "" || prov == "" || sex == "" || cred_sts == "" || contact == "" || date == "")
{
MessageBox.Show("You must fill in all the fields!", "error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
try
{
reg.delete_Customer(no);
MessageBox.Show("Customer succesfully deleted!", "Succesfull",
MessageBoxButtons.OK, MessageBoxIcon.Information);
refresh();
total_Record();
show_Customer();
customer_Id();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
//BUTTON SEARCH
private void button1_Click(object sender, EventArgs e)
{
if(cbo_src_by.Text == "" || txtsrc.Text == "")
{
MessageBox.Show("Empty Fields", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else if (cbo_src_by.Text == "Customer Id")
{
search_Id();
}
else if (cbo_src_by.Text == "Lastname")
{
search_Lname();
}
else if (cbo_src_by.Text == "Munipality")
{
search_Municipal();
}
}
}