I've been working on this for a few days now I every time I come back to this problem I just don't see why it's not working correctly.
I'm trying to bind a DataTable to a GridView control that I create Dynamically. I create the GridView control, add it to a table, and then assign the DataSource property to my DataTable.
This is the code:
Table tbl = new Table();
DataTable dattbl = Core.Transreports(Request.QueryString["itemaddress"], Request.QueryString["docnum"], Request.QueryString["docid"]);
GridView dg = new GridView() { ID = "dg", AllowPaging = true, PageSize = 10 };
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Controls.Add(dg);
tc.ColumnSpan = 10;
tr.Cells.Add(tc);
tbl.Rows.Add(tr);
if (dattbl.Rows.Count > 0)
{
dg.DataSource = dattbl;
dg.DataBind();
}
So when I get the the last line, where I execute the DataBind (dg.DataBind()) method is where I'm getting the null reference exception.
I'm not really sure why I'm running into this error, and have not yet found a solution. I've checked to make sure there are no null values in the DataTable as well, and there are none. So I'm at a loss.
Help me stack overflow, you're my only hope.