有人知道这个错误是什么吗?
我收到此错误:对象引用未设置到实例。
运行此代码后,
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>");
dtTemp.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);
}
gridview 'gvRapporten' 的数据源在 page_load 事件中设置,因此它不能为 NULL
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>");
dtTemp.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回发时,
DataSource
将为空。您必须重新运行查询才能获取原始数据。On a Post back the
DataSource
will be empty. You'll have to re-run the query to get the original data.如果此代码不是“page_load”的一部分,那么我假设这是其他控制事件的一部分,这意味着 gvRapporten.DataSource 的值将在回发时被清除。由于网络是“无状态”的,它不会保留数据表值,因此您应该从数据库中再次获取该值。
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.
如果您确定 gvRapporten,则循环中访问的行之一可能包含 NULL 引用,当您调用 ToString() 函数时会出现错误
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