This is a migrated thread and some comments may be shown as answers.

RadTree data binding issue

4 Answers 134 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Web Services
Top achievements
Rank 2
Web Services asked on 31 Jul 2009, 04:19 PM

I am trying to databind a rad tree to a table that I create from a database. I followed the video example and changed the way I populate the table, but I know I am getting the correct data back. However, I get this error

Server Error in '/' Application.

Object reference not set to an instance of an object.

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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 14:         
Line 15: ' CreateChildControls the Table and assign the datasource
Line 16: RadTreeView1.DataSource = CreateTable()
Line 17: ' Assign the data fields
Line 18: RadTreeView1.DataFieldID = "ID"

Source File: c:\websites\ClickableCommunity\ClickableCommunity.Master    Line: 16

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
ASP.clickablecommunity_master.Page_Load(Object sender, EventArgs e) in c:\websites\ClickableCommunity\ClickableCommunity.Master:16
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Control.LoadRecursive() +131
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436


Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433

Here is the vb code

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
        If Not Page.IsPostBack Then
        
            ' CreateChildControls the Table and assign the datasource
            RadTreeView1.DataSource = CreateTable()
            ' Assign the data fields
            RadTreeView1.DataFieldID = "ID"
            RadTreeView1.DataFieldParentID = "ParentID"
            RadTreeView1.DataTextField = "Text"
            ' Bind the data
            RadTreeView1.DataBind()
            
        End If
    End Sub 'page_load
    
    'this function creates a datatable for the rad
    'tree that is in the events panel
    Private Function CreateTable() As DataTable
        'set up my connection to the database
        Dim conn As New SqlConnection
        conn.ConnectionString = ConfigurationManager.ConnectionStrings("connectionString").ConnectionString

        Dim table As DataTable = New DataTable()
        table.Columns.Add("ID")
        table.Columns.Add("ParentID")
        table.Columns.Add("Text")

        'this is a counter for the id fields
        'of the data table
        Dim count As Integer = 0
        
        'this adds the parent categories to the 
        'datatable
        Dim categoryQuery As New SqlCommand
        conn.Open()
        categoryQuery.Connection = conn
        categoryQuery.CommandText = "select CategoryId, Name from categories"
        Dim cdr As SqlDataReader = categoryQuery.ExecuteReader()
        While cdr.Read()
            
            If (cdr.Item(0).ToString <> "") Then
                
                table.Rows.Add(New String() {count.ToString, count.ToString, cdr.Item(1).ToString})
                
                'we have to increment count to keep an id for the data table
                count = count + 1
                
            End If
            
        End While 'while cdr.Read()
        conn.Close()
        conn.Open()
        'this adds the events with their parent category
        'id to the datatable
        Dim eventQuery As New SqlCommand
        eventQuery.Connection = conn
        eventQuery.CommandText = "SELECT eventid, FK_CategoryID, name from events"
        Dim edr As SqlDataReader = eventQuery.ExecuteReader()
        While edr.Read()
            
            If (edr.Item(0).ToString <> "") Then
                
                table.Rows.Add(New String() {count.ToString(), edr.Item(1).ToString, edr.Item(2).ToString})
                
                'we have to increment count to keep an id for the data table
                count = count + 1
                
            End If
            
        End While 'while edr.Read()
        
        Return table

    End Function 'CreateGenreTable

What I can't understand is why it says that createtable is null. I am returning the table and I have outputted the data that I am adding to the table. Any ideas? Thanks

4 Answers, 1 is accepted

Sort by
0
Web Services
Top achievements
Rank 2
answered on 31 Jul 2009, 07:19 PM
I figured it out. Since my page was a master page, I had the rad tree in a contentplaceholdertag and you can't do that apparently. I have a new question now. I am trying to move this rad tree into a rad pane but am getting this error.
scription: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'RadTreeView1' is not declared.

Source Error:

Line 16:             'here is where the error is. it is saying that
Line 17:             'createtable is null
Line 18:             RadTreeView1.DataSource = CreateTable()
Line 19:             RadTreeView1.DataFieldID = "ID"
Line 20:             RadTreeView1.DataFieldParentID = "ParentID"

Here is my whole page
<%@ Master Language="VB" AutoEventWireup="false" Debug="True" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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">
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.Sql" %>
<%@ import namespace="System.Data.SqlClient" %>

