System.NullReferenceException -- tag -- treeview

A

Adie

Hi, I can't seem to use the tag property of the treeview without craching
my app. The basic code:

while(SqlProducts.Read())
{
TreeNode childNode = new TreeNode(SqlProducts.GetString(0));
cnode.Tag = "Product Details";
parentNode.Nodes.Add(childNode);
}


private void treeProducts_AfterSelect( object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
text1.text = e.Node.Tag.ToString();
}

There is a QB article which addresses the issue in VB but not C#, anyone
know how to fix it?

Also, why is private void treeProducts_AfterSelect( ... ) called on the
load of the form?

Thanks!
 
A

Adie

Adie said:
Hi, I can't seem to use the tag property of the treeview without craching
my app. The basic code:

while(SqlProducts.Read())
{
TreeNode childNode = new TreeNode(SqlProducts.GetString(0));
cnode.Tag = "Product Details";
parentNode.Nodes.Add(childNode);
}


private void treeProducts_AfterSelect( object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
text1.text = e.Node.Tag.ToString();
}

Ok, it appears that I have to check for null

private void treeProducts_AfterSelect( object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
if (e.Node.Tag != null)
label1.Text = e.Node.Tag.ToString();
}

stupid nulls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top