删除gridview中的一行c#

时间:2013-05-01 17:42:29

标签: c# asp.net

我在按钮中使用以下代码从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)

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

此方法也在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:服务器无法执行   处理请求。 ---&GT; System.InvalidOperationException:动态SQL   不支持针对DeleteCommand的生成   SelectCommand不返回任何键列信息。在   System.Data.Common.DbDataAdapter.UpdatingRowStatusErrors(RowUpdatingEventArgs   rowUpdatedEvent,DataRow dataRow)at   System.Data.Common.DbDataAdapter.Update(DataRow [] dataRows,   DataTableMapping tableMapping)at   System.Data.Common.DbDataAdapter.UpdateFromDataTable(数据表   dataTable,DataTableMapping tableMapping)at   System.Data.Common.DbDataAdapter.Update(DataSet dataSet,String   srcTable)在Service.ModifyDatabase(DataSet myDataset)中   e:\ web \ App_Code \ Service.cs:第183行---内部异常结束   堆栈跟踪---

3 个答案:

答案 0 :(得分:0)

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

int i = GridView1.SelectedIndex;
tshirtSet.Tables["tshirts"].Rows[i].Delete();
tshirtSet.AcceptChanges();

答案 1 :(得分: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();
        }
    }

答案 2 :(得分:0)

错误告诉您它无法从select语句中找出数据表的主键,因此当您调用{{1}时,dataadapter无法确定要传递给它创建的delete语句的内容}}

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