<script runat="server">
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
        If Not Page.IsPostBack Then
                        
            'here is where the error is. it is saying that
            'createtable is null
            RadTreeView1.DataSource = CreateTable()
            RadTreeView1.DataFieldID = "ID"
            RadTreeView1.DataFieldParentID = "ParentID"
            RadTreeView1.DataTextField = "Text"
            RadTreeView1.DataBind()
            
        End If
    End Sub 'page_load
    
    'this function creates a datatable for the rad
    'tree that is in the events panel
    Private Function CreateTable() As DataTable
        'set up my connection to the database
        Dim conn As New SqlConnection
        conn.ConnectionString = ConfigurationManager.ConnectionStrings("connectionString").ConnectionString

        Dim table As DataTable = New DataTable()
        table.Columns.Add("ID")
        table.Columns.Add("ParentID")
        table.Columns.Add("Text")

        'this is a counter for the id fields
        'of the data table
        Dim count As Integer = 1
        
        'this adds the parent categories to the
        'datatable
        Dim categoryQuery As New SqlCommand
        conn.Open()
        categoryQuery.Connection = conn
        categoryQuery.CommandText = "select CategoryId, Name from categories"
        Dim cdr As SqlDataReader = categoryQuery.ExecuteReader()
        While cdr.Read()
            
            If (cdr.Item(0).ToString <> "") Then
                
                table.Rows.Add(New String() {count.ToString, Nothing, cdr.Item(1).ToString})
                'we have to increment count to keep an id for the data table
                count = count + 1
                
            End If
            
        End While 'while cdr.Read()
        conn.Close()
        conn.Open()
        'this adds the events with their parent category
        'id to the datatable
        Dim eventQuery As New SqlCommand
        eventQuery.Connection = conn
        eventQuery.CommandText = "SELECT eventid, FK_CategoryID, name from events"
        Dim edr As SqlDataReader = eventQuery.ExecuteReader()
        While edr.Read()
            
            If (edr.Item(0).ToString <> "") Then
                
                table.Rows.Add(New String() {count.ToString(), edr.Item(1).ToString, edr.Item(2).ToString})
                'we have to increment count to keep an id for the data table
                count = count + 1
                
            End If
            
        End While 'while edr.Read()
               
        Return table

    End Function 'CreateGenreTable

</script>

<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadPanelBar ID="RadPanelBar1" runat="server" Skin="Web20" ExpandMode="SingleExpandedItem">
        <CollapseAnimation Type="None" Duration="100"></CollapseAnimation>
        <Items>
            <telerik:RadPanelItem runat="server" Text="Events" Expanded="true">
                <Items>
                    <telerik:RadPanelItem>
                        <ItemTemplate>
                            <telerik:RadTreeView ID="RadTreeView1" runat="server" Skin="Web20">
                                <CollapseAnimation Type="OutQuint" Duration="100"></CollapseAnimation>
                                <ExpandAnimation Duration="100"></ExpandAnimation>
                            </telerik:RadTreeView>
                        </ItemTemplate>
                    </telerik:RadPanelItem>
                </Items>
            </telerik:RadPanelItem>
            <telerik:RadPanelItem runat="server" Text="Search">
                <Items>
                    <telerik:RadPanelItem runat="server" Text="Child RadPanelItem 1">
                    </telerik:RadPanelItem>
                    <telerik:RadPanelItem runat="server" Text="Child RadPanelItem 2">
                    </telerik:RadPanelItem>
                </Items>
            </telerik:RadPanelItem>
            <telerik:RadPanelItem runat="server" Text="Administration">
            </telerik:RadPanelItem>
        </Items>
        <ExpandAnimation Type="None" Duration="100"></ExpandAnimation>
    </telerik:RadPanelBar>
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

0
Veselin Vasilev
Telerik team
answered on 05 Aug 2009, 12:07 PM
Hello,

Actually the error says: RadTreeView1 is not declared.

This is because the ClientID of RadTreeView1 is changed when it is placed in the ItemTemplate of RadPanelBarItem. You can access the treeview like this:

