This actually bugged me for sometime! Look at this code:
SPListItemCollection results = list.GetItems (myQuery);
DataTable dtResults = results.GetDataTable();
Here, I'll execute a CAML (SPQuery) query against a list. From the SPListItemCollection, I'll try to get a DataTable.
If the SPListItemCollection is empty (i.e., results.Count = 0), the second line assigns null to dtResults. So, if you are referring to dtResults in the following lines, application throws NullReferenceException ("Object reference not set to an instance of an object"). I was expecting that, if there is no list item, the data table should be kind of empty table (no row but it should have schema).
So the summary is, whenever you are using GetDataTable(), make sure that you handle the null appropriately.
SPListItemCollection results = list.GetItems (myQuery);
DataTable dtResults = results.GetDataTable();
Here, I'll execute a CAML (SPQuery) query against a list. From the SPListItemCollection, I'll try to get a DataTable.
If the SPListItemCollection is empty (i.e., results.Count = 0), the second line assigns null to dtResults. So, if you are referring to dtResults in the following lines, application throws NullReferenceException ("Object reference not set to an instance of an object"). I was expecting that, if there is no list item, the data table should be kind of empty table (no row but it should have schema).
So the summary is, whenever you are using GetDataTable(), make sure that you handle the null appropriately.
Comments
Post a Comment