protected void btnSave_Click(object sender, EventArgs e)
    {
        Audit       auditNotes           = new Audit();
        List <Guid> listInstallersMapped = new List <Guid>();;
        List <int>  listArcsMapped       = new List <int>();

        db = new LinqToSqlDataContext();
        try
        {
            if (lstProductsFrom.SelectedIndex > 0 && lstProductsTo.SelectedIndex > 0)
            {
                int productIdFrom = db.Products.Where(x => x.ProductCode == lstProductsFrom.SelectedItem.Text).Select(x => x.ProductId).FirstOrDefault();


                int productIdTo = db.Products.Where(x => x.ProductCode == lstProductsTo.SelectedItem.Text).Select(x => x.ProductId).FirstOrDefault();


                auditNotes.Notes += "product id:" + productIdTo;

                if (chkArcs.Checked || chkInstallers.Checked)
                {
                    if (chkInstallers.Checked)
                    {
                        //Deleting the previous mappings of the Installers with the productTo

                        var prodInstallers = db.Product_Installer_Maps.Where(x => x.ProductId == productIdTo).ToList();
                        foreach (Product_Installer_Map installerMapRecord in prodInstallers)
                        {
                            db.Product_Installer_Maps.DeleteOnSubmit(installerMapRecord);
                        }
                        db.SubmitChanges();

                        //get the list of installers mapped with the productIDFrom

                        listInstallersMapped = (from pim in db.Product_Installer_Maps
                                                where pim.ProductId == productIdFrom
                                                select pim.InstallerId).ToList();
                    }
                    if (chkArcs.Checked)
                    {
                        //Deleting the previous mappings of the ARcs with the productTo

                        var arcProds = db.Product_ARC_Maps.Where(x => x.ProductId == productIdTo).ToList();
                        foreach (Product_ARC_Map arcProdMap in arcProds)
                        {
                            db.Product_ARC_Maps.DeleteOnSubmit(arcProdMap);
                        }
                        db.SubmitChanges();

                        //get the list of ARCs mapped with productIDFrom
                        listArcsMapped = (from pam in db.Product_ARC_Maps
                                          where pam.ProductId == productIdFrom
                                          select pam.ARCId).ToList();
                    }
                }
                else
                {
                    string scripts = "alertify.alert('No Arcs or Installers Selected');";
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", scripts, true);
                }


                //insert into product_Arc_map for the new product selected -ProductIDTo

                //string csdrestrictedARC = string.Empty;
                auditNotes.Notes += "ARCsmapped: ";
                List <Product_ARC_Map> selectedARCList = new List <Product_ARC_Map>();
                if (listArcsMapped.Count > 0)
                {
                    for (int i = 0; i <= listArcsMapped.Count - 1; i++)
                    {
                        Product_ARC_Map pam = new Product_ARC_Map();
                        pam.ProductId = productIdTo;
                        pam.ARCId     = Convert.ToInt32(listArcsMapped[i]);

                        // CSD Restrictions not included
                        bool isCSDRestricted = false;

                        //for (int o = 0; o <= lstCSDRestrictedARC.Items.Count - 1; o++)
                        //{
                        //    if (lstCSDRestrictedARC.Items[o].Value == lstSelectedARC.Items[i].Value)
                        //    {
                        //        isCSDRestricted = true;
                        //        csdrestrictedARC += lstSelectedARC.Items[i].Value + ", ";
                        //    }
                        //}

                        pam.CSDRestriction = isCSDRestricted;
                        var count = selectedARCList.Where(x => x.ARCId == pam.ARCId && x.ProductId == pam.ProductId && x.CSDRestriction == pam.CSDRestriction).Count();
                        if (count <= 0)
                        {
                            selectedARCList.Add(pam);
                        }

                        auditNotes.Notes += listArcsMapped[i] + ", ";  //add list of selected arcs to Notes.
                    }


                    db.Product_ARC_Maps.InsertAllOnSubmit(selectedARCList);
                    db.SubmitChanges();
                }

                //insert into product_Installer_map

                if (listInstallersMapped.Count > 0)
                {
                    List <Product_Installer_Map> installerMapList = new List <Product_Installer_Map>();
                    auditNotes.Notes += "Installers Mapped: ";
                    for (int i = 0; i <= listInstallersMapped.Count - 1; i++)
                    {
                        Product_Installer_Map pim;

                        pim             = new Product_Installer_Map();
                        pim.ProductId   = productIdTo;
                        pim.InstallerId = listInstallersMapped[i];
                        var count = installerMapList.Where(x => x.InstallerId == pim.InstallerId &&
                                                           x.ProductId == pim.ProductId).Count();
                        if (count <= 0)
                        {
                            installerMapList.Add(pim);
                        }
                        auditNotes.Notes += listInstallersMapped[i] + ", ";
                    }
                    db.Product_Installer_Maps.InsertAllOnSubmit(installerMapList);
                    db.SubmitChanges();
                }
                string script = "alertify.alert(' " + listInstallersMapped.Count + " Installers and " + listArcsMapped.Count + " ARCs mapped  from [" + lstProductsFrom.SelectedItem.Text + "]  to [ " + lstProductsTo.SelectedItem.Text + "] successfully.');";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true);
            }
        }
        catch (Exception objException)
        {
            db = new CSLOrderingARCBAL.LinqToSqlDataContext();
            db.USP_SaveErrorDetails(Request.Url.ToString(), "MappingInstallerARCToProducts-->btnSave", 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()]));
        }
        finally
        {
            if (db != null)
            {
                db.Dispose();
            }
        }
    }
    //public String strProductCode = String.Empty;
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            Audit audit = new Audit();
            db = new LinqToSqlDataContext();

            string msg = String.Empty;


            //if updating product and delete all the mapped arc's,depprod,relatedprod and categories of this product.
            if (Session[enumSessions.ProductId.ToString()] != null)
            {
                var ProdInfo = (from pro in db.Products
                                where pro.ProductId == Convert.ToInt32(Session[enumSessions.ProductId.ToString()])
                                select pro).SingleOrDefault();

                //enter all the checked Arcs/Installers of this product

                audit.Notes = "ProductCode: " + hdnProductCode.Value.ToString();

                var arcsToDelete = (from pam in db.Product_ARC_Maps
                                    where pam.ProductId == Convert.ToInt32(Session[enumSessions.ProductId.ToString()])
                                    select pam).ToList();
                db.Product_ARC_Maps.DeleteAllOnSubmit(arcsToDelete);

                for (int i = 0; i <= lstSelectedARC.Items.Count - 1; i++)
                {
                    Product_ARC_Map pam;
                    pam           = new Product_ARC_Map();
                    pam.ProductId = Convert.ToInt32(Session[enumSessions.ProductId.ToString()]);
                    pam.ARCId     = Convert.ToInt32(lstSelectedARC.Items[i].Value);
                    bool isCSDRestricted = false;

                    for (int o = 0; o <= lstCSDRestrictedARC.Items.Count - 1; o++)
                    {
                        if (lstCSDRestrictedARC.Items[o].Value == lstSelectedARC.Items[i].Value)
                        {
                            isCSDRestricted = true;
                        }
                    }
                    pam.CSDRestriction = isCSDRestricted;
                    db.Product_ARC_Maps.InsertOnSubmit(pam);
                    db.SubmitChanges();
                    audit.Notes += ", ARCs: " + pam;
                }

                var installersToDelete = (from pam in db.Product_Installer_Maps
                                          where pam.ProductId == Convert.ToInt32(Session[enumSessions.ProductId.ToString()])
                                          select pam).ToList();
                db.Product_Installer_Maps.DeleteAllOnSubmit(installersToDelete);

                for (int i = 0; i <= lstSelectedInstaller.Items.Count - 1; i++)
                {
                    Product_Installer_Map pim;

                    pim             = new Product_Installer_Map();
                    pim.ProductId   = Convert.ToInt32(Session[enumSessions.ProductId.ToString()]);
                    pim.InstallerId = new Guid(lstSelectedInstaller.Items[i].Value);

                    db.Product_Installer_Maps.InsertOnSubmit(pim);
                    db.SubmitChanges();
                    audit.Notes += ", Installers: " + pim;
                }


                pnlproductdetails.Visible = false;
                pnlproductlist.Visible    = true;
                ClearAllInputs();


                LoadData();
                string script = "alertify.alert('Product [" + ProdInfo.ProductCode + "] - " + ProdInfo.ProductName + " updated successfully.');";
                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_Products_Lite);
            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()]));
        }
    }