protected void btnSave_Click(object sender, EventArgs e) { if (Page.IsValid) { try { Audit audit = new Audit(); string ctgCode = ""; if (!string.IsNullOrEmpty(txtctgCode.Text) && !string.IsNullOrEmpty(txtctgName.Text) && !string.IsNullOrEmpty(Txtlistorder.Text)) { int chkgprs = 0; string defaultimg = ""; string largeimg = ""; string dbdefaultimg = ""; string dblargeimg = ""; ctgCode = txtctgCode.Text.ToString(); db = new LinqToSqlDataContext(); try { //if updating category and delete all the mapped arc's and products of this category. #region existingCategory if (Session[enumSessions.CategoryId.ToString()] != null) { var CatInfo = (from cat in db.Categories where cat.CategoryId == Convert.ToInt32(Session[enumSessions.CategoryId.ToString()]) select cat).SingleOrDefault(); if (FileUpload1.HasFile) { string filenamewithoutext = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName); string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName); string ff = filenamewithoutext + txtctgCode.Text.ToString() + ext; defaultimg = "../images/" + ff; FileUpload1.SaveAs(Server.MapPath(defaultimg)); dbdefaultimg = "images/" + ff; } else { dbdefaultimg = CatInfo.DefaultImage; } if (FileUpload2.HasFile) { string filenamewithoutext = System.IO.Path.GetFileNameWithoutExtension(FileUpload2.PostedFile.FileName); string ext = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName); string ff = filenamewithoutext + txtctgCode.Text.ToString() + ext; largeimg = "../images/" + ff; FileUpload2.SaveAs(Server.MapPath(largeimg)); dblargeimg = "images/" + ff; } else { dblargeimg = CatInfo.LargeImage; } if (CheckBoxgprschip.Checked == true) { chkgprs = 1; } else { chkgprs = 0; } var catg = db.CreateCategory(Convert.ToInt32(Session[enumSessions.CategoryId.ToString()].ToString()), txtctgCode.Text.ToString(), txtctgName.Text.ToString(), txtctgdesc.Text.ToString(), dbdefaultimg, dblargeimg, SiteUtility.GetUserName(), Convert.ToBoolean(chkgprs), Convert.ToInt32(Txtlistorder.Text.ToString()), chkIsDeleted.Checked, txtSalesType.Text).SingleOrDefault(); if (catg != null) { Session[enumSessions.CategoryId.ToString()] = catg.CategoryId; } if (catg.CategoryId == 0) { string script = "alertify.alert('" + ltrDuplCat.Text + "');"; ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true); MaintainScrollPositionOnPostBack = false; } else { string script = "alertify.alert('Category [" + txtctgCode.Text + "] - " + txtctgName.Text + " updated successfully.');"; ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true); MaintainScrollPositionOnPostBack = false; } audit.Notes = "CategoryId: " + Convert.ToInt32(Session[enumSessions.CategoryId.ToString()].ToString()) + ", Code: " + txtctgCode.Text.ToString() + ", Name: " + txtctgName.Text.ToString() + ", Description: " + txtctgdesc.Text.ToString() + ", DefaultImg: " + dbdefaultimg + ", LargeImg: " + dblargeimg + ", Allow GPRS: " + Convert.ToBoolean(chkgprs) + ", List Order: " + Convert.ToInt32(Txtlistorder.Text.ToString()) + ", Is Deleted: " + chkIsDeleted.Checked + ", Sales Types: " + txtSalesType.Text; } #endregion #region newCategory else { string filenamewithoutext1 = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName); string ext1 = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName); string ff1 = filenamewithoutext1 + txtctgCode.Text.ToString() + ext1; string filenamewithoutext2 = System.IO.Path.GetFileNameWithoutExtension(FileUpload2.PostedFile.FileName); string ext2 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName); string ff2 = filenamewithoutext2 + txtctgCode.Text.ToString() + ext2; defaultimg = ff1; largeimg = ff2; string defImgPath = "images/" + defaultimg; string LImgPath = "images/" + largeimg; if (FileUpload1.HasFile) { FileUpload1.SaveAs(Server.MapPath("../images/" + defaultimg)); } if (FileUpload2.HasFile) { FileUpload2.SaveAs(Server.MapPath("../images/" + largeimg)); } if (CheckBoxgprschip.Checked == true) { chkgprs = 1; } else { chkgprs = 0; } var cats = db.CreateCategory(0, txtctgCode.Text.ToString().Trim(), txtctgName.Text.ToString().Trim(), txtctgdesc.Text.ToString().Trim(), defImgPath.ToString().Trim(), LImgPath.ToString().Trim(), SiteUtility.GetUserName(), Convert.ToBoolean(chkgprs), Convert.ToInt32(Txtlistorder.Text.ToString()), chkIsDeleted.Checked, txtSalesType.Text).Single(); if (cats != null) { Session[enumSessions.CategoryId.ToString()] = cats.CategoryId; } if (cats.CategoryId == 0) { string script = "alertify.alert('" + ltrDuplCat.Text + "');"; ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true); MaintainScrollPositionOnPostBack = false; } else { string script = "alertify.alert('Category [" + txtctgCode.Text + "] - " + txtctgName.Text + " created successfully.');"; ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true); MaintainScrollPositionOnPostBack = false; } audit.Notes = "CategoryId: " + cats.CategoryId.ToString() + ", Code: " + txtctgCode.Text.ToString() + ", Name: " + txtctgName.Text.ToString() + ", Description: " + txtctgdesc.Text.ToString() + ", DefaultImg: " + dbdefaultimg + ", LargeImg: " + dblargeimg + ", Allow GPRS: " + Convert.ToBoolean(chkgprs) + ", List Order: " + Convert.ToInt32(Txtlistorder.Text.ToString()) + ", Is Deleted: " + chkIsDeleted.Checked + ", Sales Types: " + txtSalesType.Text; } #endregion } catch (Exception exp) { db = new CSLOrderingARCBAL.LinqToSqlDataContext(); db.USP_SaveErrorDetails(Request.Url.ToString(), "btnSave_Click", Convert.ToString(exp.Message), Convert.ToString(exp.InnerException), Convert.ToString(exp.StackTrace), "", HttpContext.Current.Request.UserHostAddress, false, SiteUtility.GetUserName()); } //enter all the checked products of this category audit.Notes += "ProductID: "; List <Category_Product_Map> categoryProductDbRows = new List <Category_Product_Map>(); foreach (GridViewRow ctgrow in gvctg.Rows) { if (ctgrow.RowType == DataControlRowType.DataRow) { GridView innergrid = ctgrow.FindControl("gvinner") as GridView; foreach (GridViewRow prodrow in innergrid.Rows) { if (prodrow.RowType == DataControlRowType.DataRow) { CheckBox chkpro = prodrow.FindControl("chkprod") as CheckBox; Category_Product_Map cmp; if (chkpro.Checked == true) { int Prodid = int.Parse(innergrid.DataKeys[prodrow.RowIndex].Value.ToString()); cmp = new Category_Product_Map(); cmp.CategoryId = Convert.ToInt32(Session[enumSessions.CategoryId.ToString()]); cmp.ProductId = Convert.ToInt32(Prodid); var count = categoryProductDbRows.Where(x => x.CategoryId == cmp.CategoryId && x.ProductId == cmp.ProductId).Count(); if (count <= 0) { categoryProductDbRows.Add(cmp); } audit.Notes += Prodid + ", "; } } } } } db.Category_Product_Maps.InsertAllOnSubmit(categoryProductDbRows); db.SubmitChanges(); //enter all the checked Arc's of this category audit.Notes += "ARCId: "; var Checkedarc = (from ListItem item in CheckBoxARC.Items where item.Selected select item.Value).ToList(); if (Checkedarc.Any()) { ARC_Category_Map acm; int catid = Convert.ToInt32(Session[enumSessions.CategoryId.ToString()]); List <int> arcs = new List <int>(); foreach (String chkarc in Checkedarc) { if (chkarc != "Select All") { acm = new ARC_Category_Map(); acm.CategoryId = catid; acm.ARCId = Convert.ToInt32(chkarc); if (arcs.IndexOf(acm.ARCId) < 0) { db.ARC_Category_Maps.InsertOnSubmit(acm); db.SubmitChanges(); } arcs.Add(acm.ARCId); audit.Notes += chkarc + ","; } } } pnlcategorydetail.Visible = false; pnlcategorylist.Visible = true; LoadData(); } else { string script = "alertify.alert('" + ltrRequired.Text + "');"; ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true); } MaintainScrollPositionOnPostBack = false; audit.UserName = Session[enumSessions.User_Name.ToString()].ToString(); audit.ChangeID = Convert.ToInt32(enumAudit.Manage_Category); audit.CreatedOn = DateTime.Now; if (Request.ServerVariables["LOGON_USER"] != null) { audit.WindowsUser = Request.ServerVariables["LOGON_USER"]; } audit.IPAddress = Request.UserHostAddress; db.Audits.InsertOnSubmit(audit); db.SubmitChanges(); } catch (Exception objException) { db = new CSLOrderingARCBAL.LinqToSqlDataContext(); db.USP_SaveErrorDetails(Request.Url.ToString(), "btnSave_Click", Convert.ToString(objException.Message), Convert.ToString(objException.InnerException), Convert.ToString(objException.StackTrace), "", HttpContext.Current.Request.UserHostAddress, false, Convert.ToString(HttpContext.Current.Session[enumSessions.User_Id.ToString()])); } } else { string script = "alertify.alert('" + ltrRequired.Text + "');"; ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true); MaintainScrollPositionOnPostBack = false; } }