GridView中删除操作期间出错

时间:2018-05-27 07:38:43

标签: c# asp.net

这是使用storedprocedure的网格视图删除操作的代码。我收到此错误:{"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();
}

2 个答案:

答案 0 :(得分:0)

据我所知,你需要在一个'中传递值。 ' 所以你的SQL命令看起来应该是

<%= f.input_field :action, as: :hidden, value: ??? %> 

答案 1 :(得分:0)

首先,您将标签作为参数传递。 我假设标签文本包含您要在删除语句中使用的ID,因此您应该更改代码行:

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

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

其次,我假设你也有错误的删除查询。 您添加@Id参数,但不在查询中使用它。

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

Thirth,别忘了你的使用陈述。 SqlConnection和SqlCommand都是一次性的。 有关详细信息,请查看When should I use "using" blocks in C#?