views:

44

answers:

3

Lets start by saying that I am debugging someone else's code :-) The error occurs at the code routine that is attempting to export data from a gridview to an excel file.

GridView gv = new GridView();
Table table = new Table();
int maxRow = 60000;
int gvrow = Data().Tables[0].Rows.Count;  

The error is occuring at Data().Tables[0].Rows.Count

The error in the immediate window is:
? Data().Tables[0].Rows.Count
A first chance exception of type 'System.FormatException' occurred in System.Data.dll 'Data()' is null

The error message is:
System.NullReferenceException was unhandled by user code Message="Object reference not set to an instance of an object."

Any ideas what is wrong here?

+2  A: 

Data() returns null. Fix that.

thelost
The thing is that it doesn't return null. There are records that are always returned.
user279521
@user279521: Please verify that, i.e., do something like `var myData = Data(); if (myData == null) throw new Exception("Data() has unexpectedly returned null");`. At the moment, it *really* looks like `Data()` returns null.
Heinzi
A: 

I can't tell based just on this code (What is Data() supposed to do?), but my guess is that the call to Data() is returning null for some reason, or, at least, is returning some type of Data Set with no actual tables in it.

AllenG
A: 

Sounds like you just need to check that Data() is not null before attempting to access properties.

Gary L Cox Jr