By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Specified cast is not valid

P: 1
Thank for your answer. :-)
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 271: Microsoft.Web.UI.WebControls.TreeNode iNode = null;
Line 272:
Line 273: iNode = (Microsoft.Web.UI.WebControls.TreeNode) N;
Line 274:
Line 275: foreach ( Microsoft.Web.UI.WebControls.TreeNode tempNode in iNode.Nodes )


Source File: c:\inetpub\wwwroot\payroll_cs\frmmenu.aspx.cs Line: 273

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
Payroll_CS.frmMenu.CheckEmptyNode(Object N) in c:\inetpub\wwwroot\payroll_cs\frmmenu.aspx.cs:273
Payroll_CS.frmMenu.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\payroll_cs\frmmenu.aspx.cs:106
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
Expand|Select|Wrap|Line Numbers
  1. call CheckEmptyNode (treeview)
  2.  
  3.  
  4. public void CheckEmptyNode( Object N ) 
  5.         {             
  6.             Microsoft.Web.UI.WebControls.TreeNode menuNode = null; 
  7.             Microsoft.Web.UI.WebControls.TreeNode iNode = null;
  8.  
  9.             iNode = (Microsoft.Web.UI.WebControls.TreeNode) N;
  10.  
  11.             foreach ( Microsoft.Web.UI.WebControls.TreeNode tempNode in iNode.Nodes ) 
  12.             { 
  13.                 menuNode = tempNode;                
  14.                 if ( menuNode.Nodes.Count > 0 ) 
  15.                 { 
  16.                     CheckEmptyNode( menuNode ); 
  17.                 } 
  18.                 else 
  19.                 { 
  20.                     if ( menuNode.NavigateUrl.Trim() == "" ) 
  21.                     {                         
  22.                         itemNode.Nodes.Remove( menuNode ); 
  23.  
  24.                         if ( itemNode.Nodes.Count > 0 ) 
  25.                         { 
  26.                             CheckEmptyNode( N ); 
  27.                         }                         
  28.                         return;                         
  29.                     }                     
  30.                 }                 
  31.             }            
  32.         }
  33.  
May 20 '09 #1
Share this Question
Share on Google+
2 Replies


Curtis Rutland
Expert 2.5K+
P: 3,256
From a quick glance, I'd say that you're passing an Object to your method that can't be cast as a Microsoft.Web.UI.WebControls.TreeNode.

That's the only cast you're doing, and the error message says "invalid cast."
May 20 '09 #2

Plater
Expert 5K+
P: 7,872
Indeed, N is not a Microsoft.Web.UI.WebControls.TreeNode nor a class that derives from Microsoft.Web.UI.WebControls.TreeNode.

OR Whats contained in N.Nodes is not garunteed to be reducable to a Microsoft.Web.UI.WebControls.TreeNode
May 21 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.