-1

我在按钮中使用以下代码从我的 gridview 中删除一行:

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)

链接到数据库的 Web 服务正在使用 gridview,此方法用于从我的数据库中检索数据

[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;
}

这个方法也在webservice中,这基本更新了数据库:

[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:对于不返回任何键列信息的 SelectCommand,不支持为 DeleteCommand 生成动态 SQL。在 System.Data.Common.DbDataAdapter.UpdatingRowStatusErrors(RowUpdatingEventArgs rowUpdatedEvent, DataRow dataRow) 在 System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) 在 System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) at Service.ModifyDatabase(DataSet myDataset) in e:\web\App_Code\Service.cs:line 183 --- 内部异常堆栈结束痕迹 - -

4

3 回答 3

0

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

int i = GridView1.SelectedIndex;
tshirtSet.Tables["tshirts"].Rows[i].Delete();
tshirtSet.AcceptChanges();
于 2013-05-01T17:59:43.960 回答
0

该错误告诉您它无法从 select 语句中找出数据表的主键,因此 dataadapter 无法确定在您调用时将什么传递给它正在创建的 delete 语句Update()

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

于 2017-04-20T14:04:36.157 回答
0
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();
        }
    }
于 2017-04-20T12:46:13.183 回答