//Add:UserWeightMachineMaster
        public bool Add_UserWeightMachineMaster(UserWeightMachineMaster user)
        {
            bool status = false;

            if (user != null)
            {
                db.UserWeightMachineMasters.InsertOnSubmit(user);
                db.SubmitChanges();
                status = true;
            }
            return(status);
        }
        //Add New user master to the database
        private void AddUser()
        {
            try
            {
                UserMaster _user = umRepo.Get_UserMasterByUserName(txtUserName.Text.Trim());
                if (_user != null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "toastr", "toastr.error('Same User Name already Exist! Please try again');", true);
                }
                else
                {
                    int        Userid = 0;
                    UserMaster uM     = new UserMaster();
                    uM.Password  = txtPassword.Text.ToString();
                    uM.Plantcode = ddlPlantList.SelectedValue;
                    uM.UserType  = Convert.ToInt32(ddlUserTypeList.SelectedValue);
                    uM.UserName  = txtUserName.Text;

                    uM.IsDeleted = false;
                    if (umRepo.Add_UserMaster(uM))
                    {
                        Userid = uM.Id;

                        for (int i = 0; i < chkWeighbridgeId.Items.Count; i++)
                        {
                            if (chkWeighbridgeId.Items[i].Selected)
                            {
                                UserWeightMachineMaster uwm = new UserWeightMachineMaster();
                                uwm.UserId          = Userid;
                                uwm.WeightMachineId = Convert.ToInt32(chkWeighbridgeId.Items[i].Value);
                                uwRepo.Add_UserWeightMachineMaster(uwm);
                            }
                        }
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toastr", "toastr.success('User Saved Successfully')", true);
                        HtmlMeta meta = new HtmlMeta();
                        meta.HttpEquiv = "Refresh";
                        meta.Content   = "1;url=AddEdit.aspx";
                        this.Page.Controls.Add(meta);
                    }
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toastr", "toastr.error('" + ex.Message.ToString() + "');", true);
            }
        }
        //Update User Master
        private void UpdateUser()
        {
            if (Request.QueryString["Id"] != null)
            {
                try
                {
                    int        id   = Convert.ToInt32(Request.QueryString["Id"].ToString());
                    UserMaster _mat = umRepo.Get_UserMaster_List().Where(x => x.UserName == txtUserName.Text.Trim() && x.Id != id).FirstOrDefault();
                    if (_mat != null)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toastr", "toastr.error('Same User Name Exist! Please try again');", true);
                    }
                    else
                    {
                        UserMaster uM = db.UserMasters.FirstOrDefault(x => x.Id == id && x.IsDeleted == false);
                        if (uM != null)
                        {
                            uM.Password  = txtPassword.Text.ToString();
                            uM.Plantcode = ddlPlantList.SelectedValue;
                            uM.UserType  = Convert.ToInt32(ddlUserTypeList.SelectedValue);
                            uM.UserName  = txtUserName.Text;
                            db.SubmitChanges();
                            for (int i = 0; i < chkWeighbridgeId.Items.Count; i++)
                            {
                                if (chkWeighbridgeId.Items[i].Selected)
                                {
                                    UserWeightMachineMaster uwm = db.UserWeightMachineMasters.FirstOrDefault(x => x.UserId == uM.Id && x.WeightMachineId == Convert.ToInt32(chkWeighbridgeId.Items[i].Value));
                                    if (uwm == null)
                                    {
                                        uwm                 = new UserWeightMachineMaster();
                                        uwm.UserId          = uM.Id;
                                        uwm.WeightMachineId = Convert.ToInt32(chkWeighbridgeId.Items[i].Value);
                                        db.UserWeightMachineMasters.InsertOnSubmit(uwm);
                                        db.SubmitChanges();
                                    }
                                }
                                else
                                {
                                    UserWeightMachineMaster uwm = db.UserWeightMachineMasters.FirstOrDefault(x => x.UserId == uM.Id && x.WeightMachineId == Convert.ToInt32(chkWeighbridgeId.Items[i].Value));
                                    if (uwm != null)
                                    {
                                        db.UserWeightMachineMasters.DeleteOnSubmit(uwm);
                                        db.SubmitChanges();
                                    }
                                }
                            }

                            ScriptManager.RegisterStartupScript(this, this.GetType(), "toastr", "toastr.success('User Updated Successfully')", true);
                            HtmlMeta meta = new HtmlMeta();
                            meta.HttpEquiv = "Refresh";
                            meta.Content   = "1;url=AddEdit.aspx?id=" + id;
                            this.Page.Controls.Add(meta);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "toastr", "toastr.error('" + ex.Message.ToString() + "');", true);
                }
            }
        }
        //Save Excel File Data To SQL Server
        public string SaveDataToServer(DataSet ds)
        {
            string result = string.Empty;

            using (DataClasses1DataContext db = new DataClasses1DataContext())
            {
                string    connection = ConfigurationManager.ConnectionStrings["AveryDBConnectionString"].ConnectionString;
                DataTable dt         = ds.Tables[0];
                int       _failed    = 0;
                int       _success   = 0;
                int       _update    = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    int    userid      = 0;
                    string _UserName   = dr["User Name"].ToString();
                    string _Password   = dr["Password"].ToString();
                    string _UserType   = dr["User Type"].ToString();
                    string _PlantCode  = dr["Plant Code"].ToString();
                    string _machines   = dr["Weigh Machines"].ToString();
                    int    _UserTypeId = 0;
                    try
                    {
                        _UserTypeId = db.UserClassifications.FirstOrDefault(x => x.UserType == _UserType && x.IsDeleted == false).Id;
                    }
                    catch { }
                    PlantMaster _pm  = db.PlantMasters.FirstOrDefault(x => x.PlantCode == _PlantCode && x.IsDeleted == false);
                    var         data = db.UserMasters.FirstOrDefault(x => x.UserName == _UserName && x.IsDeleted == false);
                    if (data != null)
                    {
                        int id = 0;
                        try
                        {
                            id = db.UserMasters.FirstOrDefault(x => x.UserName == _UserName && x.IsDeleted == false).Id;
                        }
                        catch
                        {
                        }
                        userid = id;
                        UserMaster _um = db.UserMasters.FirstOrDefault(x => x.UserName == _UserName & x.IsDeleted == false && x.Id != id);
                        if (_um != null)
                        {
                            _failed++;
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(_Password) && !string.IsNullOrEmpty(_UserName) && _UserTypeId > 0 && _pm != null)
                            {
                                data.UserName  = _UserName;
                                data.Password  = _Password;
                                data.UserType  = _UserTypeId;
                                data.Plantcode = _PlantCode;
                                db.SubmitChanges();
                                _update++;
                            }
                            else
                            {
                                _failed++;
                            }
                        }
                    }
                    else
                    {
                        int id     = db.UserClassifications.FirstOrDefault(x => x.UserType == _UserType && x.IsDeleted == false).Id;
                        var _plant = db.PlantMasters.FirstOrDefault(x => x.PlantCode == _PlantCode && x.IsDeleted == false);
                        if (id > 0 && _plant != null && !string.IsNullOrEmpty(_UserName) && !string.IsNullOrEmpty(_Password))
                        {
                            var _user = db.UserMasters.FirstOrDefault(x => x.UserName == _UserName && x.IsDeleted == false);
                            if (_user != null)
                            {
                                _failed++;
                            }
                            else
                            {
                                UserMaster _um = new UserMaster();
                                _um.UserName  = _UserName;
                                _um.Password  = _Password;
                                _um.UserType  = id;
                                _um.Plantcode = _PlantCode;
                                db.UserMasters.InsertOnSubmit(_um);
                                db.SubmitChanges();
                                userid = _um.Id;
                                _success++;
                            }
                        }
                    }

                    string[] machines = _machines.Split(',');
                    foreach (string _mc in machines)
                    {
                        int machineId = 0;
                        try
                        {
                            machineId = db.WeightMachineMasters.Where(x => x.MachineId == _mc).FirstOrDefault().Id;
                        }
                        catch { }
                        if (machineId != 0 && userid != 0)
                        {
                            UserWeightMachineMaster uwmm = db.UserWeightMachineMasters.Where(x => x.UserId == userid && x.WeightMachineId == machineId).FirstOrDefault();
                            if (uwmm == null)
                            {
                                uwmm = new UserWeightMachineMaster();
                                uwmm.WeightMachineId = machineId;
                                uwmm.UserId          = userid;
                                db.UserWeightMachineMasters.InsertOnSubmit(uwmm);
                                db.SubmitChanges();
                            }
                        }
                    }
                }
                result = "Result: Not saved: " + _failed + ", Saved: " + _success + ", Updated: " + _update;
            }
            return(result);
        }