C#.NET错误:无法强制转换类型为';的对象;System.Data.DataTable';键入';
本文关键字:对象 System 键入 DataTable Data 类型 错误 NET 转换 | 更新日期: 2023-09-27 18:26:41
我有一个可编辑的Gridview,它的数据存储在一个XML文件中,当我试图在Gridview中插入/更新/删除数据时,我会收到以下错误:
无法将"System.Data.DataTable"类型的对象强制转换为"System.Data_DataSet "类型
Gridview成功地显示了存储在xml文件中的数据,但当我尝试插入/更新/删除行时出错。
我能知道我在这里遗漏了什么吗?
XML文件:
<root>
<pos>
<partNumbers>
<partid>0</partid>
<partnumber>796542</partnumber>
</partNumbers>
<partNumbers>
<partid>1</partid>
<partnumber>225614</partnumber>
</partNumbers>
<partNumbers>
<partid>2</partid>
<partnumber>123457</partnumber>
</partNumbers>
</pos>
</root>
我的C#代码:
//Bind Data
protected void BindGridView()
{
DataSet dsgvPartNumber = new DataSet();
dsgvPartNumber.ReadXml(Server.MapPath("~/xml/storeUserInfo.xml"));
gvPartNumber.DataSource = dsgvPartNumber.Tables["partNumbers"];
gvPartNumber.DataBind();
gvPartNumber.ShowFooter = true;
}
// Delete Row
protected void gvPartNumber_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
BindGridView();
DataSet dsgvPartNumberDelete = (DataSet)gvPartNumber.DataSource;
dsgvPartNumberDelete.Tables[0].Rows[gvPartNumber.Rows[e.RowIndex].DataItemIndex].Delete();
dsgvPartNumberDelete.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
// EditGridView
protected void gvPartNumber_RowEditing(object sender, GridViewEditEventArgs e)
{
gvPartNumber.ShowFooter = false;
gvPartNumber.EditIndex = e.NewEditIndex;
BindGridView();
}
// Update GridView
protected void gvPartNumber_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = gvPartNumber.Rows[e.RowIndex].DataItemIndex;
string partId = ((TextBox)gvPartNumber.Rows[e.RowIndex].FindControl("txtPartID")).Text;
string partNumber = ((TextBox)gvPartNumber.Rows[e.RowIndex].FindControl("txtPartNumber")).Text;
gvPartNumber.EditIndex = -1;
BindGridView();
DataSet dsUpdateXMLFile = (DataSet)gvPartNumber.DataSource;
dsUpdateXMLFile.Tables[0].Rows[index]["partid"] = partId;
dsUpdateXMLFile.Tables[0].Rows[index]["partnumber"] = partNumber;
dsUpdateXMLFile.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
// Insert New Row
protected void gvPartNumber_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "insertXMLData")
{
string partid = ((TextBox)gvPartNumber.FooterRow.FindControl("txtPartIDInsert")).Text;
string partnumber = ((TextBox)gvPartNumber.FooterRow.FindControl("txtPartNumberInsert")).Text;
BindGridView();
DataSet dsXMLInsert = (DataSet)gvPartNumber.DataSource;
DataRow drInsert = dsXMLInsert.Tables[0].NewRow();
drInsert["partid"] = partid;
drInsert["partnumber"] = partnumber;
dsXMLInsert.Tables[0].Rows.Add(drInsert);
dsXMLInsert.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
}
我能得到一些关于我在代码中缺少什么的指示/解释吗?
提前谢谢。
这是因为您将DataSource
设置为DataTable
:
gvPartNumber.DataSource = dsgvPartNumber.Tables["partNumbers"];
而不是CCD_ 3。因此,取而代之的是:
DataSet dsgvPartNumberDelete = (DataSet)gvPartNumber.DataSource;
这样做:
DataTable dsgvPartNumberDelete = (DataTable)gvPartNumber.DataSource;
在接下来的几行中,您将不需要像.Tables[0]
这样的访问器。它将已经是DataTable
,因此无论如何都更直接地访问。
当然,这种修改必须复制到您尝试将DataSource
强制转换为DataSet
的所有方法中。
您的数据源是DataTable,而不是DataSet。不能将DataTable强制转换为DataSet。
DataTable dtUpdateXMLFile = (DataTable)gvPartNumber.DataSource;
dtUpdateXMLFile.Rows[index]["partid"] = partId;
dtUpdateXMLFile.Rows[index]["partnumber"] = partNumber;
dtUpdateXMLFile.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));