using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                 Started Date  : 04-Jan-2008
* Module          : Audit Trail                               Last Modified : 14-Jan-2008
* Programmer      : Christopher S
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\TimeCheck_Product\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class System_Settings_System_AuditTrail : Base.BasePage
{
    #region VariableDeclaration
    // creating objetc for Audit Trail class
    AuditTrailEventData objAuditEvent = new AuditTrailEventData();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    ResourceManager rm;
    CultureInfo ci;
    cls_Common objcommon = new cls_Common();
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        //get the culture info to set the language
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            objcommon.menu("Others", "78");
        }
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        //Call to a function to implement the UserRights
        UserRights();
        if (!IsPostBack)
        {            
            btnSave.OnClientClick = "return confirm('" + rm.GetString("Alert_ConfirmSave", ci) + "');";
            // check whether is Audit Trail is saved before
            bool isAuditTrailSaved;
            isAuditTrailSaved = objAuditEvent.IsAuditTrailBeforeSaved();
            if (isAuditTrailSaved.Equals(true))
            {
                // bind GridView without information
                BindingGridView();
                AssignHiddenValue();
                // get Audit Trail information from DB
                GetAuditTrailInformation();
            }
            else
            {
                // bind GridView without information
                BindingGridView();
                AssignHiddenValue();
            }
        }
    }
    #endregion
    #region UserDefinedFunctions
    private void GetAuditTrailInformation()
    {
        DataSet dsAuditTable = new DataSet();
        DataSet dsAuditColumn = new DataSet();
        // get configured Audit Table information from TA_Tbl_AuditTable
        dsAuditTable = objAuditEvent.SelectAllAuditTableNames();
        // required data set and data table declaration
        // add rows to data table and add data table to dats set
        DataSet dsAudit = new DataSet();
        DataTable dsTable = new DataTable("Tables");
        dsTable.Columns.Add(new DataColumn("TableID", System.Type.GetType("System.Decimal")));
        dsTable.Columns.Add(new DataColumn("TableName", System.Type.GetType("System.String")));
        dsTable.Columns.Add(new DataColumn("Status", System.Type.GetType("System.Boolean")));
        foreach (DataRow drTable in dsAuditTable.Tables[0].Rows)
        {
            DataRow dsAuditRow = dsTable.NewRow();
            dsAuditRow.BeginEdit();
            dsAuditRow["TableID"] = drTable["TableID"];
            dsAuditRow["TableName"] = drTable["TableName"];
            dsAuditRow["Status"] = drTable["Status"];
            dsAuditRow.EndEdit();
            dsTable.Rows.Add(dsAuditRow);
        }
        dsAudit.Tables.Add(dsTable);
        // seting Primary Key Column 
        DataColumn[] Parent_PKColumns = new DataColumn[1];
        Parent_PKColumns[0] = dsTable.Columns["TableID"];
        dsTable.PrimaryKey = Parent_PKColumns;
        DataTable dsColumn = new DataTable("Columns");
        dsColumn.Columns.Add(new DataColumn("FieldID", System.Type.GetType("System.Decimal")));
        dsColumn.Columns.Add(new DataColumn("TableID", System.Type.GetType("System.Decimal")));
        dsColumn.Columns.Add(new DataColumn("FieldName", System.Type.GetType("System.String")));
        dsColumn.Columns.Add(new DataColumn("InsertBit", System.Type.GetType("System.Boolean")));
        dsColumn.Columns.Add(new DataColumn("UpdateBit", System.Type.GetType("System.Boolean")));
        dsColumn.Columns.Add(new DataColumn("DeleteBit", System.Type.GetType("System.Boolean")));
        dsColumn.Columns.Add(new DataColumn("Status", System.Type.GetType("System.Boolean")));
        foreach (DataRow drTableId in dsAuditTable.Tables[0].Rows)
        {
            // get configured Audit Column information from TA_Tbl_AuditField
            dsAuditColumn = objAuditEvent.SelectAllAuditColumnNames(Convert.ToDecimal(drTableId["TableID"]));
            foreach (DataRow drColumnName in dsAuditColumn.Tables[0].Rows)
            {
                DataRow dsAuditColRow = dsColumn.NewRow();
                dsAuditColRow.BeginEdit();
                dsAuditColRow["FieldID"] = drColumnName["FieldID"];
                dsAuditColRow["TableID"] = drColumnName["TableID"];
                dsAuditColRow["FieldName"] = drColumnName["FieldName"];
                dsAuditColRow["InsertBit"] = drColumnName["InsertBit"];
                dsAuditColRow["UpdateBit"] = drColumnName["UpdateBit"];
                dsAuditColRow["DeleteBit"] = drColumnName["DeleteBit"];
                dsAuditColRow["Status"] = drColumnName["Status"];
                dsAuditColRow.EndEdit();
                dsColumn.Rows.Add(dsAuditColRow);
            }
        }
        // setting Primary and Foregin Key column
        DataColumn[] Child_PKColumns = new DataColumn[1];
        Child_PKColumns[0] = dsColumn.Columns["FieldID"];
        dsColumn.PrimaryKey = Child_PKColumns;
        dsAudit.Tables.Add(dsColumn);
        DataColumn[] Child_FKColumns = new DataColumn[1];
        Child_FKColumns[0] = dsColumn.Columns["TableID"];
        // create DataRelation and add that datarelation to dataset
        DataRelation datarel = new DataRelation("ParentChild", Parent_PKColumns, Child_FKColumns);
        datarel.Nested = true;
        dsAudit.Relations.Add(datarel);
        // iterate through GridView rows
        foreach (GridViewRow gvRow in grvAuditTrail.Rows)
        {
            int rowCount = grvAuditTrail.Rows.Count;
            // check for Table Names in GridView
            if (grvAuditTrail.Rows[gvRow.RowIndex].Cells[1].Text != "&nbsp;")
            {
                bool isTableChecked = false;
                // get Table Name from GridView
                string tName = grvAuditTrail.Rows[gvRow.RowIndex].Cells[1].Text;
                // filter that Table Name's row from data table
                dsAudit.Tables["Tables"].DefaultView.RowFilter = "TableName = '" + tName + "'";
                string tStatus = string.Empty;
                // check that Table Name is already saved in DB
                if (dsAudit.Tables["Tables"].DefaultView.Count > 0)
                {
                    // if saved get the Table Status 
                    tStatus = dsAudit.Tables["Tables"].DefaultView[0]["Status"].ToString();
                    // check Status, if True then get TableID and
                    // get corresponding Fields information through rowfilter of TableID
                    if (tStatus.Contains("True"))
                    {
                        int fIndex = 0;
                        fIndex = gvRow.RowIndex + 1;
                        while (fIndex < rowCount && grvAuditTrail.Rows[fIndex].Cells[1].Text == "&nbsp;")
                        {
                            string fName = grvAuditTrail.Rows[fIndex].Cells[3].Text;
                            dsAudit.Tables["Columns"].DefaultView.RowFilter = "FieldName = '" + fName + "'";
                            if (dsAudit.Tables["Columns"].DefaultView.Count > 0)
                            {
                                string iBit = string.Empty;
                                string uBit = string.Empty;
                                string dBit = string.Empty;
                                string insertBit = dsAudit.Tables["Columns"].DefaultView[0]["InsertBit"].ToString();
                                string updateBit = dsAudit.Tables["Columns"].DefaultView[0]["UpdateBit"].ToString();
                                string deleteBit = dsAudit.Tables["Columns"].DefaultView[0]["DeleteBit"].ToString();
                                // check insert,update,delet bit's status and set in GridView the same
                                if (insertBit.Contains("True"))
                                {
                                    iBit = "t";
                                    CheckBox cBInsert = (CheckBox)grvAuditTrail.Rows[fIndex].FindControl("chkListInsertField");
                                    cBInsert.Checked = true;
                                }
                                else
                                {
                                    CheckBox cBInsert = (CheckBox)grvAuditTrail.Rows[fIndex].FindControl("chkListInsertField");
                                    cBInsert.Checked = false;
                                }
                                if (updateBit.Contains("True"))
                                {
                                    uBit = "t";
                                    CheckBox cBUpdate = (CheckBox)grvAuditTrail.Rows[fIndex].FindControl("chkListUpdateField");
                                    cBUpdate.Checked = true;
                                }
                                else
                                {
                                    CheckBox cBUpdate = (CheckBox)grvAuditTrail.Rows[fIndex].FindControl("chkListUpdateField");
                                    cBUpdate.Checked = false;
                                }
                                if (deleteBit.Contains("True"))
                                {
                                    dBit = "t";
                                    CheckBox cBDelete = (CheckBox)grvAuditTrail.Rows[fIndex].FindControl("chkListDeleteField");
                                    cBDelete.Checked = true;
                                }
                                else
                                {
                                    CheckBox cBDelete = (CheckBox)grvAuditTrail.Rows[fIndex].FindControl("chkListDeleteField");
                                    cBDelete.Checked = false;
                                }
                                // check if any one of the bit is checked then set table status to true
                                if (iBit == "t" || uBit == "t" || dBit == "t")
                                {
                                    isTableChecked = true;
                                }
                            }
                            dsAudit.Tables["Columns"].DefaultView.RowFilter = " ";
                            fIndex = fIndex + 1;
                        }
                    }
                    // after went through one table's information then decide GridView's Table Name's check box staus
                    if (isTableChecked == true)
                    {
                        CheckBox cBTable = (CheckBox)grvAuditTrail.Rows[gvRow.RowIndex].FindControl("chkListTable");
                        cBTable.Checked = true;
                    }
                    else
                    {
                        CheckBox cBTable = (CheckBox)grvAuditTrail.Rows[gvRow.RowIndex].FindControl("chkListTable");
                        cBTable.Checked = false;
                    }
                }
                // empty the Tables data table's defaultview
                // to filter new Tables row
                dsAudit.Tables["Tables"].DefaultView.RowFilter = " ";
            }
        }
    }
    private void BindingGridView()
    {
        DataSet dsTableName = new DataSet();
        DataSet dsColumnName = new DataSet();
        Hashtable ht = new Hashtable();
        // data set to hold All congigured Table Name's description from respective Database        
        dsTableName = objAuditEvent.SelectAllTableNames();
        if (dsTableName != null && dsTableName.Tables.Count > 0)
        {
            if (dsTableName.Tables[0].Rows.Count > 0)
            {
                // data set and two data tables to hold All Table Names and
                // Column Names with Data Table Relation 
                DataSet dsAudit = new DataSet();
                DataTable dsAuditTable = new DataTable("AllTableName");
                dsAuditTable.Columns.Add(new DataColumn("id", System.Type.GetType("System.Decimal")));
                dsAuditTable.Columns.Add(new DataColumn("TableName", System.Type.GetType("System.String")));
                dsAuditTable.Columns.Add(new DataColumn("Description", System.Type.GetType("System.String")));
                foreach (DataRow drTableName in dsTableName.Tables[0].Rows)
                {
                    DataRow dsAuditTableRow = dsAuditTable.NewRow();
                    dsAuditTableRow.BeginEdit();
                    dsAuditTableRow["id"] = drTableName["id"];
                    dsAuditTableRow["TableName"] = drTableName["TableName"];
                    dsAuditTableRow["Description"] = drTableName["Description"];
                    dsAuditTableRow.EndEdit();
                    dsAuditTable.Rows.Add(dsAuditTableRow);
                }
                dsAudit.Tables.Add(dsAuditTable);
                // setting parent table's Primary Key column
                DataColumn[] Parent_PKColumns = new DataColumn[1];
                Parent_PKColumns[0] = dsAuditTable.Columns["id"];
                dsAuditTable.PrimaryKey = Parent_PKColumns;
                DataTable dsAuditColumn = new DataTable("AllColumnName");
                dsAuditColumn.Columns.Add(new DataColumn("columnid", System.Type.GetType("System.Int32")));
                dsAuditColumn.Columns.Add(new DataColumn("id", System.Type.GetType("System.Decimal")));
                dsAuditColumn.Columns.Add(new DataColumn("ColumnName", System.Type.GetType("System.String")));
                dsAuditColumn.Columns.Add(new DataColumn("Description", System.Type.GetType("System.String")));
                Int32 columnId = 1;
                foreach (DataRow drTableId in dsTableName.Tables[0].Rows)
                {
                    // select congigured Field Name's description from database for respective tables
                    dsColumnName = objAuditEvent.SelectAllColumnNames(Convert.ToDecimal(drTableId["id"]));
                    foreach (DataRow drColumnName in dsColumnName.Tables[0].Rows)
                    {
                        DataRow dsAuditColRow = dsAuditColumn.NewRow();
                        dsAuditColRow.BeginEdit();
                        dsAuditColRow["columnid"] = columnId;
                        dsAuditColRow["id"] = drColumnName["id"];
                        dsAuditColRow["ColumnName"] = drColumnName["ColumnName"];
                        dsAuditColRow["Description"] = drColumnName["Description"];
                        dsAuditColRow.EndEdit();
                        dsAuditColumn.Rows.Add(dsAuditColRow);
                        columnId = columnId + 1;
                    }
                }
                // setting child table's Primary Key column
                DataColumn[] Child_PKColumns = new DataColumn[1];
                Child_PKColumns[0] = dsAuditColumn.Columns["columnid"];
                dsAuditColumn.PrimaryKey = Child_PKColumns;
                dsAudit.Tables.Add(dsAuditColumn);
                // setting child table's Foregin Key column
                DataColumn[] Child_FKColumns = new DataColumn[1];
                Child_FKColumns[0] = dsAuditColumn.Columns["id"];
                // creating and adding DataRelation with Primary and Foregin Key relationship 
                DataRelation datarel = new DataRelation("ParentChild", Parent_PKColumns, Child_FKColumns);
                datarel.Nested = true;
                dsAudit.Relations.Add(datarel);
                // data table to hold result of Parent and Child rows of dataset for binding GridView  
                DataTable dtResult = new DataTable();
                DataRow rResult;
                dtResult.Columns.Add(new DataColumn("Table Name", typeof(string)));
                dtResult.Columns.Add(new DataColumn("Page Name", typeof(string)));
                dtResult.Columns.Add(new DataColumn("Field Name", typeof(string)));
                dtResult.Columns.Add(new DataColumn("Label Name", typeof(string)));
                // iterate through the parent and respective child rows using DataRelation
                foreach (DataRow parentRow in dsAudit.Tables["AllTableName"].Rows)
                {
                    rResult = dtResult.NewRow();
                    rResult.BeginEdit();
                    rResult["Table Name"] = parentRow["TableName"];
                    rResult["Page Name"] = parentRow["Description"];
                    rResult.EndEdit();
                    dtResult.Rows.Add(rResult);
                    foreach (DataRow childRow in parentRow.GetChildRows(datarel))
                    {
                        rResult = dtResult.NewRow();
                        rResult.BeginEdit();
                        rResult["Field Name"] = childRow["ColumnName"];
                        rResult["Label Name"] = childRow["Description"];
                        rResult.EndEdit();
                        dtResult.Rows.Add(rResult);
                    }
                }
                //grvAuditTrail.Columns["Page Name"].HeaderStyle.BorderStyle = Double;
                //grvAuditTrail.Columns["Label Name"].HeaderStyle.BorderStyle = Double;
                // binding DataTable with gridview                
                grvAuditTrail.DataSource = dtResult;
                grvAuditTrail.DataBind();
                //grvAuditTrail.Columns[0].HeaderStyle.BorderStyle = BorderStyle.Double;
                HideFieldLinkButton();
            }
            else
            {
                lblAuditTrailNotDefined.Visible = true;
                btnSave.Enabled = false;
            }
        }
    }
    // hide necessary rows and buttons and check boxes after data binding and before showing UI
    private void HideFieldLinkButton()
    {
        // hide header - inser, update,delete labels
        Label lblInsert = (Label)grvAuditTrail.HeaderRow.FindControl("lblInsert");
        lblInsert.Visible = false;
        Label lblUpdate = (Label)grvAuditTrail.HeaderRow.FindControl("lblUpdate");
        lblUpdate.Visible = false;
        Label lblDelete = (Label)grvAuditTrail.HeaderRow.FindControl("lblDelete");
        lblDelete.Visible = false;
        // hide database TableName and Field Name columns' header
        grvAuditTrail.HeaderRow.Cells[1].Visible = false;
        grvAuditTrail.HeaderRow.Cells[3].Visible = false;
        foreach (GridViewRow gvRow in grvAuditTrail.Rows)
        {
            // hide database TableName and Field Name columns' content
            gvRow.Cells[1].Visible = false;
            gvRow.Cells[3].Visible = false;
            if (gvRow.Cells[1].Text.ToString() == "&nbsp;")
            {
                // hide link button in TableName row index
                LinkButton lButton = (LinkButton)grvAuditTrail.Rows[gvRow.RowIndex].FindControl("btnLink");
                lButton.Visible = false;
                grvAuditTrail.Rows[gvRow.RowIndex].Visible = false;
            }
            else
            {
                // hide inser,update, delete check boxes in FieldName row index
                CheckBox cBoxInsert = (CheckBox)grvAuditTrail.Rows[gvRow.RowIndex].FindControl("chkListInsertField");
                cBoxInsert.Visible = false;
                CheckBox cBoxUpdate = (CheckBox)grvAuditTrail.Rows[gvRow.RowIndex].FindControl("chkListUpdateField");
                cBoxUpdate.Visible = false;
                CheckBox cBoxDelete = (CheckBox)grvAuditTrail.Rows[gvRow.RowIndex].FindControl("chkListDeleteField");
                cBoxDelete.Visible = false;
            }
        }
    }
    public void SelectUnSelectcheckbox(int RowIndex)
    {
        int intHeaderRowIndex = 0;
        foreach (GridViewRow gvrow in grvAuditTrail.Rows)
        {
            HiddenField hdnRowIndex = (HiddenField)grvAuditTrail.Rows[gvrow.RowIndex].FindControl("hdnRowIndex");
            if (Convert.ToInt32(hdnRowIndex.Value) == RowIndex)
            {
                if (grvAuditTrail.Rows[gvrow.RowIndex].Cells[1].Text != "&nbsp;")
                {
                    intHeaderRowIndex = gvrow.RowIndex;
                    CheckBox chkListTable = (CheckBox)grvAuditTrail.Rows[intHeaderRowIndex].FindControl("chkListTable");
                    chkListTable.Checked = true;
                }
                if (grvAuditTrail.Rows[gvrow.RowIndex].Cells[1].Text == "&nbsp;")
                {
                    CheckBox chkListInsertField = (CheckBox)grvAuditTrail.Rows[gvrow.RowIndex].FindControl("chkListInsertField");
                    CheckBox chkListUpdateField = (CheckBox)grvAuditTrail.Rows[gvrow.RowIndex].FindControl("chkListUpdateField");
                    CheckBox chkListDeleteField = (CheckBox)grvAuditTrail.Rows[gvrow.RowIndex].FindControl("chkListDeleteField");
                    // Modified by Gopinath on Jan23-2014 for Checkbox update in Audit trail
                    //if (chkListInsertField.Checked == false || chkListUpdateField.Checked == false || chkListDeleteField.Checked == false)
                    //{
                    //    // CheckBox chkListTable = (CheckBox)grvAuditTrail.Rows[intHeaderRowIndex].FindControl("chkListTable");
                    //    //chkListTable.Checked = false;
                    //}
                    if (chkListInsertField.Checked == false && chkListUpdateField.Checked == false && chkListDeleteField.Checked == false)
                    {
                        CheckBox chkListTable = (CheckBox)grvAuditTrail.Rows[intHeaderRowIndex].FindControl("chkListTable");
                        chkListTable.Checked = false;
                    }
                }
            }
        }
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderAuditTrail.Text = rm.GetString("Report_Audit_Trail", ci);
        btnSave.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(78, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] == DBNull.Value || Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) != 1)
            {
                Response.Redirect("SystemLandingPage.aspx");
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.btnSave.Visible = true;
            }
            else
            {
                this.btnSave.Visible = false;
            }
        }
        else
        {
            Response.Redirect("SystemLandingPage.aspx");
        }
    }
    public void AssignHiddenValue()
    {
        int intRowIndex = 0;
        int intCount = 0;
        int intRowCount = grvAuditTrail.Rows.Count;
        while (intRowIndex < intRowCount)
        {
            if (grvAuditTrail.Rows[intRowIndex].Cells[1].Text != "&nbsp;")
            {
                intCount = intCount + 1;
            }
            HiddenField hdnRowIndex = (HiddenField)grvAuditTrail.Rows[intRowIndex].FindControl("hdnRowIndex");
            hdnRowIndex.Value = Convert.ToString(intCount);
            intRowIndex = intRowIndex + 1;
        }
    }
    #endregion
    #region ButtonAndGridviewEvents
    protected void chkListInsertField_CheckedChanged(object sender, EventArgs e)
    {
        int rowindex = 0;
        CheckBox cb = (CheckBox)sender;
        GridViewRow row = (GridViewRow)cb.NamingContainer;
        if (row != null)
        {
            rowindex = row.RowIndex;
        }
        HiddenField hdnRowIndex = (HiddenField)grvAuditTrail.Rows[rowindex].FindControl("hdnRowIndex");
        SelectUnSelectcheckbox(Convert.ToInt32(hdnRowIndex.Value));
    }
    protected void chkListUpdateField_CheckedChanged(object sender, EventArgs e)
    {
        int rowindex = 0;
        CheckBox cb = (CheckBox)sender;
        GridViewRow row = (GridViewRow)cb.NamingContainer;
        if (row != null)
        {
            rowindex = row.RowIndex;
        }
        HiddenField hdnRowIndex = (HiddenField)grvAuditTrail.Rows[rowindex].FindControl("hdnRowIndex");
        SelectUnSelectcheckbox(Convert.ToInt32(hdnRowIndex.Value));
    }
    protected void chkListDeleteField_CheckedChanged(object sender, EventArgs e)
    {
        int rowindex = 0;
        CheckBox cb = (CheckBox)sender;
        GridViewRow row = (GridViewRow)cb.NamingContainer;
        if (row != null)
        {
            rowindex = row.RowIndex;
        }
        HiddenField hdnRowIndex = (HiddenField)grvAuditTrail.Rows[rowindex].FindControl("hdnRowIndex");
        SelectUnSelectcheckbox(Convert.ToInt32(hdnRowIndex.Value));
    }
    protected void chkListTable_CheckedChanged(object sender, EventArgs e)
    {
        int rowindex = 0;
        CheckBox cb = (CheckBox)sender;
        GridViewRow row = (GridViewRow)cb.NamingContainer;
        if (row != null)
        {
            rowindex = row.RowIndex;
        }
        int rowCount = grvAuditTrail.Rows.Count;
        CheckBox cBoxTable = (CheckBox)grvAuditTrail.Rows[rowindex].FindControl("chkListTable");
        if (cBoxTable.Checked == true)
        {
            rowindex = rowindex + 1;
            while (rowindex < rowCount && grvAuditTrail.Rows[rowindex].Cells[1].Text == "&nbsp;")
            {
                CheckBox chkListInsertField = (CheckBox)grvAuditTrail.Rows[rowindex].FindControl("chkListInsertField");
                CheckBox chkListUpdateField = (CheckBox)grvAuditTrail.Rows[rowindex].FindControl("chkListUpdateField");
                CheckBox chkListDeleteField = (CheckBox)grvAuditTrail.Rows[rowindex].FindControl("chkListDeleteField");
                chkListInsertField.Checked = true;
                chkListUpdateField.Checked = true;
                chkListDeleteField.Checked = true;
                rowindex = rowindex + 1;
            }
        }
        else
        {
            rowindex = rowindex + 1;
            while (rowindex < rowCount && grvAuditTrail.Rows[rowindex].Cells[1].Text == "&nbsp;")
            {
                CheckBox chkListInsertField = (CheckBox)grvAuditTrail.Rows[rowindex].FindControl("chkListInsertField");
                CheckBox chkListUpdateField = (CheckBox)grvAuditTrail.Rows[rowindex].FindControl("chkListUpdateField");
                CheckBox chkListDeleteField = (CheckBox)grvAuditTrail.Rows[rowindex].FindControl("chkListDeleteField");
                chkListInsertField.Checked = false;
                chkListUpdateField.Checked = false;
                chkListDeleteField.Checked = false;
                rowindex = rowindex + 1;
            }
        }
    }
    // handle Expand and Collapse events
    protected void grv_AuditTrail_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Label lblInsert = (Label)grvAuditTrail.HeaderRow.FindControl("lblInsert");
        lblInsert.Visible = true;
        Label lblUpdate = (Label)grvAuditTrail.HeaderRow.FindControl("lblUpdate");
        lblUpdate.Visible = true;
        Label lblDelete = (Label)grvAuditTrail.HeaderRow.FindControl("lblDelete");
        lblDelete.Visible = true;
        int rowCount = grvAuditTrail.Rows.Count;
        if (e.CommandName == "Expand")
        {
            WebControl Parent = (e.CommandSource as WebControl).Parent as WebControl;
            while (!(Parent is GridViewRow))
            {
                Parent = Parent.Parent as WebControl;
            }
            int RowIndex = (Parent as GridViewRow).RowIndex;
            LinkButton lButton = (LinkButton)grvAuditTrail.Rows[RowIndex].FindControl("btnLink");
            //lButton.Text = "-";
            lButton.CssClass = "fa fa-minus-circle";
            lButton.CommandName = "Collapse";
            RowIndex = RowIndex + 1;
            while (RowIndex < rowCount && grvAuditTrail.Rows[RowIndex].Cells[1].Text == "&nbsp;")
            {
                CheckBox cBoxTable = (CheckBox)grvAuditTrail.Rows[RowIndex].FindControl("chkListTable");
                cBoxTable.Visible = false;
                grvAuditTrail.Rows[RowIndex].Visible = true;
                RowIndex = RowIndex + 1;
            }
        }
        else
        {
            WebControl Parent = (e.CommandSource as WebControl).Parent as WebControl;
            while (!(Parent is GridViewRow))
            {
                Parent = Parent.Parent as WebControl;
            }
            int RowIndex = (Parent as GridViewRow).RowIndex;
            LinkButton lButton = (LinkButton)grvAuditTrail.Rows[RowIndex].FindControl("btnLink");
            //lButton.Text = "+";
            lButton.CssClass = "fa fa-plus-circle";
            lButton.CommandName = "Expand";
            RowIndex = RowIndex + 1;
            while (RowIndex < rowCount && grvAuditTrail.Rows[RowIndex].Cells[1].Text == "&nbsp;")
            {
                grvAuditTrail.Rows[RowIndex].Visible = false;
                RowIndex = RowIndex + 1;
            }
        }
    }
    // save the required information 
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int rowCount = grvAuditTrail.Rows.Count;
        string tableNames = string.Empty;
        // iterate through the each row of the GridView
        foreach (GridViewRow gvRow in grvAuditTrail.Rows)
        {
            CheckBox cBoxTable = (CheckBox)grvAuditTrail.Rows[gvRow.RowIndex].FindControl("chkListTable");
            // get the table name
            string tableName = grvAuditTrail.Rows[gvRow.RowIndex].Cells[1].Text;
            string tableDescription = grvAuditTrail.Rows[gvRow.RowIndex].Cells[2].Text;
            bool tableStatus = false;
            // if Table check box selected
            if (cBoxTable.Checked)
            {
                string insertFields = string.Empty;
                string updateFields = string.Empty;
                string deleteFields = string.Empty;
                Int32 insertBitCount = 0, updateBitCount = 0, deleteBitCount = 0;
                Int32 fieldIndex = gvRow.RowIndex;
                fieldIndex = fieldIndex + 1;
                decimal auditTableId = 0;
                // again iteratet through the respective table's each field
                while (fieldIndex < rowCount && grvAuditTrail.Rows[fieldIndex].Cells[1].Text == "&nbsp;")
                {
                    bool insertBit = false;
                    bool updateBit = false;
                    bool deleteBit = false;
                    bool fieldStatus = false;
                    CheckBox cBoxInsert = (CheckBox)grvAuditTrail.Rows[fieldIndex].FindControl("chkListInsertField");
                    CheckBox cBoxUpdate = (CheckBox)grvAuditTrail.Rows[fieldIndex].FindControl("chkListUpdateField");
                    CheckBox cBoxDelete = (CheckBox)grvAuditTrail.Rows[fieldIndex].FindControl("chkListDeleteField");
                    // get the tables's columns name
                    string fieldName = grvAuditTrail.Rows[fieldIndex].Cells[3].Text;
                    string fieldDescription = grvAuditTrail.Rows[fieldIndex].Cells[4].Text;
                    // set the insertbit status of the table and append insert field names
                    if (cBoxInsert.Checked)
                    {
                        insertBit = true;
                        if (insertFields == string.Empty)
                        {
                            insertFields = fieldName;
                        }
                        else
                        {
                            insertFields = insertFields + "," + fieldName;
                        }
                        insertBitCount = insertBitCount + 1;
                    }
                    // set the updatebit status of the table and append update field names
                    if (cBoxUpdate.Checked)
                    {
                        updateBit = true;
                        if (updateFields == string.Empty)
                        {
                            updateFields = fieldName;
                        }
                        else
                        {
                            updateFields = updateFields + "," + fieldName;
                        }
                        updateBitCount = updateBitCount + 1;
                    }
                    // set the deletebit status of the table and append delete field names
                    if (cBoxDelete.Checked)
                    {
                        deleteBit = true;
                        if (deleteFields == string.Empty)
                        {
                            deleteFields = fieldName;
                        }
                        else
                        {
                            deleteFields = deleteFields + "," + fieldName;
                        }
                        deleteBitCount = deleteBitCount + 1;
                    }
                    // if any one of the operation is selected then enable table and field status for Audit Trail
                    if (insertBit == true || updateBit == true || deleteBit == true)
                    {
                        tableStatus = true;
                        fieldStatus = true;
                        // run insert sp for table name
                        if (tableName != string.Empty)
                        {
                            DataSet dsAuditTableId = objAuditEvent.InsertAuditTable(tableName, tableDescription, tableStatus);
                            tableName = string.Empty;
                            auditTableId = Convert.ToDecimal(dsAuditTableId.Tables[0].Rows[0][0]);
                        }
                        // run insert sp for fields related to previously added table name's id
                        objAuditEvent.InsertAuditField(auditTableId, fieldName, fieldDescription, insertBit, updateBit, deleteBit, fieldStatus);
                    }
                    // no one operation is selected then enable table status and disable field status for Audit Trail
                    else
                    {
                        tableStatus = true;
                        fieldStatus = false;
                        // run insert sp for table name
                        if (tableName != string.Empty)
                        {
                            DataSet dsAuditTableId = objAuditEvent.InsertAuditTable(tableName, tableDescription, tableStatus);
                            tableName = string.Empty;
                            auditTableId = Convert.ToDecimal(dsAuditTableId.Tables[0].Rows[0][0]);
                        }
                        // run insert sp for fields related to previously added table name's id
                        objAuditEvent.InsertAuditField(auditTableId, fieldName, fieldDescription, insertBit, updateBit, deleteBit, fieldStatus);
                    }
                    fieldIndex = fieldIndex + 1;
                }
                // run trigger sp with required table name, insert fields, update fields and delete fields
                string tableNameforTrigger = grvAuditTrail.Rows[gvRow.RowIndex].Cells[1].Text;
                objAuditEvent.TriggerCreation(tableNameforTrigger, insertFields, updateFields, deleteFields);
            }
            // if table check box is unselected
            else
            {
                if (grvAuditTrail.Rows[gvRow.RowIndex].Cells[1].Text != "&nbsp;")
                {
                    Int32 fieldIndex = gvRow.RowIndex;
                    fieldIndex = fieldIndex + 1;
                    decimal auditTableId = 0;
                    while (fieldIndex < rowCount && grvAuditTrail.Rows[fieldIndex].Cells[1].Text == "&nbsp;")
                    {
                        bool insertBit = false;
                        bool updateBit = false;
                        bool deleteBit = false;
                        bool fieldStatus = false;
                        string fieldName = grvAuditTrail.Rows[fieldIndex].Cells[3].Text;
                        string fieldDescription = grvAuditTrail.Rows[fieldIndex].Cells[4].Text;
                        // run insert sp for table name
                        if (tableName != string.Empty)
                        {
                            DataSet dsAuditTableId = objAuditEvent.InsertAuditTable(tableName, tableDescription, tableStatus);
                            tableName = string.Empty;
                            auditTableId = Convert.ToDecimal(dsAuditTableId.Tables[0].Rows[0][0]);
                        }
                        // run insert sp for fields related to previously added table name's id
                        objAuditEvent.InsertAuditField(auditTableId, fieldName, fieldDescription, insertBit, updateBit, deleteBit, fieldStatus);
                        fieldIndex = fieldIndex + 1;
                    }
                    // run trigger sp with required table name, insert fields, update fields and delete fields
                    string tableNameforTrigger = grvAuditTrail.Rows[gvRow.RowIndex].Cells[1].Text;
                    objAuditEvent.TriggerCreation(tableNameforTrigger, "", "", "");
                }
            }
        }
        // script to show alert 
        string strScript = "<script language='javascript' type='text/javascript'>";
        strScript += "alert('" + rm.GetString("Alert_AuditTrailhasbeenconfigured", ci) + "');";
        strScript += "location.replace('System_AuditTrail.aspx')";
        strScript += " </script>";
        this.RegisterStartupScript("setScript", strScript);
    }
    #endregion
}