DataTable.Merge 抛出错误:未将对象引用设置到实例
我想合并 DropDownList selectedindex 更改事件中的数据表。所以我在 DropDownList_SelectedIndexChanged 事件中编写了以下代码。
DataTable dt = (DataTable)POBLL.GetPOAdhocDetail();
DataView dv = new DataView(dt);
dv.RowFilter = "IndentTypeID = " + ddlIndentType.SelectedValue + "AND IndentID = " + ddlIndentNumber.SelectedValue;
DataTable dtView = dv.ToTable();
gvPODetails.DataSource = dtView;
gvPODetails.DataBind();
DTMerge.Merge(dtView);
其中 DTMerge 是 DataTable,我将其声明如下:
public DataTable DTMerge
{
get
{
if (ViewState["Merge"] != null)
{
return (DataTable)ViewState["Merge"];
}
else
return null;
}
set
{
ViewState["Merge"] = value;
}
}
但它会抛出错误“对象引用未设置到实例”。怎么解决这个问题呢?
I want to merge datatables in the DropDownList selectedindex changed event. So I have written the below code in the DropDownList_SelectedIndexChanged Event.
DataTable dt = (DataTable)POBLL.GetPOAdhocDetail();
DataView dv = new DataView(dt);
dv.RowFilter = "IndentTypeID = " + ddlIndentType.SelectedValue + "AND IndentID = " + ddlIndentNumber.SelectedValue;
DataTable dtView = dv.ToTable();
gvPODetails.DataSource = dtView;
gvPODetails.DataBind();
DTMerge.Merge(dtView);
where DTMerge is the DataTable and I declared it like below:
public DataTable DTMerge
{
get
{
if (ViewState["Merge"] != null)
{
return (DataTable)ViewState["Merge"];
}
else
return null;
}
set
{
ViewState["Merge"] = value;
}
}
But it throws the Error "Object reference not set to an instance". How to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您请求了 DTMerge 属性,该属性需要存在相应的 ViewState 才能有效。在访问 DTMerge 属性时,您还没有 ViewState。
这很可能就是问题所在。
You requested DTMerge property, which for being valid need of presence of corresponding ViewState. You don't have a ViewState yet, at the moment you access DTMerge property.
This is most likely the problem.