private void btnSave_Click(object sender, EventArgs e) { if (!ValidateFields()) { return; } SpecialOffer so = new SpecialOffer(); SpecialOfferProduct sp = new SpecialOfferProduct(); try { so.Description = txtDescription.Text; so.SpecialOfferID = m_SpecialOfferID; so.Type = cmbDiscountType.SelectedValue.ToString(); so.DiscountPct = decimal.Parse(txtDiscount.Text); so.StartDate = DateTime.Parse(dtStartDate.Value.ToShortDateString() + " 06:00:00 AM"); so.EndDate = DateTime.Parse(dtEndDate.Value.ToShortDateString() + " 06:00:00 AM"); if (txtMinQuantity.Text.Trim() != String.Empty) { so.MinQty = Int32.Parse(txtMinQuantity.Text); } if (txtMaxQuantity.Text.Trim() != String.Empty) { so.MaxQty = Int32.Parse(txtMaxQuantity.Text); } so.Category = ""; if (so.SpecialOfferID == 0) { //new record so.SpecialOfferID = so.AddSpecialOffer(so); //If there are selected products add them to specialofferproduct table //First delete, then insert. sp.RemoveSpecialOfferProduct(so.SpecialOfferID); foreach (DataGridViewRow row in grdProducts.Rows) { if (bool.Parse(row.Cells[0].Value.ToString()) == true) { sp.ProductID = Int32.Parse(row.Cells[1].Value.ToString()); sp.SpecialOfferID = so.SpecialOfferID; sp.AddSpecialOfferProduct(sp); } } } else { //exsiting record. so.UpdateSpecialOffer(so); sp.RemoveSpecialOfferProduct(so.SpecialOfferID); foreach (DataGridViewRow row in grdProducts.Rows) { if (bool.Parse(row.Cells[0].Value.ToString()) == true) { sp.ProductID = Int32.Parse(row.Cells[1].Value.ToString()); sp.SpecialOfferID = so.SpecialOfferID; sp.AddSpecialOfferProduct(sp); } } } MessageBox.Show("Record saved successfully", "MICS", MessageBoxButtons.OK, MessageBoxIcon.Information); RefreshForm(); } catch (Exception ex) { MessageBox.Show(ex.Message, "MICS", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { so = null; sp = null; } }