private void btn_create_Click(object sender, EventArgs e) { //add new record DBtest prep = new DBtest(txt_fname.Text, txt_mname.Text, txt_lname.Text); prep.Add(); MessageBox.Show("Insert Successfully"); display(); }
private void btn_delete_Click(object sender, EventArgs e) { int id = Int32.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()); DBtest del = DBtest.GetDataID(id); del.Delete(); display(); }
private void btn_update_Click(object sender, EventArgs e) { //edit record int id = Int32.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()); DBtest rec = DBtest.GetDataID(id); rec.Firstname = txt_fname.Text; rec.Middlename = txt_lname.Text; rec.Lastname = txt_mname.Text; rec.Update(); MessageBox.Show("Updated Successfully"); display(); }
public void display() { try { BindingSource bindingsource = new BindingSource(); List <DBtest> rec = DBtest.GetData(); foreach (DBtest data in rec) { bindingsource.Add(data); } this.dataGridView1.Refresh(); this.dataGridView1.DataSource = bindingsource; } catch (Exception ex) { ex.ToString(); } }
public static List <DBtest> GetData() { List <DBtest> data = new List <DBtest>(); MySqlConnection con = DBConnection.ConnectDatabase(); try { MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + tablename, con); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { DBtest rawData = new DBtest(); rawData.Id = reader.GetInt32(0); rawData.Firstname = reader.GetString(1); rawData.Middlename = reader.GetString(2); rawData.Lastname = reader.GetString(3); data.Add(rawData); } } reader.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } return(data); }
//getdatabyID public static DBtest GetDataID(int id) { DBtest data = null; MySqlConnection con = DBConnection.ConnectDatabase(); try { MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + tablename + " WHERE id=" + id, con); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { reader.Read(); data = new DBtest(); data.Id = reader.GetInt32(0); data.Firstname = reader.GetString(1); data.Middlename = reader.GetString(2); data.Lastname = reader.GetString(3); } reader.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); MessageBox.Show(ex.ToString()); } finally { con.Close(); } return(data); }