0

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();
}
jotaen
  • 6,739
  • 1
  • 11
  • 24
  • I am getting error here cmd.ExecuteNonQuery(); An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code – Gopi Naidu Annem May 27 '18 at 07:48

2 Answers2

0

As far as I know about SQL you need to pass the value within a ' ' So yours SQL command should looks like

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

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.

Kjell Derous
  • 441
  • 1
  • 5
  • 11