Showing posts with label VS2005. Show all posts
Showing posts with label VS2005. Show all posts

February 17, 2009

Exploring Treeview in VS005

Here is a scenario of displaying employee hierarchy information in a treeview control. This application supports tree view binding in vs2005 up to any levels. We can make use of existing vs2005 treeview for displaying employee hierarchy in our organization. Basically we need to loop through the tree control recursively and use the same logic to bind tree nodes up-to n levels.
I have created table named Employee in SqlServer 2005 EmployeeID, EMPName,ManagerId as columns. Set EmployeeID as PKEY, and ManagerID as foreign key for the same table.
Here is the sample data in the database.


Open VS 2005 and create new web project give name as EMPHierarchy
Drag and drop Treeview control and set ExpandDepth property to 0.Expnadcollapse to true. ShowLines to true.
ID="TreeView1"
ExpandDepth="0"
PopulateNodesFromClient="true"
ShowLines="true"
ShowExpandCollapse="true"
runat="server" OnTreeNodePopulate="TreeView1_TreeNodePopulate1" />.

Our first logic is to populate nodes in the root level when the page loads. Here is code for it.
//load for the page code to populate main nodes is called here.
protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
PopulateMainLevel();

}
Populate mainlevel where manager doesnot have any reporting means managerid is null
///
/// function to populate main level nodes
///

private void PopulateMainLevel()
{
SqlConnection objConn = new SqlConnection(
"server=vamshi;Trusted_Connection=true;DATABASE=Employee");
SqlCommand cmd = new SqlCommand("select employeeid,empname,(select count(*) FROM Employee WHERE managerid=e.employeeid) childnodecount FROM Employee e where ManagerId IS NULL", objConn);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

PopulateNodes(dt, TreeView1.Nodes);
}

Here is the main important logic. The PopulateNodes method iterates through all rows in the DataTable. If given node has child nodes, it sets the PopulateOnDemand property of the TreeNode object according to that.
///
/// Code to populate nodes
///

///
///
private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{

foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["empname"].ToString();
tn.Value = dr["employeeid"].ToString();
nodes.Add(tn);
int i = Convert.ToInt32(dr["childnodecount"].ToString());
// If node has child nodes, then enable on-demand populating
if (i > 0)
tn.PopulateOnDemand = true;

}
}

Now we need to populate sublevels in the treenode click. We have to place logic in TreenodePopulate.

protected void TreeView1_TreeNodePopulate1(object sender, TreeNodeEventArgs e)
{
PopulateSubLevel(Convert.ToInt32(e.Node.Value), e.Node);


}

///
/// function to populate sublevel nodes
///

///
///

private void PopulateSubLevel(int parentid, TreeNode parentNode)
{

SqlConnection objConn = new SqlConnection(
"server=vamshi;Trusted_Connection=true;DATABASE=Employee");
SqlCommand cmd = new SqlCommand("select employeeid,empname,(select count(*) FROM employee WHERE employeeid=e.employeeid) childnodecount FROM Employee e where managerID=@parentID", objConn);

cmd.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
}


HERE is the output.



Conclusion: Based on the hierarchy in organization Populating treeview in vs2005 is explained.

January 24, 2008

HTML source validation in Visual web developer 2005

It’s a good practice be to find HTML errors or warnings carefully and fix them. For example a missing the closing of TD could result in a warning, but it would affect the entire code to dysfunctional. You are unable to find out where is the root cause upon debugging. So its always nice practice not to ignore warnings or HTML errors.

To set these validations



Go to tools menu-> Options menu item in VS or Visual Web Developer.
Select the Text Editor->Html->Validation tree option in the left-hand side of the options window,
Select the target as XHTML 1.0 transitional (Netscape7, opera 7, InternetExplorer 6) and check the “Show Errors” checkbox:


Note: HTML validation errors will not block a web application from compiling / running.