GridView中的删除问题在问
点击页面的删除,程序运行到 st_comm.ExecuteNonQuery();时提示将参数值从 DataKey 转换到 String 失败,而不加int,也是将参数值从 DataKey 转换到 String 失败,而GridView1.DataKeys[(int)e.RowIndex].Value.ToString();,提示了删除成功,但回到页面上,数据还在,,数据库里数据也在,没有被删除,帮帮我,谢谢!!
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string st_sqlstr = "delete from ST_student where ST_Student_id=@userid ";
SqlCommand st_comm = new SqlCommand(st_sqlstr, st_conn);
st_comm.Parameters.Add(new SqlParameter( "@userid ", SqlDbType.VarChar,50));
st_comm.Parameters[ "@userid "].Value =GridView1.DataKeys[(int)e.RowIndex];
st_comm.Connection.Open();
try
{
st_comm.ExecuteNonQuery();
st_lbl_note.Text= "删除成功 ";
}
catch(SqlException)
{
st_lbl_note.Text= "删除失败 ";
st_lbl_note.Style[ "color "]= "red ";
}
ST_BindGrid();
}
------解决方案--------------------确实奇怪
------解决方案--------------------st_comm.Parameters[ "@userid "].Value =GridView1.DataKeys[(int)e.RowIndex].Value;
------解决方案--------------------另:(int)没有必要.