Laboratory Activity Add Edit Delete Search

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

LABORATORY ACTIVITY: INSERT(ADD), UPDATE, DELETE, SEARCH ON C# WITH MYSQL

1. Create a database customerdb and follow the table structure.

2. Create a GUI on your C# programming language and place the following objects.

3. Perform the following syntax:


using MySql.Data.MySqlClient;

namespace customerdb
{
public partial class main_form : Form
{

registration reg = new registration();


public MySqlConnection connect = new
MySqlConnection("Server=localhost;username=root;password=;database=customerdb");
public main_form()
{
InitializeComponent();
}
private void main_form_Load(object sender, EventArgs e)
{
try
{
connect.Open();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
show_Customer();
total_Record();
customer_Id();

private void total_Record()


{
foreach(ListViewItem rec in listView1.Items)
{
lblrecords.Text = listView1.Items.Count.ToString();
}
}
private void refresh()
{
txt_Id.Text = "";
txtlname.Text = "";
txtfname.Text = "";
txtmname.Text= "";
txtbrgy.Text = "";
txtmun.Text ="";
txtprov.Text = "";
cboSex.Text = "";
cboCredit.Text = "";
txtCon.Text = "";
lblcustNo.Text = "";
}
private void show_Customer()
{
listView1.Items.Clear();
MySqlCommand cust_Cmd = new MySqlCommand("SELECT * FROM
customer_table",connect);
try
{
MySqlDataAdapter cust_adp = new MySqlDataAdapter(cust_Cmd);
DataTable cust_data = new DataTable();
cust_adp.Fill(cust_data);
foreach (DataRow customer in cust_data.Rows)
{
ListViewItem cust = new ListViewItem(customer[0].ToString());
for (int i = 1; i < 12; i++)
{
cust.SubItems.Add(customer[i].ToString());
}
listView1.Items.Add(cust);
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}

}
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);
}
}
}

private void listView1_SelectedIndexChanged(object sender, EventArgs e)


{

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();
}
}
}

You might also like