// Update customer public bool UpdateCustomerInformation(CustomerDataTbl data) { SqlCommand cmd = DataBaseConnection.OpenConnection().CreateCommand(); cmd.CommandText = "update customer set firstName=@Fname,lastname=@Lname,fathername=@fathername, address=@address, phone=@phone where custId=@custId"; cmd.Parameters.AddWithValue("@custId", data.CustId); cmd.Parameters.AddWithValue("@Fname", data.FirstName); cmd.Parameters.AddWithValue("@Lname", data.LastName); cmd.Parameters.AddWithValue("@fathername", data.fatherName); cmd.Parameters.AddWithValue("@address", data.Address); cmd.Parameters.AddWithValue("@phone", data.Phone); bool ans = cmd.ExecuteNonQuery() > 0; cmd.Dispose(); return(ans); }
// to add customer information public bool AddCustomerData(CustomerDataTbl data) { SqlCommand cmd = DataBaseConnection.OpenConnection().CreateCommand(); cmd.CommandText = "AddCustInfo"; // stored procedure cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@FirstName", data.FirstName); cmd.Parameters.AddWithValue("@LastName", data.LastName); cmd.Parameters.AddWithValue("@fathername", data.fatherName); cmd.Parameters.AddWithValue("@Address", data.Address); cmd.Parameters.AddWithValue("@Phone", data.Phone); bool ans = cmd.ExecuteNonQuery() > 0; cmd.Dispose(); return(ans); }
// Show Customer Information public CustomerDataTbl ShowCustomerFromID(int CustId) { SqlCommand cmd = DataBaseConnection.OpenConnection().CreateCommand(); cmd.CommandText = "Select * from customer where custid=@CustId"; cmd.Parameters.AddWithValue("@CustId", CustId); CustomerDataTbl data = null; SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { data = new CustomerDataTbl(); data.FirstName = reader[1].ToString(); data.LastName = reader[2].ToString(); data.fatherName = reader[3].ToString(); data.Address = reader[4].ToString(); data.Phone = reader[5].ToString(); } reader.Dispose(); cmd.Dispose(); return(data); }
//Update Customer table data private void UpdateCustomer_Click(object sender, EventArgs e) { try { string custId = tbxCustomerID.Text; string FirstName, LastName, fatherName, address, phone; LastName = textBoxLastName.Text; FirstName = TextBoxFirstName.Text; address = textBoxAddress.Text; phone = textBoxPhone.Text; fatherName = textBox1FatherName.Text; if (FirstName == "") { MessageBox.Show("First name required!"); TextBoxFirstName.Focus(); } else if (LastName == "") { MessageBox.Show("Last name required!"); textBoxLastName.Focus(); } else if (fatherName == "") { MessageBox.Show("Father's name required!"); textBox1FatherName.Focus(); } else if (address == "") { MessageBox.Show("Address required!"); textBoxAddress.Focus(); } else if (phone == "") { MessageBox.Show("Phone number required!"); textBoxPhone.Focus(); } else { CustomerDataTbl data = new CustomerDataTbl(); data.FirstName = FirstName; data.LastName = LastName; data.Address = address; data.Phone = phone; data.fatherName = fatherName; data.CustId = int.Parse(custId); if (new Customer().UpdateCustomerInformation(data)) { AddCustomer.Enabled = true; BindDdlWithCustomer(); // Bind comboBox with Customer table BindGridCustomersTbl(); // BindGridView with Customer table MessageBox.Show("Customer Data Updated!"); TextBoxFirstName.Text = ""; textBoxLastName.Text = ""; textBoxAddress.Text = ""; textBox1FatherName.Text = ""; textBoxPhone.Text = ""; tbxCustomerID.Text = ""; } else { MessageBox.Show("Unable to update this customer!!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); DataBaseConnection.CloseConnection(); // close connection with database when error occured } }
//Add Customers to database private void AddCustomer_Click(object sender, EventArgs e) { try { string Firstname, Lastname, address, phone, fatherName; Firstname = TextBoxFirstName.Text; Lastname = textBoxLastName.Text; address = textBoxAddress.Text; phone = textBoxPhone.Text; fatherName = textBox1FatherName.Text; if (Firstname == "") { MessageBox.Show("First name required!"); TextBoxFirstName.Focus(); } else if (Lastname == "") { MessageBox.Show("Last name required!"); textBoxLastName.Focus(); } else if (fatherName == "") { MessageBox.Show("Father's name required!"); textBox1FatherName.Focus(); } else if (address == "") { MessageBox.Show("Address required!"); textBoxAddress.Focus(); } else if (phone == "") { MessageBox.Show("Phone number required!"); textBoxPhone.Focus(); } else { CustomerDataTbl data = new CustomerDataTbl(); data.FirstName = Firstname; data.LastName = Lastname; data.fatherName = fatherName; data.Address = address; data.Phone = phone; if (new Customer().AddCustomerData(data)) { BindDdlWithCustomer(); // bind customer table to combobox BindGridCustomersTbl(); // Bind GridView with Customer table MessageBox.Show("Customer Saved to Database!!"); TextBoxFirstName.Text = ""; textBoxLastName.Text = ""; textBoxAddress.Text = ""; textBox1FatherName.Text = ""; textBoxPhone.Text = ""; } else { MessageBox.Show("Unable to add this customer!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); DataBaseConnection.CloseConnection(); // close DB connection when error occured } }