Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 4th, 2009, 03:15 AM
Registered User
 
Join Date: Sep 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default treeview - database

Hi,

In my page, i used the tree view node retrieved from mysql db.

The following code generate error as Specified cast is not valid.

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeViewCS.aspx.cs" Inherits="TreeViewCS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeView Sample in C#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView
ID="TreeView1"
ExpandDepth="0"
PopulateNodesFromClient="true"
ShowLines="true"
ShowExpandCollapse="true"
runat="server"
OnTreeNodePopulate="TreeView1_TreeNodePopulate" />
</div>
</form>
</body>
</html>


C#


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;

public partial class TreeViewCS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateRootLevel();
}

private void PopulateRootLevel()
{
OdbcConnection objConn = new OdbcConnection(@"Driver={MySQL ODBC 3.51 Driver};server=localhost;user=root;password=root;o ption=3;DATABASE=ems");
OdbcCommand objCommand = new OdbcCommand(@"select id,title,imageurl,(select cast(count(*) as unsigned) FROM menu_details WHERE parentid=sc.id) as childnodecount FROM menu_details sc where parentID =0", objConn);
OdbcDataAdapter da = new OdbcDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);

}

private void PopulateSubLevel(int parentid,TreeNode parentNode)
{
OdbcConnection objConn = new OdbcConnection(@"Driver={MySQL ODBC 3.51 Driver};server=localhost;user=root;password=root;o ption=3;DATABASE=ems");
OdbcCommand objCommand = new OdbcCommand(@"select id,title,imageurl, (select cast(count(*) as unsigned) FROM menu_details WHERE parentid=sc.id) as childnodecount FROM menu_details sc where parentID=@parentID", objConn);
objCommand.Parameters.Add("@parentID", OdbcType.Int).Value = parentid;
OdbcDataAdapter da = new OdbcDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
}


protected void TreeView1_TreeNodePopulate(object sender,TreeNodeEventArgs e)
{
PopulateSubLevel(Int32.Parse(e.Node.Value),e.Node) ;
}

private void PopulateNodes(DataTable dt,TreeNodeCollection nodes)
{
foreach( DataRow dr in dt.Rows)
{
TreeNode tn=new TreeNode();
tn.Text = dr["title"].ToString();
tn.Value = dr["id"].ToString();
nodes.Add(tn);

//If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0); // Specified cast is not valid
}
}

}


Thanks in advance,
Murugavel
 
Old September 16th, 2009, 06:15 AM
Authorized User
 
Join Date: Apr 2008
Posts: 54
Thanks: 0
Thanked 4 Times in 4 Posts
Default

You try to use databinding rather than adding nodes.TreeView Asp.Net DataBinding from Dataset. I feel will be better since most of the time we deal with Dataset and databinding.
__________________
Pon Saravanan
VbKnowledgebase.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
TreeView Binding With DataBase sumith ASP.NET 2.0 Basics 2 September 16th, 2009 06:12 AM
Populating TreeView Control using Access Database dotnetDeveloper ADO.NET 0 November 18th, 2008 12:58 PM
TreeView - Please help dotnetprogrammer VS.NET 2002/2003 1 September 5th, 2005 09:02 AM
treeview and database zouky ADO.NET 1 October 8th, 2004 04:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.