tags:

views:

161

answers:

3

I have a page with a button that when I click, it retrieves data from a database and stores it into a datatable and binds it to a GridView. This datatable is stored in a Session variable. I also have a button that exports specified columns from the datatable to an excel file, but when I click the export button a second time, I get the following error:

Object reference not set to an instance of an object.

It seems to happen at this line:

dtExport.Columns["Business"].ColumnName = "Licensee";

I think I know why, the original column is called "Business", but when I export, I want to export the column header as Licensee, so I change the ColumnName to "Licensee"; however when I call Export a second time, it is looking for dtExport.Columns["Business"] again which it does not find, so it throws an error. Do I just need to check if the column as already been renamed to resolve this or is there another way?

Here is the code that does the Export:

private void ExportExcel()
{
    DataTable dtExport = Session["dtSearchResults"] as DataTable;

    dtExport.Columns["Business"].ColumnName = "Licensee";

    List<int> columnSelect = new List<int>();

    columnSelect.Add(dtExport.Columns["Licensee"].Ordinal);
    columnSelect.Add(dtExport.Columns["Name"].Ordinal);
    columnSelect.Add(dtExport.Columns["Address"].Ordinal);
    columnSelect.Add(dtExport.Columns["City"].Ordinal);
    columnSelect.Add(dtExport.Columns["State"].Ordinal);
    columnSelect.Add(dtExport.Columns["Zip"].Ordinal);

    int[] ColList = columnSelect.ToArray();

    GridViewExportUtil.ExportDetails(dtExport, 
                                     ColList,
                                     GridViewExportUtil.ExportFormat.Excel,
                                     string.Format("{0}_{1}-{2}-{3}{4}",
                                     "SearchResults",
                                     DateTime.Now.Month, 
                                     DateTime.Now.Day,
                                     DateTime.Now.Year, 
                                     ".xls"));
    }

When you change a ColumnName, does it persit, even if you get a new DataTable from Session again?

+2  A: 

I am guessing the value is by reference, and when you change the Business column name it is changed in Session as well. I would try this:

DataTable dtExport = (Session["dtSearchResults"] as DataTable).Copy();
Shawn Simon
Does this have any performance or overhead hit?
Xaisoft
Sure, but only on the button click of downloading to excel, which is relatively infrequent. Depends on the size of the datatable.
Shawn Simon
This was the best solution. I guess because I am copying, I am keeping the original columnames intact, but the other way, I was actually changing them in memory, is that a good way to think about it?
Xaisoft
+1  A: 

You're not getting a new DataTable you are getting the same one that you originally generated. Any changes you make to it will remain to be seen by subsequent uses.

In this case I would rename the column back to its original name after the export.

AnthonyWJones
Isn't it easier just to check if the column exists?
Xaisoft
Its easier but what if you have other uses for this object in its original form. It seems likely the rename is happening purely to suit the needs of the excel export. If the change should be permanent then it ought to happen at source.
AnthonyWJones
+1  A: 

It's an awful solution, but... add

dtExport.Columns["Licensee"].ColumnName = "Business";

at the end of your method to restore the initial situation. And add some error/existence checking.

Fabrizio C.