-1

I am using the following code in a button to delete a row from my 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)

The gridview is being used by a web service which is linked to the database, this method is used to retrieve data from my database

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

This method is also in the webservice, this basically updates the database:

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

These are the methods, I cant seem to delete a row, any suggestions?

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information. at 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 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 --- End of inner exception stack trace ---

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
budosvic
  • 1
  • 2
  • I believe this is what you are looking for: http://stackoverflow.com/questions/7226548/how-to-delete-row-in-gridview-using-rowdeleting-event – RBZ May 01 '13 at 17:44
  • 1
    Nothing in that code deletes anything from your GridView. What does `I cant seem to delete a row` mean? You get an error, nothing happens? **Be more specific** – tnw May 01 '13 at 17:46
  • I get an error, it says deletecommand is not supported against a select command that does not return any key column information – budosvic May 01 '13 at 17:56
  • Edit your post, show me WHERE in the code the exception is happening, and include the FULL exception – tnw May 01 '13 at 17:59

3 Answers3

0

Call DataSet.AcceptChanges() method to commit your changes.

int i = GridView1.SelectedIndex;
tshirtSet.Tables["tshirts"].Rows[i].Delete();
tshirtSet.AcceptChanges();
Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
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();
        }
    }
Pavel Pája Halbich
  • 1,529
  • 2
  • 17
  • 22
0

The error is telling you that it can't figure out the primary key for your datatable from the select statement, therefore the dataadapter can't figure out what to pass in to the delete statement it's creating when you call Update().

Does your database table have a primary key defined?

Spivonious
  • 718
  • 7
  • 20