private void btnSave_Click(object sender, EventArgs e) { int index = Int32.Parse(dGVPersonalDetails.SelectedCells[0].Value.ToString()); PersonalContact personalContact = new PersonalContact();//create a new instance of personal contact //insert records in text boxes into PersonalContact table in the database personalContact.ContactID = index; personalContact.ContactFname = tbFname.Text; personalContact.ContactLname = tbLname.Text; personalContact.ContactEmail = tbEmail.Text; personalContact.HomeTel = tbHomeTel.Text; personalContact.ContactAddr1 = tbAddress1.Text; personalContact.ContactAddr2 = tbAddress2.Text; personalContact.ContactCity = tbCity.Text; personalContact.ContactPostcode = tbPostcode.Text; dbConn.UpdatePersonal(personalContact); //updates PersonalContacts with the new personal Contact instance dGVPersonalDetails.DataSource = dbConn.GetAllPersonal(); //gets personal contact details in the datagridview //Accessiblity of textboxes tbFname.Enabled = false; tbLname.Enabled = false; tbEmail.Enabled = false; tbHomeTel.Enabled = false; tbAddress1.Enabled = false; tbAddress2.Enabled = false; tbCity.Enabled = false; tbPostcode.Enabled = false; //function of buttons when Save button is clicked btnUpdate.Enabled = true; btnDelete.Enabled = true; btnAddNew.Enabled = true; btnSave.Enabled = false; }
private void btnSaveNew_Click(object sender, EventArgs e) { PersonalContact personalContact = new PersonalContact();//create a new instance of personal contact //insert records present in text boxes into PersonalContact table in the database personalContact.ContactFname = tbFname.Text; personalContact.ContactLname = tbLname.Text; personalContact.ContactEmail = tbEmail.Text; personalContact.HomeTel = tbHomeTel.Text; personalContact.ContactAddr1 = tbAddress1.Text; personalContact.ContactAddr2 = tbAddress2.Text; personalContact.ContactCity = tbCity.Text; personalContact.ContactPostcode = tbPostcode.Text; dbConn.InsertPersonal(personalContact); //inserts new personalcontact records into the database dGVPersonalDetails.DataSource = dbConn.GetAllPersonal(); //gets all personalrecords and shows on the datagridview //Accesiblity of Textboxes when SaveNew button is clicked tbFname.Enabled = false; tbLname.Enabled = false; tbEmail.Enabled = false; tbHomeTel.Enabled = false; tbAddress1.Enabled = false; tbAddress2.Enabled = false; tbCity.Enabled = false; tbPostcode.Enabled = false; //Functions of buttons when SaveNew button is clicked btnUpdate.Enabled = true; btnDelete.Enabled = true; btnSaveNew.Enabled = false; }
//INSERT PERSONAL CONTACTS public async void InsertPersonal(PersonalContact personalContact) { using (var conn = new MySqlConnection(connString)) { await conn.OpenAsync();//open database connection using (var cmd = new MySqlCommand()) { cmd.Connection = conn; cmd.CommandText = "CALL insertPersonal(@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8);";//parameters where data would be inserted using command text cmd.Parameters.AddWithValue("p1", personalContact.ContactFname); cmd.Parameters.AddWithValue("p2", personalContact.ContactLname); cmd.Parameters.AddWithValue("p3", personalContact.ContactEmail); cmd.Parameters.AddWithValue("p4", personalContact.HomeTel); cmd.Parameters.AddWithValue("p5", personalContact.ContactAddr1); cmd.Parameters.AddWithValue("p6", personalContact.ContactAddr2); cmd.Parameters.AddWithValue("p7", personalContact.ContactCity); cmd.Parameters.AddWithValue("p8", personalContact.ContactPostcode); await cmd.ExecuteNonQueryAsync(); } } }
public async void UpdatePersonal(PersonalContact personalContact)//update Personal Contact { using (var conn = new MySqlConnection(connString)) { await conn.OpenAsync(); using (var cmd = new MySqlCommand()) { cmd.Connection = conn; cmd.CommandText = "CALL updatePersonal(@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9);"; //update each parameter through the Contact ID cmd.Parameters.AddWithValue("p1", personalContact.ContactID); //added parameter contact ID cmd.Parameters.AddWithValue("p2", personalContact.ContactFname); cmd.Parameters.AddWithValue("p3", personalContact.ContactLname); cmd.Parameters.AddWithValue("p4", personalContact.ContactEmail); cmd.Parameters.AddWithValue("p5", personalContact.HomeTel); cmd.Parameters.AddWithValue("p6", personalContact.ContactAddr1); cmd.Parameters.AddWithValue("p7", personalContact.ContactAddr2); cmd.Parameters.AddWithValue("p8", personalContact.ContactCity); cmd.Parameters.AddWithValue("p9", personalContact.ContactPostcode); await cmd.ExecuteNonQueryAsync(); } } }