简体   繁体   中英

Error during delete operation in GridView

This is the code of my grid view delete operation using storedprocedure . I get this error: {"No mapping exists from object type System.Web.UI.WebControls.Label to a known managed provider native type."}

protected void GVEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    GridViewRow row = GVEmployee.Rows[e.RowIndex];
    Control c1 = row.FindControl("Label1");
    Label l1 = (Label)c1;
    SqlConnection con = new SqlConnection();
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBCM"].ConnectionString;
    SqlCommand cmd = new SqlCommand("delete from Spempdet where Id= ' " + ID , con);
    con.Open();
    cmd.Parameters.AddWithValue("@Id", l1);
    cmd.ExecuteNonQuery();
    con.Close();
    Bind();
}

据我对SQL的了解,您需要在''中传递值,因此您的SQL命令应类似于

SqlCommand cmd = new SqlCommand("delete from Spempdet where Id= ' " + ID+" ' " , con);

First, you are passing the label as a parameter. I assume the label text contains the ID you want to use in your delete statement so you should change the code line:

cmd.Parameters.AddWithValue("@Id", l1);

to

cmd.Parameters.AddWithValue("@Id", l1.Text);

Second, I assume you also have the wrong delete query. You add the @Id parameter but you don't use it in your query.

SqlCommand cmd = new SqlCommand("delete from Spempdet where Id = @id", connection)

Thirth, don't forget your using statements. Both SqlConnection and SqlCommand are disposable. Please check When should I use "using" blocks in C#? for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM