protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.hdnPagingIndex.Value == "")
            {
                this.hdnPagingIndex.Value = "0";
            }
            else
            {
                this.hdnPagingIndex.Value = (int.Parse(this.hdnPagingIndex.Value) + 1).ToString();
            }

            string keywordCollection = this.GetCriteriaCollectionFromList(this.lstKeyword);
            string authorCollection  = this.GetCriteriaCollectionFromList(this.lstAuthor);

            ATTLibraryMaterial obj = new ATTLibraryMaterial();

            obj.OrgID                   = int.Parse(this.ddlOrg.SelectedValue);
            obj.LibraryID               = int.Parse(this.ddlLibrary.SelectedValue);
            obj.LibraryMaterialType     = int.Parse(this.ddlType.SelectedValue);
            obj.LibraryMaterialCategory = int.Parse(this.ddlCategory.SelectedValue);
            obj.LanguageID              = int.Parse(this.ddlLanguage.SelectedValue);
            obj.PublisherID             = int.Parse(this.ddlPublisher.SelectedValue);
            obj.CallNo                  = this.txtCallNo.Text;

            Session["LibraryMaterialEditor"] = BLLLibraryMaterial.GetSearchedLibraryMaterialList(obj, keywordCollection, authorCollection, "M");
            this.grdSearch.DataSource        = (List <ATTLibraryMaterial>)Session["LibraryMaterialEditor"];
            this.grdSearch.DataBind();
            this.lblRecordCount.Text = "Total " + this.grdSearch.Rows.Count.ToString() + " record found.";

            this.LoadImageX.Width  = new Unit(0);
            this.LoadImageX.Height = new Unit(0);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    void SaveLibraryDetails(long?LibMatID, string action, long acc_id)
    {
        long LMID = 0;

        if (LibMatID == null)
        {
            LMID = long.Parse(Session["LibMatID"].ToString());
        }
        else
        {
            LMID = LibMatID.Value;
        }

        ATTLibraryMaterial LMObj = (ATTLibraryMaterial)Session["LM"];
        ////---------Add Material Copy---------
        ATTLibraryMaterialCopy libCopyObj = new ATTLibraryMaterialCopy();

        libCopyObj.Edition          = editionTxt.Text;
        libCopyObj.PublicationDate  = pubDateTxt.Text;
        libCopyObj.RegistrationDate = DateTime.Now;
        regDateTxt.Text             = DateTime.Now.ToShortDateString();
        libCopyObj.Price            = double.Parse(priceTxt.Text);
        libCopyObj.CurrencyID       = int.Parse(this.ddlCurrency.SelectedValue);
        libCopyObj.Location         = locTxt.Text;
        libCopyObj.Action           = action;
        libCopyObj.AccessionID      = acc_id;
        libCopyObj.IsbnIssnNo       = isbnTxt.Text;
        libCopyObj.OrgID            = LMObj.OrgID;
        libCopyObj.LibraryID        = LMObj.LibraryID;
        libCopyObj.EntryBy          = LMObj.EntryBy;

        List <ATTLibraryMaterialCopy> lstLMCpy = new List <ATTLibraryMaterialCopy>();

        lstLMCpy.Add(libCopyObj);
        BLLLibraryMaterial.AddLibraryMaterialCopy(lstLMCpy, LMID);
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (this.hdnAction.Value == "")
        {
            if (!ServerValidateAll())
            {
                this.lblStatusMessage.Text = sb.ToString();
                this.programmaticModalPopup.Show();
                return;
            }
        }
        else if (this.hdnAction.Value == "M")
        {
            if (!ServerValidateMaster())
            {
                this.lblStatusMessage.Text = sb.ToString();
                this.programmaticModalPopup.Show();
                return;
            }
        }
        string ext = System.IO.Path.GetExtension(this.fupAttachment.FileName);

        if (ext == "pdf" || ext == "jpeg" || ext == "jpg" || ext == "gif")
        {
            this.lblStatusMessage.Text = "Uploaded file's extention must be of 'pdf' or 'jpg' or 'jpeg' or 'gif'.";
            this.programmaticModalPopup.Show();
            return;
        }

        ATTLibraryMaterial LM = (ATTLibraryMaterial)Session["LM_LibraryMaterial"];

        LM.OrgID     = int.Parse(this.ddlOrg.SelectedValue);
        LM.LibraryID = int.Parse(this.ddlLibrary.SelectedValue);
        if (this.hdnAction.Value != "M")
        {
            LM.LMaterialID = 0;
        }
        LM.LibraryMaterialType     = int.Parse(this.ddlType_Rqd.SelectedValue);
        LM.LibraryMaterialCategory = int.Parse(this.ddlCategory_Rqd.Text);
        LM.CallNo              = this.txtCallNo_Rqd.Text;
        LM.CorporateBody       = this.txtCorporateBody.Text;
        LM.Title               = this.txtTitle_Rqd.Text;
        LM.SeriesStatement     = this.txtSeriesState.Text;
        LM.Note                = this.txtNote.Text;
        LM.BoardSubjectHeading = this.txtBrdSubHeading.Text;
        LM.GeoDescription      = this.txtGeoDescription.Text;
        LM.LanguageID          = int.Parse(this.ddlLanguage_Rqd.SelectedValue);
        LM.PublisherID         = int.Parse(this.ddlPublisher_Rqd.SelectedValue);
        LM.PhysicalDescription = this.txtPhyDescription.Text;
        LM.EntryBy             = ((ATTUserLogin)Session["Login_User_Detail"]).UserName;
        LM.EntryOn             = DateTime.Now;
        //LM.ContentFile = null;
        //LM.CFileType = "";
        if (this.hdnAction.Value == "M")
        {
            LM.Action = "M";
        }
        else
        {
            LM.Action = "A";
        }

        if (this.fupAttachment.HasFile == true)
        {
            LM.ContentFile = this.fupAttachment.FileBytes;
            LM.CFileType   = System.IO.Path.GetExtension(this.fupAttachment.FileName);
        }

        ObjectValidation OV = BLLLibraryMaterial.Validate(LM);

        if (OV.IsValid == false)
        {
            //this.lblMaterialStatus.Text = OV.ErrorMessage;
            this.lblStatusMessage.Text = OV.ErrorMessage;
            this.programmaticModalPopup.Show();
            return;
        }

        GridViewRow keyRow;
        int         keyRowCount = 0;
        bool        keyChecked;

        foreach (ATTLibraryMaterialKeyword ky in LM.LstLMKeyword)
        {
            keyRow = this.grdKeyword.Rows[keyRowCount];

            keyChecked = ((CheckBox)keyRow.FindControl("chkKeyword")).Checked;
            if (keyChecked == true && ky.Action == "A")
            {
                ky.Action = "N";
            }
            else if (keyChecked == true && ky.Action == "M")
            {
                ky.Action = "D";
            }
            else if (keyChecked == false && ky.Action == "M")
            {
                ky.Action = "N";
            }

            keyRowCount = keyRowCount + 1;
        }

        GridViewRow authorRow;
        int         authorRowCount = 0;
        bool        authorChecked;

        foreach (ATTLibraryMaterialAuthor au in LM.LstLMAuthor)
        {
            authorRow = this.grdAuthor.Rows[authorRowCount];

            authorChecked = ((CheckBox)authorRow.FindControl("chkAuthor")).Checked;
            if (authorChecked == true && au.Action == "A")
            {
                au.Action = "N";
            }
            else if (authorChecked == true && au.Action == "M")
            {
                au.Action = "D";
            }
            else if (authorChecked == false && au.Action == "M")
            {
                au.Action = "N";
            }

            authorRowCount = authorRowCount + 1;
        }



        ATTUserLogin user = ((ATTUserLogin)Session["Login_User_Detail"]);

        try
        {
            if (LM.Action == "A")
            {
                if (user.MenuList["4,9,1"] == null || user.MenuList["4,9,1"].PAdd == "N")
                {
                    this.lblStatusMessage.Text = Utilities.PreviledgeMsg + " add Library Material.";
                    this.programmaticModalPopup.Show();
                    return;
                }
            }
            else if (LM.Action == "M")
            {
                if (user.MenuList["4,9,1"] == null || user.MenuList["4,9,1"].PEdit == "N")
                {
                    this.lblStatusMessage.Text = Utilities.PreviledgeMsg + " update Library Material.";
                    this.programmaticModalPopup.Show();
                    return;
                }
            }

            Previlege.PrevilegeType pType = Previlege.PrevilegeType.P_ADD;
            string modeName = "";

            if (LM.Action == "A")
            {
                pType    = Previlege.PrevilegeType.P_ADD;
                modeName = "added";
            }
            else if (LM.Action == "M")
            {
                pType    = Previlege.PrevilegeType.P_EDIT;
                modeName = "updated";
            }

            Previlege pobj = new Previlege(user.UserName, "4,9,1", 4, pType);


            Session["LM"] = LM;

            //save details
            //save master detail every time
            if (Session["LibMatID"] == null)
            {
                LM.Action = "A";
            }
            else
            {
                LM.Action      = "M";
                LM.LMaterialID = long.Parse(Session["LibMatID"].ToString());
            }
            long LMID = BLLLibraryMaterial.AddLibraryMaterial(LM, pobj);
            Session["LibMatID"] = LMID;

            if (this.hdnAction.Value == "M")
            {
                if (this.libMatCopyGrd.SelectedIndex > -1)
                {
                    SaveLibraryDetails(LMID, "M", long.Parse(Session["AccessionID"].ToString()));
                    LoadLibraryMaterialCopyGrid(LMID);
                }
                else if (this.libMatCopyGrd.SelectedIndex < 0)
                {
                    if (this.editionTxt.Text != "" && this.pubDateTxt.Text != "" && this.priceTxt.Text != "" && this.isbnTxt.Text != "")
                    {
                        SaveLibraryDetails(LMID, "A", 0);
                        LoadLibraryMaterialCopyGrid(LMID);
                    }
                }
            }
            else if (this.hdnAction.Value != "M")
            {
                if (this.libMatCopyGrd.SelectedIndex > -1)
                {
                    //update the material copy
                    SaveLibraryDetails(LMID, "M", long.Parse(Session["AccessionID"].ToString()));
                    LoadLibraryMaterialCopyGrid(LMID);
                }
                else if (this.libMatCopyGrd.SelectedIndex < 0)
                {
                    //add new material copy
                    SaveLibraryDetails(LMID, "A", 0);
                    LoadLibraryMaterialCopyGrid(LMID);
                }
            }


            this.ClearLibraryMaterialControl();
            this.ClearLMCopyControls();
            this.LoadRegDate();

            this.lblStatusMessage.Text = "Library material " + modeName + " successfully.";
            this.programmaticModalPopup.Show();
        }
        catch (Exception ex)
        {
            this.lblStatusMessage.Text = ex.Message;
            this.programmaticModalPopup.Show();
            return;
        }
    }