// This method calls the UpdateBusiness() method from DbConn class updating values to the table // private void btnSave_Click(object sender, EventArgs e) { int index = Int32.Parse(dGVBusinessRecords.SelectedCells[0].Value.ToString()); // businessContact object instance of the class BusinessContact to populate the class fields // BusinessContact businessContact = new BusinessContact(); businessContact.ContactID = index; businessContact.ContactFname = tbFname.Text; businessContact.ContactLname = tbLname.Text; businessContact.ContactEmail = tbEmail.Text; businessContact.ContactAddr1 = tbAddress1.Text; businessContact.ContactAddr2 = tbAddress2.Text; businessContact.ContactCity = tbCity.Text; businessContact.ContactPostCode = tbPostCode.Text; businessContact.BusinessTel = tbTelNumber.Text; // UpdateBuiness method called from DbConn class with the parameter businessContact and its populated fields // dbConn.UpdateBusiness(businessContact); // Repopulate datagridview by calling the GetAllBusiness() method from DbConn class dGVBusinessRecords.DataSource = dbConn.GetAllBusiness(); tbFname.Enabled = false; tbLname.Enabled = false; tbEmail.Enabled = false; tbAddress1.Enabled = false; tbAddress2.Enabled = false; tbCity.Enabled = false; tbPostCode.Enabled = false; tbTelNumber.Enabled = false; btnUpdate.Enabled = true; btnDelete.Enabled = true; btnAddNew.Enabled = true; btnSave.Enabled = false; }
// This method calls the InsertBusiness() method from DbConn class adding new values to the table // private void btnSaveNew_Click(object sender, EventArgs e) { // businessContact object instance of the class BusinessContact to populate the class fields // BusinessContact businessContact = new BusinessContact(); businessContact.ContactFname = tbFname.Text; businessContact.ContactLname = tbLname.Text; businessContact.ContactEmail = tbEmail.Text; businessContact.ContactAddr1 = tbAddress1.Text; businessContact.ContactAddr2 = tbAddress2.Text; businessContact.ContactCity = tbCity.Text; businessContact.ContactPostCode = tbPostCode.Text; businessContact.BusinessTel = tbTelNumber.Text; // InsertBusiness() method called with the populated fields from BusinessContact clas dbConn.InsertBusiness(businessContact); // datagridview repopulated with the new data from SQL table // dGVBusinessRecords.DataSource = dbConn.GetAllBusiness(); tbFname.Enabled = false; tbLname.Enabled = false; tbEmail.Enabled = false; tbAddress1.Enabled = false; tbAddress2.Enabled = false; tbCity.Enabled = false; tbPostCode.Enabled = false; tbTelNumber.Enabled = false; // text boxes emptied tbFname.Text = String.Empty; tbLname.Text = String.Empty; tbEmail.Text = String.Empty; tbAddress1.Text = String.Empty; tbAddress2.Text = String.Empty; tbCity.Text = String.Empty; tbPostCode.Text = String.Empty; tbTelNumber.Text = String.Empty; btnSave.Enabled = false; btnAddNew.Enabled = true; btnSaveNew.Enabled = false; btnUpdate.Enabled = true; btnDelete.Enabled = true; }
// This method calls the insertBusiness() store procedure using the businessContact object // as parameter for the method and its values from each field public async void InsertBusiness(BusinessContact businessContact) { using (var conn = new MySqlConnection(connString)) { await conn.OpenAsync(); using (var cmd = new MySqlCommand()) { cmd.Connection = conn; cmd.CommandText = "CALL insertBusiness(@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8);"; cmd.Parameters.AddWithValue("p1", businessContact.ContactFname); cmd.Parameters.AddWithValue("p2", businessContact.ContactLname); cmd.Parameters.AddWithValue("p3", businessContact.ContactEmail); cmd.Parameters.AddWithValue("p4", businessContact.ContactAddr1); cmd.Parameters.AddWithValue("p5", businessContact.ContactAddr2); cmd.Parameters.AddWithValue("p6", businessContact.ContactCity); cmd.Parameters.AddWithValue("p7", businessContact.ContactPostCode); cmd.Parameters.AddWithValue("p8", businessContact.BusinessTel); await cmd.ExecuteNonQueryAsync(); } } }