//insert contact method public bool InsertContact(tblContacts ct) { //insert statement string query = "INSERT INTO tblContacts(FirstName, LastName, PhoneNumber, EmailAddress, Address) VALUES(@FirstName, @LastName, @PhoneNumber,@EmailAddress, @Address)"; SqlConnection conn = datacon.getConnect(); try { //create a command object comm = new SqlCommand(query, conn); //open the connection conn.Open(); //add parameter @FirstName comm.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = ct.FirstName; //add parameter @LastName comm.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = ct.LastName; //add parameter @PhoneNumber comm.Parameters.Add("@PhoneNumber", SqlDbType.NVarChar).Value = ct.PhoneNumber; //add parameter @EmailAddress comm.Parameters.Add("@EmailAddress", SqlDbType.NVarChar).Value = ct.EmailAddress; //add parameter @Address comm.Parameters.Add("@Address", SqlDbType.NVarChar).Value = ct.Address; //execute command comm.ExecuteNonQuery(); //close connection conn.Close(); } catch (Exception e) { return(false); } return(true); }
//Update Contact Method public bool UpdateContact(tblContacts ct) { //update statement string query = "UPDATE tblContacts SET FirstName = @FirstName, LastName = @LastName, PhoneNumber = @PhoneNumber, EmailAddress = @EmailAddress, Address = @Address WHERE ID = @ID"; SqlConnection conn = datacon.getConnect(); try { //create a command object comm = new SqlCommand(query, conn); //open connection conn.Open(); comm.Parameters.Add("@ID", SqlDbType.Int).Value = ct.ID; comm.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = ct.FirstName; comm.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = ct.LastName; comm.Parameters.Add("@PhoneNumber", SqlDbType.NVarChar).Value = ct.PhoneNumber; comm.Parameters.Add("@EmailAddress", SqlDbType.NVarChar).Value = ct.EmailAddress; comm.Parameters.Add("@Address", SqlDbType.NVarChar).Value = ct.Address; //execute command comm.ExecuteNonQuery(); //close connection conn.Close(); } catch (Exception e) { return(false); } return(true); }
private void button1_Click(object sender, EventArgs e) { if (CheckData()) {//create a new contact table and include the below info tblContacts ct = new tblContacts(); ct.FirstName = txtFirstName.Text.ToUpper(); ct.LastName = txtLastName.Text.ToUpper(); ct.PhoneNumber = txtPhoneNumber.Text; ct.EmailAddress = txtEmailAddress.Text.ToUpper(); ct.Address = txtAddress.Text.ToUpper(); //insert contact if (contactBLL.InsertContact(ct)) { this.DialogResult = DialogResult.OK; } //close addform this.Close(); //show successful message box MessageBox.Show("Added Successfully!"); } //clear text from text boxes from adding a new contact txtFirstName.Text = ""; txtLastName.Text = ""; txtPhoneNumber.Text = ""; txtEmailAddress.Text = ""; txtAddress.Text = ""; }
//when user clicks on Update Button private void Update_Click(object sender, EventArgs e) { if (updateForm.ShowDialog() == DialogResult.OK) { tblContacts ct = new tblContacts(); ct.ID = Id; ct.FirstName = updateForm.txtFirstName.Text.ToUpper(); ct.LastName = updateForm.txtLastName.Text.ToUpper(); ct.PhoneNumber = updateForm.txtPhoneNumber.Text; ct.Address = updateForm.txtAddress.Text.ToUpper(); ct.EmailAddress = updateForm.txtEmailAddress.Text.ToUpper(); ct.Address = updateForm.txtAddress.Text.ToUpper(); //update contact info if (bllContact.UpdateContact(ct)) { //after updating, show new data for all contacts this.ShowAllContacts(); } } }
//Delete Contact Method public bool DeleteContact(tblContacts ct) { //delete statement string sql = "DELETE tblContacts WHERE ID = @ID"; SqlConnection conn = datacon.getConnect(); try { //create a command object comm = new SqlCommand(sql, conn); conn.Open(); comm.Parameters.Add("@ID", SqlDbType.Int).Value = ct.ID; comm.ExecuteNonQuery(); conn.Close(); } catch (Exception e) { return(false); } return(true); }
//when Delete button is clicked private void btnDelete_Click(object sender, EventArgs e) { //prompt user if they are sure they want to delete the contact if (MessageBox.Show("Are You Sure?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { //create new contact table tblContacts ct = new tblContacts(); //use contact ID to delete ct.ID = Id; //delete contact if (bllContact.DeleteContact(ct)) { //after deleting, show all contacts ShowAllContacts(); } else { MessageBox.Show("Error, please try again", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop); } } }
public bool DeleteContact(tblContacts ct) { return(daContacts.DeleteContact(ct)); }
public bool UpdateContact(tblContacts ct) { //return update contact return(daContacts.UpdateContact(ct)); }
public bool InsertContact(tblContacts ct) { //return insert contact return(daContacts.InsertContact(ct)); }