This is a migrated thread and some comments may be shown as answers.

Excel Export NullReferenceException

1 Answer 46 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 14 May 2012, 06:32 PM
I am getting a NullReferenceException when exporting my grid to Excel.  It happens when I am referencing a button that exists in a GridTemplateColumn.  I am setting the BackColor of the GridTableCell the button exists in and the CommandArgument of the button in the ItemDataBound event of the grid.

These are the lines I get the exception on.
((GridTableCell)btn.Parent).BackColor = System.Drawing.Color.Red;
btn.CommandArgument = "etid=" + drv["JOC_Event_Ticket_ID"] + "&ttype=" + drv["Ticket_Type_ID"];

Is there a way to get around this when exporting to Excel?
int ticket_number = int.Parse(DataBinder.Eval(e.Item.DataItem, "Ticket").ToString());
                if (Ticket_number > 0)
                {
                    if (ticket_number > 0)
                    {
                        e.Item.Cells[13].BackColor = System.Drawing.Color.Red;
                        ((GridTableCell)btn.Parent).BackColor = System.Drawing.Color.Red;
                    }
                    if (drv["JOC_Event_Ticket_ID"].ToString().Length > 0)
                    {
                        btn.CommandArgument = "etid=" + drv["JOC_Event_Ticket_ID"] + "&ttype=" + drv["Ticket_Type_ID"];
                    }
                    else
                    {
                        btn.CommandArgument = "problem_no=" + drv["Ticket"] + "&ttype=1"; // +drv["Ticket_Type_ID"];
                    }
                }


Thanks,
Steven

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 17 May 2012, 12:06 PM
Hi Steven,

I'd recommend that you check whether the btn variable is null in this case.

Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Steven
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or