正在删除网格视图c#中的一行

本文关键字:一行 删除 网格 视图 | 更新日期: 2023-09-27 18:11:46

我在按钮中使用以下代码从网格视图中删除一行:

int i = GridView1.SelectedIndex;
tshirtSet.Tables["tshirts"].Rows[i].Delete();
GridView1.DataSource = tshirtset;
GridView1.DataBind();
localhost.Service mc = new localhost.Service();
mc.GetTshirtSet();
mc.ModifyDatabase(tshirtSet); <-------(Error on this line)

gridview正由链接到数据库的web服务使用,该方法用于从我的数据库中检索数据

[WebMethod]
public DataSet GetTshirtSet()
{
    DataSet tshirtSet = new DataSet();
    string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/shop.accdb;Persist Security Info=True";
    OleDbConnection myConn = new OleDbConnection(database);
    string queryStr = "SELECT * FROM tshirt";
    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(queryStr, myConn);
    myConn.Open();
    myDataAdapter.Fill(tshirtSet, "Tshirt");
    myConn.Close();
    return tshirtSet;
}

这种方法也在Web服务中,这基本上更新了数据库:

[WebMethod]
public string ModifyDatabase(DataSet myDataset)
{
    string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/shop.accdb;Persist Security Info=True";
    OleDbConnection myConn = new OleDbConnection(database);
    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from tshirt", myConn);
    OleDbCommandBuilder builder = new OleDbCommandBuilder(myDataAdapter);
    builder.QuotePrefix = "[";
    builder.QuoteSuffix = "]";
    myConn.Open();
    myDataAdapter.Update(myDataset, "tshirt");
    myConn.Close();
    return "done";
}

这些都是方法,我似乎无法删除一行,有什么建议吗?

System.Web.Services.Protocols.SoapException:服务器无法处理请求。--->System.InvalidOperationException:动态SQL不支持对生成DeleteCommand选择不返回任何键列信息的命令。在System.Data.Common.DbDataAdapter.UpdatingRowStatusErrors(RowUpdatingEventArgsrowUpdatedEvent,DataRow DataRow(System.Data.Common.DbDataAdapter.Update(DataRow[]dataRows,DataTableMapping tableMapping(System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTabledataTable、DataTableMapping tableMapping(System.Data.Common.DbDataAdapter.Update(数据集数据集,字符串srcTable(,位于中的Service.ModifyDatabase(DataSet myDataset(e: ''web''App_Code''Service.cs:line 183--内部异常结束堆栈跟踪---

正在删除网格视图c#中的一行

调用DataSet.AcceptChanges((方法来提交更改。

int i = GridView1.SelectedIndex;
tshirtSet.Tables["tshirts"].Rows[i].Delete();
tshirtSet.AcceptChanges();
protected void lnkDeleteRisk_onclick(object sender, EventArgs e)
{
        LinkButton obj_lnkbidtask = (LinkButton)sender;
        GridViewRow gvTask = (GridViewRow)obj_lnkbidtask.NamingContainer;
        HiddenField var_txtId (HiddenField)grdBidRisk.Rows[gvTask.RowIndex].Cells[0].FindControl("txtId");
        int int_ReturnVal = obj_WebService.DeleteRisk(var_txtId.Value);
        if (int_ReturnVal == -1)
        {
            lblErrMsg.Visible = true;
            lblErrMsg.Text = "Error occured during this operation, please contact administrator.";// "Authentication failed. Please try later.";
        }
        else
        {
            lblErrMsg.Visible = true;
            lblErrMsg.Text = "Selected risk deleted successfully.";
            SetInitialRow();
        }
    }

错误告诉您,它无法从select语句中找出数据表的主键,因此,当您调用Update()时,数据适配器无法找出要传递给它正在创建的delete语句的内容。

您的数据库表是否定义了主键?