Hi,
I hope someone could help with this error I'm getting. I'm new to ASP.NET and haven't a clue how to even begin debugging this. If you can offer any help, I'd appreciate it. FYI:I'm using Web Matrix not VS.Net and everything compiles ok.
Thank you!
Tomcat
HERE'S THE ERROR
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
LazyLoadingWeb.WebForm1.DisplayCategoryTree() +150
LazyLoadingWeb.WebForm1.Category_Selected(Object sender, EventArgs e) +92
System.Web.UI.WebControls.LinkButton.OnClick(Event Args e) +108
System.Web.UI.WebControls.LinkButton.System.Web.UI .IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +58
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1277
HERE'S THE CODE FOR NEWTREE.ASPX
<%@ Page Language="
VB" debug="true" autoeventwireup="false" Src="LazyLoading.aspx.
vb" Inherits="LazyLoadingWeb.WebForm1" %>
<%@ assembly name="VideoStoreDataModel.EnterpriseVB.VideoStore. Data" %>
<%@ assembly name="EnterpriseVB.VideoStore.Data" %>
<script runat="server">
' Insert page code here
'
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Translational//EN">
<html>
<head>
</head>
<body>
<form id="Form1" method="post" runat="server">
Hello
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tbody>
<tr>
<td valign="top" align="left">
<asp:table id="CategoryTree" runat="server"></asp:table>
</td>
<td valign="top" align="left" width="100%">
<asp:DataGrid id="VideoTapesGrid" runat="server" AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:BoundColumn DataField="VideoTapeID"></asp:BoundColumn>
<asp:BoundColumn DataField="Title"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
HERE'S THE CODE FOR LAZYLOADING.ASPX.
VB
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports VideoStoreDataModel.EnterPriseVB.VideoStore.Data
Imports EnterPriseVB.VideoStore.Data
Imports System.ComponentModel.Design
Imports System.Drawing
Namespace LazyLoadingWeb
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents VideoTapesGrid As System.Web.UI.WebControls.DataGrid
Protected WithEvents CategoryTree As System.Web.UI.WebControls.Table
Public Property SelectedCategoryID() As Integer
Get
If (("" + ViewState("SelCatID")) = "") Then
Return -1
End If
Return Convert.ToInt32("" + ViewState("SelCatID"))
End Get
Set(ByVal Value As Integer)
ViewState("SelCatID") = "" + Value.ToString()
End Set
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DisplayCategoryTree()
End Sub
Private Sub DisplayCategoryTree()
CategoryTree.Rows.Clear()
Dim dac As New VideoCategoryDataAccess()
Dim category As VideoCategory
category = dac.GetCategoryTree(1)
Dim depth as Integer
depth = GetDepth(category, 0)
RenderTree(category, depth, 0)
If (SelectedCategoryID <> -1) Then
Dim cCat As VideoCategory
cCat = cCat.FindCategoryByID(SelectedCategoryID)
VideoTapesGrid.DataSource = cCat.Videos
VideoTapesGrid.DataBind()
End If
End Sub
Public Function RenderTree(ByRef cat As VideoCategory, _
ByVal depth As Integer, ByVal currentDepth As Integer)
Dim tr As New TableRow()
Dim i As Integer
For i = 0 To currentDepth
Dim spacerCell As New TableCell()
spacerCell.Text = " "
spacerCell.Width = Unit.Pixel(10)
spacerCell.Height = Unit.Pixel(10)
spacerCell.BorderWidth = Unit.Pixel(0)
tr.Cells.Add(spacerCell)
Next
Dim descCell As New TableCell()
descCell.ColumnSpan = (depth - currentDepth) +1
Dim lb As New LinkButton()
lb.Text = cat.Description
lb.ID = "CAT" + cat.CategoryID.ToString()
AddHandler lb.Click, AddressOf Category_Selected
descCell.Controls.Add(lb)
If (Me.SelectedCategoryID = cat.CategoryID) Then
descCell.BackColor = System.Drawing.Color.Yellow
Else
descCell.BackColor = System.Drawing.Color.White
End If
descCell.Style.Add("white-space", "nowrap")
tr.Cells.Add(descCell)
CategoryTree.Rows.Add(tr)
currentDepth = currentDepth + 1
For i = 0 To cat.CountSubCategories() - 1
RenderTree(cat.GetSubCategory(i), depth, currentDepth)
Next
End Function
Public Sub Category_Selected(ByVal sender As Object, ByVal e As EventArgs)
Me.SelectedCategoryID = _
Convert.ToInt32((CType(sender, LinkButton)).ID.SubString(3))
Me.DisplayCategoryTree()
End Sub
Public Function GetDepth(ByVal cat As VideoCategory, _
ByVal depth As Integer) As Integer
Dim tDepth As Integer
Dim deepest As Integer
deepest = depth
Dim i As Integer
For i = 0 To cat.CountSubCategories() - 1
tDepth = GetDepth(cat.GetSubCategory(i), depth + 1)
If (tDepth > deepest) Then
deepest = tDepth
End If
Next
Return deepest
End Function
End Class
End Namespace
HERE'S THE CODE FOR EnterpriseVB.VideoStore.Data.
VB
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections
Imports VideoStoreDataModel.EnterpriseVB.VideoStore.Data
Namespace EnterpriseVB.VideoStore.Data
Public Class VideoCategory
Protected data As DataTable
Protected index As Integer
Protected subCategories As New ArrayList()
Protected Friend ReadOnly Property MyData() As DataRow()
Get
Dim myRow() As DataRow = {Me.data.Rows(index)}
Return myRow
End Get
End Property
Public Sub New(ByRef data As VideoCategoryData, ByVal index As Integer)
Me.data = data
Me.index = index
End Sub
Public Sub New()
Me.data = New VideoCategoryData()
data.Rows.Add(data.NewRow())
Me.index = 0
End Sub
Public Function GetColumn(ByRef ColumnName As String) As Object
Return data.Rows(index)(ColumnName)
End Function
Public Function SetColumn(ByRef ColumnName As String, ByRef ColumnValue As Object)
data.Rows(index)(ColumnName) = ColumnValue
End Function
Public Property CategoryID() As Int32
Get
If (GetColumn("CategoryID").GetType() Is Type.GetType("System.DBNull")) Then
Return -1
End If
Return CType(GetColumn("CategoryID"), Int32)
End Get
Set(ByVal Value As Int32)
SetColumn("CategoryID", Value)
End Set
End Property
Public Property Description() As String
Get
Return "" + GetColumn("Description")
End Get
Set(ByVal Value As String)
SetColumn("Description", Value)
End Set
End Property
Public Property ParentCategoryID() As Int32
Get
If (GetColumn("ParentCategoryID").GetType() Is Type.GetType("System.DBNull")) Then
Return -1
End If
Return CType(GetColumn("ParentCategoryID"), Decimal)
End Get
Set (ByVal Value As Int32)
SetColumn("ParentCategoryID", Value)
End Set
End Property
Public Property RootCategoryID() As Int32
Get
If (GetColumn("RootCategoryID").GetType() Is Type.GetType("System.DBNull")) Then
Return -1
End If
Return CType(GetColumn("RootCategoryID"), Int32)
End Get
Set (ByVal Value As Int32)
SetColumn("RootCategoryID", Value)
End Set
End Property
Public Function AddSubCategory(ByRef subCat As VideoCategory)
Me.subCategories.Add(subCat)
End Function
Public Function FindCategoryById(ByVal CatID As Integer) As VideoCategory
Return Me.FindCategoryByID(Me, catID)
End Function
Friend Function FindCategoryById(ByRef cat As VideoCategory, ByVal catID As Integer) As VideoCategory
If (catID = cat.CategoryID) Then
Return cat
End If
Dim i As Integer
For i = 0 to cat.CountSubCategories() -1
If (FindCategoryByID(cat.GetSubCategory(i), catID) Is Nothing) Then
Else
Return cat.GetSubCategory(i)
End If
Next
Return Nothing
End Function
Public Function CountSubCategories() As Int32
Return Me.subCategories.Count
End Function
Public Function GetSubCategory(ByVal i As Int32) As VideoCategory
Return CType(Me.subCategories(i), VideoCategory)
End Function
Dim vids as VideoTape()
Public ReadOnly Property Videos() as VideoTape()
Get
If (vids is Nothing) Then
Dim vDAC As New VideoTapeDataAccess()
vids = vDAC.GetAllVideoTapesInCategory(Me.CategoryID)
End If
Return Vids
End Get
End Property
End Class
Public Class VideoCategoryData
Inherits DataTable
Public Sub New()
MyBase.New("VideoCategory")
Me.Columns.Add("CategoryID", Type.GetType("System.Int32"))
Me.Columns.Add("Description", Type.GetType("System.String"))
Me.Columns.Add("ParentCategoryID", Type.GetType("System.Int32"))
Me.Columns.Add("RootCategoryID", Type.GetType("System.Int32"))
End Sub
End Class
Public Class VideoCategoryDataAccess
Public connectionString As String
Protected adapter As SqlDataAdapter
Protected loadAll As SqlDataAdapter
Public Sub New()
connectionString = "Data Source=ISA; Initial Catalog=WRC-SQL; User ID=sa; Password=meghan"
adapter = New SqlDataAdapter()
adapter.SelectCommand = New SqlCommand("VideoCategoryGetTree", New SqlConnection(connectionString))
adapter.SelectCommand.CommandType = CommandType.StoredProcedure
adapter.SelectCommand.Parameters.Add _
("@RootCategoryID", SqlDbType.Decimal, 0, "RootCategoryID")
End Sub
Public Function GetCategoryTree(ByVal rootCategoryID As Int32) As VideoCategory
Dim data As New VideoCategoryData()
adapter.SelectCommand.Parameters("@RootCategoryID" ).Value = rootCategoryID
adapter.Fill(data)
Dim categories As VideoCategory()
categories = GetVideoCategoryArrayFromData(data)
Dim i As Integer
Dim x As Integer
If (categories.Length = 0) Then
Return Nothing
End If
For i = 0 To categories.Length - 1
For x = 0 To categories.Length - 1
If (categories(i).CategoryID = categories(x).ParentCategoryID) Then
categories(i).AddSubCategory(categories(x))
End If
Next
Next
Return categories(0)
End Function
Public Shared Function GetVideoCategoryArrayFromData(ByRef data As VideoCategoryData) As VideoCategory()
Dim vArray(data.Rows.Count - 1) As VideoCategory
Dim i As Integer
For i = 0 To (data.Rows.Count - 1)
vArray(i) = New VideoCategory(data, i)
Next i
Return vArray
End Function
End Class
End Namespace
HERE'S THE CODE FOR VideoStoreDataModel.EnterpriseVB.VideoStore.Data.V B
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections
Imports System.Xml.Serialization
Namespace VideoStoreDataModel.EnterpriseVB.VideoStore.Data
Public Class VideoTapeData
Inherits DataTable
Public Sub New()
MyBase.New("VideoTape")
Me.Columns.Add("VideoTapeID", Type.GetType("System.Decimal"))
Me.Columns.Add("Title", Type.GetType("System.String"))
Me.Columns.Add("Description", Type.GetType("System.String"))
End Sub
End Class
Public Class VideoTape
Protected data As DataTable
Protected index As Integer
Protected Friend ReadOnly Property MyData() As DataRow()
Get
Dim myRow() As DataRow = {Me.data.Rows(index)}
Return myRow
End Get
End Property
Public Sub New(ByRef data As VideoTapeData, ByVal index As Integer)
Me.data = data
Me.index = index
End Sub
Public Function GetColumn(ByRef ColumnName As String) As Object
Return data.Rows(index)(ColumnName)
End Function
Public Function SetColumn(ByRef ColumnName As String, ByRef ColumnValue As Object)
data.Rows(index)(ColumnName) = ColumnValue
End Function
Public Property VideoTapeID() As Decimal
Get
If (GetColumn("VideoTapeID").GetType() Is Type.GetType("System.DBNull")) Then
Return -1
End If
Return CType(GetColumn("VideoTapeID"), Decimal)
End Get
Set(ByVal Value As Decimal)
SetColumn("VideoTapeID", Value)
End Set
End Property
Public Property Title() As String
Get
Return "" + GetColumn("Title")
End Get
Set(ByVal Value As String)
SetColumn("Title", Value)
End Set
End Property
Public Property Description() As String
Get
Return "" + GetColumn("Description")
End Get
Set(ByVal Value As String)
SetColumn("Description", Value)
End Set
End Property
End Class
Public Class VideoTapeDataAccess
Public connectionString As String
Protected adapter As SqlDataAdapter
Protected loadAll As SqlDataAdapter
Protected loadCat As SqlDataAdapter
Public Sub New()
connectionString = "Data Source=ISA; Initial Catalog=WRC-SQL; User ID=sa; Password=meghan"
adapter = New SqlDataAdapter()
adapter.SelectCommand = New SqlCommand("ap_VideoTapeLoadByID", _
New SqlConnection(connectionString))
adapter.SelectCommand.CommandType = CommandType.StoredProcedure
adapter.SelectCommand.CommandType = CommandType.StoredProcedure
adapter.SelectCommand.Parameters.Add _
("@VideoTapeID", SqlDbType.Decimal, 0, "VideoTapeID")
adapter.InsertCommand = New SqlCommand("ap_VideoTapeInsert", _
New SqlConnection(connectionString))
adapter.InsertCommand.CommandType = CommandType.StoredProcedure
adapter.InsertCommand.Parameters.Add("@VideoTapeID ", _
SqlDbType.Decimal, 0, "VideoTapeID")
adapter.InsertCommand.Parameters("@VideoTapeID").D irection = _
ParameterDirection.Output
adapter.InsertCommand.Parameters.Add("@Title", _
SqlDbType.Char, 50, "Title")
adapter.InsertCommand.Parameters.Add("@Description ", _
SqlDbType.Text, 0, "Description")
adapter.UpdateCommand = New SqlCommand("ap_VideoTapeUpdate", _
New SqlConnection(connectionString))
adapter.UpdateCommand.CommandType = CommandType.StoredProcedure
adapter.UpdateCommand.Parameters.Add("@VideoTapeID ", _
SqlDbType.Decimal, 0, "VideoTapeID")
adapter.UpdateCommand.Parameters.Add("@Title", _
SqlDbType.Char, 50, "Title")
adapter.UpdateCommand.Parameters.Add("@Description ", _
SqlDbType.Text, 0, "Description")
adapter.DeleteCommand = New SqlCommand("ap_VideoTapeDelete", _
New SqlConnection(connectionString))
adapter.SelectCommand.CommandType = CommandType.StoredProcedure
adapter.DeleteCommand.Parameters.Add("@VideoTapeID ", _
SqlDbType.Decimal, 0, "VideoTapeID")
loadAll = New SqlDataAdapter()
loadAll.SelectCommand = New SqlCommand("ap_VideoTapeLoadAll", _
New SqlConnection(connectionString))
loadAll.SelectCommand.CommandType = CommandType.StoredProcedure
loadCat = New SqlDataAdapter()
loadCat.SelectCommand = New SqlCommand("ap_VideoTapeLoadInCat", _
New SqlConnection(connectionString))
loadCat.SelectCommand.CommandType = CommandType.StoredProcedure
loadCat.SelectCommand.CommandType = CommandType.StoredProcedure
loadCat.SelectCommand.Parameters.Add _
("@CategoryID", SqlDbType.Decimal, 0, "CategoryID")
End Sub
Public Function GetVideoTapeByID(ByVal vtID As Decimal) As VideoTape
Dim data As New VideoTapeData()
adapter.SelectCommand.Parameters("@VideoTapeID").V alue = vtID
adapter.Fill(data)
If (data.Rows.Count < 1) Then
Return Nothing
End If
Dim vt As New VideoTape(data, 0)
Return vt
End Function
Public Function GetAllVideoTapes() As VideoTape()
Dim data As New VideoTapeData()
loadAll.Fill(data)
Return GetVideoTapeArrayFromData(data)
End Function
Public Shared Function GetVideoTapeArrayFromData(ByRef data As VideoTapeData) As VideoTape()
Dim vArray(data.Rows.Count - 1) As VideoTape
Dim i As Integer
For i = 0 To (data.Rows.Count - 1)
vArray(i) = New VideoTape(data, i)
Next
Return vArray
End Function
Public Function GetAllVideoTapesInCategory(ByVal catID As Decimal) As VideoTape()
Dim data As New VideoTapeData()
loadCat.SelectCommand.Parameters("@CategoryID").Va lue = catID
loadCat.Fill(data)
If (data.Rows.Count < 1) Then
Return Nothing
End If
Return GetVideoTapeArrayFromData(data)
End Function
Public Function SetVideoTape(byRef vTape As VideoTape)
adapter.Update(vTape.MyData)
End Function
Public Function RemoveVideoTape(ByRef vTape As VideoTape)
adapter.DeleteCommand.Parameters("@VideoTapeID").V alue = vTape.VideoTapeID
adapter.DeleteCommand.Connection.Open()
adapter.DeleteCommand.ExecuteNonQuery()
adapter.DeleteCommand.Connection.Close()
End Function
End Class
Public Class VideoCategoryIndexer
Private vids As ArrayList
Private cnt As Integer
Public Sub New(ByVal catID as Integer, ByRef conn as SqlConnection)
vids = New ArrayList()
Dim sda As New SqlDataAdapter()
sda.SelectCommand = New SqlCommand("VideoTapeLoadByCategoryIncrID", conn)
sda.SelectCommand.CommandType = CommandType.StoredProcedure
sda.SelectCommand.Parameters.Add("@CategoryID", SqlDbType.Int, 0, "CategoryID")
sda.SelectCommand.Parameters.Add("@TotalRows", SqlDbType.Int)
sda.SelectCommand.Parameters("@TotalRows").Directi on = ParameterDirection.Output
sda.SelectCommand.Parameters("CategoryID").Value = catID
Dim data As New VideoTapeData()
sda.Fill(data)
cnt = Convert.ToInt32(sda.SelectCommand.Parameters("@Tot alRows").Value)
vids.AddRange(VideoTapeDataAccess.GetVideoTapeArra yFromData(data))
End Sub
Public Function Count() As Integer
Return cnt
End Function
Public Property Videos(ByVal index As Integer) As VideoTape
Get
If (index > vids.Count-1) Then
FullLoad()
End If
Return CType(vids(index), VideoTape)
End Get
Set(ByVal Value As VideoTape)
vids(index) = Value
End Set
End Property
Public ReadOnly Property LoadedCount() As Integer
Get
Return vids.Count
End Get
End Property
Private Function FullLoad()
Dim dac As New VideoTapeDataAccess()
vids.Clear()
vids.AddRange(dac.GetAllVideoTapes())
End Function
End Class
End Namespace