public async void updatePContacts(PersonalContacts personalContact) { using (var conn = new MySqlConnection(sConn)) { await conn.OpenAsync(); using (var callCommand = new MySqlCommand()) /* * The difference between this method and the insertIntoPersonalContacts(); * is that we pass through the "first" paramater ID so that the seleted row identified can be changed when the information is matched to the ID of that row * Taking a look at Heidi SQL will show this. */ { callCommand.Connection = conn; callCommand.CommandText = "CALL updatePersonalContacts(@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10);"; callCommand.Parameters.AddWithValue("p1", personalContact.ContactID); callCommand.Parameters.AddWithValue("p2", personalContact.cFirstName); callCommand.Parameters.AddWithValue("p3", personalContact.cSecondName); callCommand.Parameters.AddWithValue("p4", personalContact.cEmail); callCommand.Parameters.AddWithValue("p5", personalContact.cContactTel); callCommand.Parameters.AddWithValue("p6", personalContact.pContactTel); callCommand.Parameters.AddWithValue("p7", personalContact.pAddress1); callCommand.Parameters.AddWithValue("p8", personalContact.pAddress2); callCommand.Parameters.AddWithValue("p9", personalContact.pCity); callCommand.Parameters.AddWithValue("p10", personalContact.aPostCode); await callCommand.ExecuteNonQueryAsync(); } } }
public async void insertPContacts(PersonalContacts personalContact) // create an asynchronous method which we call the class and create an object which can access the attributes of the class { using (var conn = new MySqlConnection(sConn)) { await conn.OpenAsync(); using (var callCommand = new MySqlCommand()) { /* After establishing a connection to the mysql server, * We pass the sql command insertIntoPersonal with the paramater we created with the command in HeidiSQL * The paramaters are set to the initial parameters we called in reading the databae in the datagridview * */ callCommand.Connection = conn; callCommand.CommandText = "CALL insertIntoPersonal(@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9);"; callCommand.Parameters.AddWithValue("p1", personalContact.cFirstName); callCommand.Parameters.AddWithValue("p2", personalContact.cSecondName); callCommand.Parameters.AddWithValue("p3", personalContact.cEmail); callCommand.Parameters.AddWithValue("p4", personalContact.cContactTel); callCommand.Parameters.AddWithValue("p5", personalContact.pContactTel); callCommand.Parameters.AddWithValue("p6", personalContact.pAddress1); callCommand.Parameters.AddWithValue("p7", personalContact.pAddress2); callCommand.Parameters.AddWithValue("p8", personalContact.pCity); callCommand.Parameters.AddWithValue("p9", personalContact.aPostCode); await callCommand.ExecuteNonQueryAsync(); //execute query/command. } } }
private void SaveNewButton_Click(object sender, EventArgs e) { /* * This method creats an object that allows the code to access the PersonalContact class * Which then we set the textfield of the textbox's to objects in the class such as firstname, secondname and so on. */ PersonalContacts pContact = new PersonalContacts(); pContact.cFirstName = fname_Textbox.Text; pContact.cSecondName = sname_Textbox.Text; pContact.cEmail = cemail_Textbox.Text; pContact.cContactTel = cnumber_Textbox.Text; pContact.pContactTel = pnumber_Textbox.Text; pContact.pAddress1 = fAddress_Textbox.Text; pContact.pAddress2 = sAddress_Textbox.Text; pContact.pCity = city_Textbox.Text; pContact.aPostCode = postcode_Textbox.Text; DbConn.insertPContacts(pContact); //object passed through the insertPContacts method from the DbConn. // Sets the properties of the buttons on the form to either on or off SaveNewButton.Enabled = false; addNewButton.Enabled = true; updateButton.Enabled = true; deleteButton.Enabled = true; fname_Textbox.Enabled = false; sname_Textbox.Enabled = false; cemail_Textbox.Enabled = false; cnumber_Textbox.Enabled = false; pnumber_Textbox.Enabled = false; fAddress_Textbox.Enabled = false; sAddress_Textbox.Enabled = false; city_Textbox.Enabled = false; postcode_Textbox.Enabled = false; // Clears the text inside the textbox fields. fname_Textbox.Text = String.Empty; sname_Textbox.Text = String.Empty; cemail_Textbox.Text = String.Empty; cnumber_Textbox.Text = String.Empty; pnumber_Textbox.Text = String.Empty; fAddress_Textbox.Text = String.Empty; sAddress_Textbox.Text = String.Empty; city_Textbox.Text = String.Empty; postcode_Textbox.Text = String.Empty; pContactsDataGrid.DataSource = DbConn.getPContacts(); }
private void saveSelectedButton_Click(object sender, EventArgs e) { int index = Int32.Parse(pContactsDataGrid.SelectedCells[0].Value.ToString()); PersonalContacts pContact = new PersonalContacts(); pContact.ContactID = index; pContact.cFirstName = fname_Textbox.Text; pContact.cSecondName = sname_Textbox.Text; pContact.cEmail = cemail_Textbox.Text; pContact.cContactTel = cnumber_Textbox.Text; pContact.pContactTel = pnumber_Textbox.Text; pContact.pAddress1 = fAddress_Textbox.Text; pContact.pAddress2 = sAddress_Textbox.Text; pContact.pCity = city_Textbox.Text; pContact.aPostCode = postcode_Textbox.Text; DbConn.updatePContacts(pContact); pContactsDataGrid.DataSource = DbConn.getPContacts(); SaveNewButton.Enabled = false; saveSelectedButton.Enabled = false; addNewButton.Enabled = true; updateButton.Enabled = true; deleteButton.Enabled = true; fname_Textbox.Enabled = false; sname_Textbox.Enabled = false; cemail_Textbox.Enabled = false; cnumber_Textbox.Enabled = false; pnumber_Textbox.Enabled = false; fAddress_Textbox.Enabled = false; sAddress_Textbox.Enabled = false; city_Textbox.Enabled = false; postcode_Textbox.Enabled = false; fname_Textbox.Text = String.Empty; sname_Textbox.Text = String.Empty; cemail_Textbox.Text = String.Empty; cnumber_Textbox.Text = String.Empty; pnumber_Textbox.Text = String.Empty; fAddress_Textbox.Text = String.Empty; sAddress_Textbox.Text = String.Empty; city_Textbox.Text = String.Empty; postcode_Textbox.Text = String.Empty; }