public void saveCustomer(Customer cus) { cmd.CommandText = "INSERT INTO customer(name, address, email, phone, birthday, point)" + " values(@name, @address, @email, @phone, @birthday, @point)"; cmd.Parameters.AddWithValue("@name", cus.getName()); cmd.Parameters.AddWithValue("@address", cus.getAddress()); cmd.Parameters.AddWithValue("@email", cus.getEmail()); cmd.Parameters.AddWithValue("@phone", cus.getPhone()); cmd.Parameters.AddWithValue("@birthday", cus.getBirthday()); cmd.Parameters.AddWithValue("@point", cus.getPoint()); cmd.ExecuteNonQuery(); conn.Close(); }
public void updateCustomer(Customer cus) { cmd.CommandText = " UPDATE customer SET name = @name, address=@address, "+ " email = @email, phone = @phone, birthday=@birthday "+ " where id = @id "; cmd.Parameters.AddWithValue("@name", cus.getName()); cmd.Parameters.AddWithValue("@address", cus.getAddress()); cmd.Parameters.AddWithValue("@email", cus.getEmail()); cmd.Parameters.AddWithValue("@phone", cus.getPhone()); cmd.Parameters.AddWithValue("@birthday", cus.getBirthday()); cmd.Parameters.AddWithValue("@id", cus.getId()); cmd.ExecuteNonQuery(); conn.Close(); }
private void button1_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Are you sure to add a customer?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { name = textBoxName.Text; address = textBoxAddress.Text; email = textBoxEmail.Text; phone = textBoxPhone.Text; birthday = textBoxBirthday.Text; cus = new Customer(name, address, email, phone, birthday, "0"); if (cus.repOK()) { //MessageBox.Show(cus.toString()); cusMan = new CustomerManager(); cusMan.saveCustomer(cus); //write text file //MessageBox.Show(cus.toString()); DateTime now = DateTime.Now; System.IO.StreamWriter file = new System.IO.StreamWriter(@"person.txt", true); file.WriteLine("Add customer{" + cus.toStringInfo() + "}" + " " + now); file.Close(); tableGetData(); MessageBox.Show("Add customer successfully."); } else { MessageBox.Show("Invalid Customer."); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void button3_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Are you sure to delete a customer?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int selectedIndex = table1.CurrentCell.RowIndex; if (selectedIndex >= 0) { String id = table1.Rows[selectedIndex].Cells[0].Value.ToString(); cusMan = new CustomerManager(); cusMan.deleteCustomer(id); //print log cus = new Customer(); DateTime now = DateTime.Now; System.IO.StreamWriter file = new System.IO.StreamWriter(@"person.txt", true); file.WriteLine("Delete customer{" + id + "}" + " " + now); file.Close(); tableGetData(); MessageBox.Show("Delete Customer successfully."); } else MessageBox.Show("Select a customer to delete."); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void button2_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Are you sure to update a customer?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int selectedIndex = table1.CurrentCell.RowIndex; if (selectedIndex >= 0) { String id = table1.Rows[selectedIndex].Cells[0].Value.ToString(); name = textBoxName.Text; address = textBoxAddress.Text; email = textBoxEmail.Text; phone = textBoxPhone.Text; birthday = textBoxBirthday.Text; point = labelPoint.Text; cus = new Customer(id, name, address, email, phone, birthday, point); if (cus.repOK()) { //MessageBox.Show("ID= " + cusID + "\n" + cus.toString()); cusMan = new CustomerManager(); cusMan.updateCustomer(cus); //write text file DateTime now = DateTime.Now; System.IO.StreamWriter file = new System.IO.StreamWriter(@"person.txt", true); file.WriteLine("Update customer{" + cus.toStringInfo() + "}" + " " + now); file.Close(); tableGetData(); MessageBox.Show("Update customer successfully."); } else { MessageBox.Show("Invalid Customer."); } } else MessageBox.Show("Select a customer to update."); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }