//Add loyalty card customer private void btn_loy_add_Click(object sender, EventArgs e) { CustomerObject co = new CustomerObject(); SqlConnection con = ConnectionManager.GetConnection(); if (con.State.ToString() == "Closed") { con.Open(); } if (CustomerLoyaltyCardValidation()) { try { SqlCommand cmd = con.CreateCommand(); string nic = "select NIC from Customer where NIC = '" + txt_loy_nic.Text + "'"; cmd.CommandText = nic; SqlDataReader rd = cmd.ExecuteReader(); rd.Read(); bool result1 = rd.HasRows; if (!result1) { MessageBox.Show("This customer is not registered customer cannot issue a loyalty card"); } else { if (CustomerLoyaltyCardValidation()) { CustomerObject objCustomer = new CustomerObject(); objCustomer.Nic = txt_loy_nic.Text; objCustomer.CardType = cmb_loy_cardtype.SelectedItem.ToString(); objCustomer.CardNo = lbl_loymem_cardno.Text; // objCustomer.CardPoints = Convert.ToInt32(txtb_loy_cardpoints.Text); objCustomer.CardPoints = 0; //show the confirmation dialog box DialogResult dr; dr = MessageBox.Show("Do you want to save the record", "Confirm", MessageBoxButtons.YesNo); if (dr.ToString() == "Yes") { try { MegaCoolMethods mcm = new MegaCoolMethods(); bool result = mcm.AddLoyaltyCustomer(objCustomer); if (result) { MessageBox.Show("Successfully Saved", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Unable to Save", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (ApplicationException appEx) { MessageBox.Show(appEx.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (SqlException ex) { if (ex.Number == 2627) { MessageBox.Show("This loyalty customer is alreay exist, please add new customer "); //Violation of primary key. Handle Exception } } } else { MessageBox.Show("Record is not saved", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } catch (SqlException ex) { throw ex; } } CustomerLotaltyCardFillGrid(); Clear(); CustomerFillGrid(); tb_cus_tabcontroller.SelectedTab = tab_customermanagement; }