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: |
Source File: c:\websites\ClickableCommunity\ClickableCommunity.Master Line: 16
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] |
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