Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NullReferenceException inside DataTable

Status
Not open for further replies.

icrf

Programmer
Dec 4, 2001
1,300
US
This is the second time this error has come up within the last week and I can't see any cause for it. The null reference is deeper inside the DataTable than I'm touching, so I don't know what goes wrong. First a little information:
Code:
* System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.Index.InitRecords()
   at System.Data.Index..ctor(DataTable table, Int32[] indexDesc, DataViewRowState recordStates, IFilter rowFilter)
   at System.Data.Select.CreateIndex()
   at System.Data.Select.SelectRows()
   at System.Data.DataTable.Select(String filterExpression, String sort)
   at ProductList.Page_Load(Object sender, EventArgs e) in C:\VSASP\vipweb\Products\ProductList.ascx.vb:line 63
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain()
being called from
Code:
...
Dim dt As DataTable = GetCategories
Dim dr As DataRow = dt.NewRow
dr("CategoryID") = 0
dr("Description") = "All"
dr("Parent") = iCat
dt.Rows.InsertAt(dr, 0)

'next line is 63
Filter.DataSource = dt.Select("[Parent] = " & iCat, "Description")
Filter.DataBind()
...

Public Shared Function GetCategories() As DataTable
    Dim key As String = "Cats" & Utils.clean(HttpContext.Current.Session("portal"))
    If HttpContext.Current.Cache(key) Is Nothing Then
        dim dt as DataTable
        ''' fill dt from database '''

        HttpContext.Current.Cache.Insert(key, dt, Nothing, Now.AddHours(6), TimeSpan.Zero)
    End If
    Return CType(HttpContext.Current.Cache(key), DataTable)
End Function
The most I can get down to is that it's something that screws up in the datatable object itself. When the error happens, it gets cached and comes up for everyone with the same Session("portal") value. The query is always the same for a given "portal" and it works the vast majority of the time. The first time the error happened, I was in the office and cleared the cache by touching web.config and the error went away.

This second time it happened over the weekend and it looks like it only affected two people for two minutes (the problem was either fixed or the two users gave up). The first error this weekend happened to both users during the same second.

The DataTable is retrieved from the database using a custom class that's been in production for over a year, so I really doubt it's at fault. GetCategories is a shared method of a class that is called from a variety of places, and a similar error comes up when those pages are hit.

Googling for different methods/objects in the call stack didn't come up with anything related. Can someone offer a pointer of where else I should look? Is there any more information I can offer that would help?

.NET Framework 1.1 SP1 + hotfix

Thanks for your time.

________________________________________
Andrew
 
Strangly enough i think

Public Shared Function GetCategories() As DataTable
Dim key As String = "Cats" & Utils.clean(HttpContext.Current.Session("portal"))
If HttpContext.Current.Cache(key) Is Nothing Then
dim dt as DataTable


is your problem

You still haven't built a datatable..

Also are you sure that you have a valid session variable called protal

Normally if all else looks good to me I will run a trace on the db server (SQLTrace for sqlserver) and it tells all, but I wuould suspect the fucntion you are calling.


Rob
 
Sorry to be specific
change

If HttpContext.Current.Cache(key) Is Nothing Then
dim dt as DataTable

to

If HttpContext.Current.Cache(key) Is Nothing Then
dim dt as [red]new[/red] DataTable
 
Sorry, my little comment

''' fill dt from database '''

means that I really do create and fill a datatable there (maybe should add <snip> to clarify), but the query and code to do it is not relevant to this problem. As I mentioned, I have a custom database class that is returning the data, and it's worked reliably for over a year.

Utils.clean() will return an empty string if the session value is not set, then the key is a simple "Cats". In any case, GetCategories is running fine, no errors are being thrown from inside there. It is creating and returning a valid DataTable, as I'm successfully adding another record to the table. The exception is only thrown when I try a .Select() on the DT. The same happens when I create a DataView with a RowFilter from the DT. This works consistently most of the time, but twice now it hasn't. I can't reproduce the problem by trying.

The DataTable is not null, something deep inside it is. I'm guessing the data is returned fine from the database, as no errors are being thrown from there. The proper data columns are being created in the datatable, as I can create a new row and add it to the DT after it's created. Tracing the database end likely won't turn up anything.

My guess is that it's either a bug in .NET (not overly likely) or some issue with cacheing or filtering a large DataTable. Large is a bit of an overstatement, as it's about 200 rows with 8 columns. I think the application cache is just storing the object in memory, so serialization of something like Session doesn't apply. If it expires, the whole thing goes or the whole things stays, I'd hope parts of the references heirarchy don't disappear. Filtering a couple hundred records should be a simple matter for .NET.

Without being able to reproduce the problem, I'm having a hard time tracking down a cause, much less a solution.

Thanks for your suggestion.

________________________________________
Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top