Datatable: works in windowsApp, not in ASP.NET

Hi all,

I've got a very weird problem. I'm trying to retrieve a datatable and in my view this works. Here's my (test) code in ans ASP.NET code page.

1Public Sub New()

2 cls_datamanager =New DataManager()

3 Get_errors()

4End Sub

5

6 Public Sub Get_errors()

7Dim cls_ErrorAs New Error_Logger(cls_datamanager)

8Dim cls_datatableAs New DataTable

9 cls_datatable = cls_Error.List_errors

10

11'grd_errors.DataSource

12 lbl_test.Text = cls_datatable.Rows(0).Item(0)

13

14End Sub

cls_error and cls_datamanager are some classes for DAL and three tier development I use and they work.

I get a NullReference Exception when I get to the last line of code. The strange thing is when I do exactly the same in a windowsapplication (see below) this gives me no problem at all. And it uses the same assmeblies!

When debugging I also see this datatable DOES have 2 rows and 5 columns, as is expected. The Stored Procedure that is behind works fine and does not need any paramaters, it basically does a select *.

I have no idea why I get a this error. In my SQL Profiler I see the correct SQL Stored proc passing by so it retrieves the data correctly

This is a small windowsapplication I used to test my datamanager and error_logger classes and as mentioned above, this works without error.

1Private Sub Form1_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles MyBase.Load

2

3Try

4 Dim cls_datamanagerAs DataManager

5Dim keyAs Integer

6

7

8

9

10

11 cls_datamanager =New DataManager

12

18

19Dim cls_errorAs New Error_Logger(cls_datamanager)

20Dim cls_datatableAs DataTable

21 cls_datatable = cls_error.List_errors

22 MessageBox.Show(cls_datatable.Rows(1).Item(1))

23

24

25Catch exAs Exception

26 MessageBox.Show(ex.Message)

27End Try

-->c This does return the data in column 1 of row 1

Any help would be appreciated

[3632 byte] By [StijnV] at [2007-12-24]
# 1

I'm answering based on you assumption that the DAL is working fine and getting data from database (scenario of win apps)

The problem could with the following line of code

Dim cls_Error As New Error_Logger(cls_datamanager)

I think cls_datamanager is having null value. That's why you are getting this kind of exceptions. Make sure that object cls_datamanager is public through out the code-behind of ASP.NET page and also make sure that cls_datamanager is not null before calling the Error_Logger component. This is the problem with scope and instantiation of cls_datamanager. Please try along those lines

If you look at the code of Windows App the decleartion and instantion of cls_datamanager is done within the form_load. It is alive within the form_load method. That's why you are not getting the problem there.

Pls let me know whether this is useful and whether you need more information

Thanks & Regards,

Sundar Narasimman

SundarNarasimman at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2

Thanks for the reply Sundar.

Since I see the correct Stored procedure passing by in the SQL profiler, and this is done within the datamanager class it's impossible that this is the cause of the nullreference exception.

I also initialize this class prior to calling any procedure , property of function from it and the error only occurs when I try do to anything with cls_datatable. I'm really puzzled, must be something stupid I'm overlooking here...

StijnV at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3

Could it be that the value in row 1, column 1 is null? Notice that in the win app, you are looking at a different record, different column.

Try: lbl_test.Text = cls_datatable.Rows(0).Item(0).ToString()

In the end, something is obviously null so you've got to find where it is. It may not even be the DataTable. If you see records in the DataTable, can you place some other text in lbl_test programmatically?

Oftentimes is the head-smacker that catches you the worst.

axshon at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 4

Since there are two rows that index 1 for rows should return something as well. This is actually an example, cause I've got the problem with everything I try with that cls_datatable (I stumbled upon it when binding to a datagrid and this obviously didn't work as well).

I would have gotten an index out of range exception if there was no row(1) I think...

Nothing wrong with lbl_test.... I can display anything I want in the text property. It's driving me up the walls

StijnV at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 5
Ok, next step: Save the DataTable as XML with schema and post the XML here so we can look at it.
axshon at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 6
ok, i'll reply with the xml schema tonight (@ work now)
StijnV at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified