Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
Reply With Quote
  #2 (permalink)  
Old September 16th, 2009, 06:15 AM
Authorized User
Points: 198, Level: 3
Points: 198, Level: 3 Points: 198, Level: 3 Points: 198, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: singapore, singapore, Singapore.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:27 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.