locked
error in deleting a gridview RRS feed

  • Question

  • User1253203540 posted

    hello, i have a problem when i try ot delete a row in a gridview, everythiing is display ok except when the delete function is run. There are three columns in my Access, ID = Auto First & Last Name = Text

    the error says data type mismatch in criteria expression -> cmd.ExceuteNonQuery();

    This is the code for delete:

    protected void ResultGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

    string Connection = "Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=D:/Customers.mdb";

    OleDbConnection con = new OleDbConnection(Connection);

    OleDbCommand cmd = new OleDbCommand();

    cmd.Connection = con;

    cmd.CommandText ="delete from tblCustomers where ID='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";

    con.Open();

    cmd.ExecuteNonQuery();

    con.Close();

    FillVendorGrid();

    }

    Tuesday, September 11, 2012 2:09 AM

Answers

  • User-1716253493 posted

    try this (remove ' around idvalue)

    cmd.CommandText =
    "delete from tblCustomers where ID=" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 11, 2012 3:00 AM

All replies

  • User-1835425756 posted

    the datatype of "ID" is integer and u passed the string. that is the problem.

    do this

    Convert.ToInt32(ResultGridView.DataKeys[e.RowIndex].Values[0].ToString());

    hope this will help.

    Tuesday, September 11, 2012 2:10 AM
  • User-1716253493 posted

    try this (remove ' around idvalue)

    cmd.CommandText =
    "delete from tblCustomers where ID=" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 11, 2012 3:00 AM
  • User3866881 posted

    ResultGridView.DataKeys[e.RowIndex].Values[0].ToString()

    Hi,

    If your ID is type of int,you cannot add a single quote.

    Wednesday, September 12, 2012 8:40 PM