RadTreeView tree = RadPanelBar1.FindItemByText("Events").Items[0].FindControl("RadTreeView1"as RadTreeView; 
        if (tree != null
            tree.Nodes.Add(new RadTreeNode("Root node")); 


Greetings,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Web Services
Top achievements
Rank 2
answered on 05 Aug 2009, 01:48 PM
Do you think you could help me with a related problem. I have the treeview getting populated within the panel, but the rad panel with the tree view won't collapse. The other two with static content behave properly, but the one with the tree view just stays open. You can go here to check out what I mean. http://alpha.clickablecommunity.com/
Here is my aspx page.

<%

@ Master Language="VB" AutoEventWireup="false" CodeBehind="MasterPage.master.vb" Inherits="ClickableCommunity.MasterPage" %>

 

<%

@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

 

<!

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">

 

<

html xmlns="http://www.w3.org/1999/xhtml" >

 

<%

@ import namespace="System.Data" %>

 

<%

@ import namespace="System.Data.Sql" %>

 

<%

@ import namespace="System.Data.SqlClient" %>

 

<%

@ import namespace="Telerik.Web.UI" %>

 

<

script runat="server">

 

 

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

 

 

If Not Page.IsPostBack Then

 

 

 

Dim tree As RadTreeView = DirectCast(RadPanelBar1.Items(0).FindControl("RadTreeView1"), RadTreeView)

 

 

'here is where the error is. it is saying that

 

 

'createtable is null

 

tree.DataSource = CreateTable()

tree.DataFieldID =

"ID"

 

tree.DataFieldParentID =

"ParentID"

 

tree.DataTextField =

"Text"

 

tree.DataBind()

 

 

End If

 

 

End Sub 'page_load

 

 

 

'this function creates a datatable for the rad

 

 

'tree that is in the events panel

 

 

Private Function CreateTable() As DataTable

 

 

'set up my connection to the database

 

 

Dim conn As New SqlConnection

 

conn.ConnectionString = ConfigurationManager.ConnectionStrings(

"connectionString").ConnectionString

 

 

Dim table As DataTable = New DataTable()

 

table.Columns.Add(

"ID")

 

table.Columns.Add(

"ParentID")

 

table.Columns.Add(

"Text")

 

 

'this is a counter for the id fields

 

 

'of the data table

 

 

Dim count As Integer = 1

 

 

 

'this adds the parent categories to the

 

 

'datatable

 

 

Dim categoryQuery As New SqlCommand

 

conn.Open()

categoryQuery.Connection = conn

categoryQuery.CommandText =

"select CategoryId, Name from categories"

 

 

Dim cdr As SqlDataReader = categoryQuery.ExecuteReader()

 

 

While cdr.Read()

 

 

 

If (cdr.Item(0).ToString <> "") Then

 

 

table.Rows.Add(

New String() {count.ToString, Nothing, cdr.Item(1).ToString})

 

 

'we have to increment count to keep an id for the data table

 

count = count + 1

 

 

End If

 

 

 

End While 'while cdr.Read()

 

conn.Close()

conn.Open()

 

'this adds the events with their parent category

 

 

'id to the datatable

 

 

Dim eventQuery As New SqlCommand

 

eventQuery.Connection = conn

eventQuery.CommandText =

"SELECT eventid, FK_CategoryID, name from events"

 

 

Dim edr As SqlDataReader = eventQuery.ExecuteReader()

 

 

While edr.Read()

 

 

 

If (edr.Item(0).ToString <> "") Then

 

 

table.Rows.Add(

New String() {count.ToString(), edr.Item(1).ToString, edr.Item(2).ToString})

 

 

'we have to increment count to keep an id for the data table

 

count = count + 1

 

 

End If

 

 

 

End While 'while edr.Read()

 

 

 

Return table

 

 

End Function 'CreateGenreTable

 

 

</

script>

 

<

head runat="server">

 

 

<link href="ClickableCommunity.css" rel="stylesheet" type="text/css" />

 

 

<title></title>

 

 

<asp:ContentPlaceHolder ID="head" runat="server">

 

 

</asp:ContentPlaceHolder>

 

</

head>

 

<

body id="MasterBody" runat="server" style="margin: 0px; height: 100%; overflow: hidden;" scroll="no">

 

 

<form id="Form1" method="post" runat="server" style="height: 100%">

 

 

<asp:ScriptManager ID="ScriptManager1" runat="server">

 

 

</asp:ScriptManager><br />

 

 

<asp:ContentPlaceHolder ID="header" runat="server">

 

 

<div id="topDiv"></div>

 

 

</asp:ContentPlaceHolder>

 

 

<telerik:RadSplitter ID="RadSplitter1" runat="server" Orientation="Vertical" Width="100%"

 

 

Height="100%" CssClass="AllContent">

 

 

<telerik:RadPane ID="LeftRadPane1" runat="server" Width="220px" Scrolling="Both" BorderStyle="None" BorderSize="0">

 

 

<div id="leftPanel">

 

 

<telerik:RadPanelBar ID="RadPanelBar1" runat="server" Skin="Web20" ExpandMode="SingleExpandedItem"

 

 

AllowCollapseAllItems="true" Width="220px">

 

 

<CollapseAnimation Type="None" Duration="100"></CollapseAnimation>

 

 

<Items>

 

 

<telerik:RadPanelItem runat="server" Text="Events">

 

 

<ItemTemplate>

 

 

<telerik:RadTreeView ID="RadTreeView1" runat="server" Skin="Web20" CssClass="leftPanelContent">

 

 

</telerik:RadTreeView>

 

 

</ItemTemplate>

 

 

</telerik:RadPanelItem>

 

 

<telerik:RadPanelItem runat="server" Text="Calendar" ChildGroupCssClass="leftPanelContent">

 

 

<Items>

 

 

<telerik:RadPanelItem runat="server" Text="Child RadPanelItem 1">

 

 

</telerik:RadPanelItem>

 

 

<telerik:RadPanelItem runat="server" Text="Child RadPanelItem 2">

 

 

</telerik:RadPanelItem>

 

 

</Items>

 

 

</telerik:RadPanelItem>

 

 

<telerik:RadPanelItem runat="server" Text="Administrator" ChildGroupCssClass="leftPanelContent">

 

 

<Items>

 

 

<telerik:RadPanelItem runat="server" Text="Child RadPanelItem 1">

 

 

</telerik:RadPanelItem>

 

 

<telerik:RadPanelItem runat="server" Text="Child RadPanelItem 2">

 

 

</telerik:RadPanelItem>

 

 

</Items>

 

 

</telerik:RadPanelItem>

 

 

</Items>

 

 

<ExpandAnimation Type="None" Duration="100"></ExpandAnimation>

 

 

</telerik:RadPanelBar>

 

 

</div>

 

 

</telerik:RadPane>

 

 

<telerik:RadSplitBar runat="server" ID="RadSplitBar2" CollapseMode="Forward" EnableResize="false" ForeColor='Green'/>

 

 

<telerik:RadPane ID="RightRadPane1" runat="server" CssClass="RightPanelPadding"

 

 

Scrolling="Both" BorderStyle="None" BorderSize="0">

 

 

<asp:ContentPlaceHolder ID="MainContent" runat="server"></asp:ContentPlaceHolder>

 

 

</telerik:RadPane>

 

 

</telerik:RadSplitter>

 

 

</form>

 

</

body>

 

</

html>

 

0
Accepted
Veselin Vasilev
Telerik team
answered on 10 Aug 2009, 11:21 AM
Hi,

PROBLEM

Root items that have ItemTemplate defined cannot be collapsed. This is by design and can be observed on our online demo.

SOLUTION

Add an additional item with no text as a child item of the root item and set its ItemTemplate.

EXAMPLE

The root item of this panelbar cannot be collapsed:

<telerik:RadPanelBar ID="RadPanelBar2" runat="server" ExpandMode="FullExpandedItem">
   
<Items>
       
<telerik:RadPanelItem runat="server" Text="Root RadPanelItem1">
           
<ItemTemplate>
               
<telerik:RadComboBox ID="RadComboBox1" runat="server">
               
</telerik:RadComboBox>
           
</ItemTemplate>
       
</telerik:RadPanelItem>
   
</Items>
</
telerik:RadPanelBar>

 

The root item of this panelbar can be collapsed:

<telerik:RadPanelBar ID="RadPanelBar1" runat="server">
   
<Items>
       
<telerik:RadPanelItem runat="server" Text="Root RadPanelItem1">
           
<Items>
               
<telerik:RadPanelItem>
                   
<ItemTemplate>
                       
<telerik:RadComboBox ID="RadComboBox1" runat="server">
                       
</telerik:RadComboBox>
                   
</ItemTemplate>
               
</telerik:RadPanelItem>
           
</Items>
       
</telerik:RadPanelItem>
   
</Items>
</
telerik:RadPanelBar>


Sincerely yours,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
TreeView
Asked by
Web Services
Top achievements
Rank 2
Answers by
Web Services
Top achievements
Rank 2
Veselin Vasilev
Telerik team
Share this question
or