I'm getting this error: object reference not set to an instance.
after running this code
DataTable dtOriginal = new DataTable();
dtOriginal = (DataTable)gvRapporten.DataSource; //Return Table consisting data
//Create Tempory Table
DataTable dtTemp = new DataTable();
//Creating Header Row
dtTemp.Columns.Add("<b>Melder</b>");
dtTemp.Columns.Add("<b>Onderwerp</b>");
dtTemp.Columns.Add("<b>Oplosser</b>");
dtTe开发者_如何学运维mp.Columns.Add("<b>Niveau 2</b>");
DataRow drAddItem;
for (int i = 0; i < dtOriginal.Rows.Count; i++)
{
drAddItem = dtTemp.NewRow();
drAddItem[0] = dtOriginal.Rows[i][0].ToString();//Melder
drAddItem[1] = dtOriginal.Rows[i][1].ToString();//Onderwerp
drAddItem[2] = dtOriginal.Rows[i][2].ToString();//Oplosser
drAddItem[3] = dtOriginal.Rows[i][3].ToString();//Niveau 2
dtTemp.Rows.Add(drAddItem);
}
The datasource of the gridview 'gvRapporten' is set at the page_load event, so it can't be NULL
On a Post back the DataSource
will be empty. You'll have to re-run the query to get the original data.
If this code is not part of the 'page_load' then I assume that this is on the other control events which means the value of the gvRapporten.DataSource will be cleared on postback. Since web is 'State-less' it will not retain the datatable value, so you should get the value again from your database.
If you are sure about gvRapporten, it is possible that one of the Rows accessed in the loop contains a NULL reference giving error when you call the ToString() function
精彩评论