Tuesday, January 21, 2014

Disable Browser Back Button Functionality using JavaScript


In this article, I am discussing how to prevent user from navigate to previous page using back button of the browse.
One cannot disable the browser back button functionality only thing that can be done is prevent them.
Below is the JavaScript snippets that needs to be placed in the head section of the page where you don’t want the user to revisit using the back button

<script type = "text/javascript" >
   function preventBack(){window.history.forward();}
    setTimeout("preventBack()", 0);
    window.onunload=function(){null};
</script>

Tuesday, January 7, 2014

Change All values in specific column in Datatable

 
    DataTable dt = new DataTable(); 
    DataRow[] rowList = dt.Select("Item_Id IS NOT NULL");
    foreach (DataRow dr in rowList)
       dr["employee_id"] = "NewValue";

Monday, January 6, 2014

Get Hierarchy from SQL DATABASE Tables

CREATE PROCEDURE SP_Fin_Get_All_AccountHeads_Group_Hierarchy
    @Group VARCHAR(100),
    @CompID VARCHAR(100)
AS
---SP_Fin_Get_All_AccountHeads_Group_Hierarchy 'Admnistrative Expenses', 'ANOOS'
BEGIN
DECLARE    @primary_account_head_group_id VARCHAR(100)
DECLARE @MaxDepth INT
    DECLARE @CurrentFiscal VARCHAR(50)
    SET @MaxDepth = 6;   
   
    ;WITH LedgerList AS(
                    SELECT isnull(account_heads_parent_id,0) AS ParentId,
                            account_heads_id AS Id,       
                            account_heads_code AS Code,
                            description AS Description,   1 AS Hierarchy
                    FROM fin_account_heads
                    WHERE account_heads_parent_id is null
                    AND primary_account_head_group_id=(select account_heads_group_id    from               fin_account_heads_group where account_heads_group_name =@Group)
                    and company_code = @CompID
                    AND is_active=1
                   
                   UNION ALL

                    SELECT AH.account_heads_parent_id AS ParentId,
                            AH.account_heads_id AS Id,       
                            AH.account_heads_code AS Code,
                            AH.description AS Description,
                            LedgerList.Hierarchy + 1 AS Hierarchy
                    FROM fin_account_heads AH
                    INNER JOIN LedgerList
                                ON AH.account_heads_parent_id = LedgerList.ID
                    WHERE AH.company_code = @CompID
                            AND LedgerList.Hierarchy < @MaxDepth
                    AND is_active=1
                    )   
                    SELECT * FROM LedgerList                   
End

Populate TreeView From DataBase Using Recursive Function

Table Name: accountheads

IID Name ParentID
1 Test01 0
2 Test001 1
3 Test002 1
4 Test02 0
5 Test001 4
6 Test002 4
7 Test003 6


protected DataSet PDataset(string select_statement)
{
            SqlConnection _con = new SqlConnection("DataSource(Local); 
            database =DATABASE NAME ; UserID=sa;Password=******");
            SqlDataAdapter ad = new SqlDataAdapter(select_statement, _con);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            _con.Close();
            return ds;
}


 public void Load_tree()
 {
            DataSet PrSet = PDataset("SELECT * FROM accountheads");
            treeView1.Nodes.Clear();
            foreach (DataRow dr in PrSet.Tables[0].Rows)
            {
                if ((int)dr["ParentID"] ==  0 )
                {
                    TreeNode tnParent = new TreeNode();
                    tnParent.Text = dr["Name"].ToString();
                    string value = dr["ID"].ToString();
                    tnParent.Expand();
                    treeView1.Nodes.Add(tnParent);
                    FillChild(tnParent, value);
                 }
             }
}

        public int FillChild(TreeNode parent,string ID)
        {

            DataSet ds = PDataset("SELECT * FROM tree WHERE ParentID =" + ID );
            if (ds.Tables[0].Rows.Count > 0)
            {

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    TreeNode child = new TreeNode();
                    child.Text = dr["Name"].ToString().Trim();
                    string temp = dr["ID"].ToString();
                    child.Collapse();
                    parent.Nodes.Add(child);
                    FillChild(child, temp);
                }
                return 0;
            }
            else 
            {
                return 0;
            }
                                                          
        }