我需要从网格中删除一行,但是每次单击网格中的删除按钮时,我的整个数据库都会被删除。请帮我纠正我的代码谢谢。
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
gridbind();
con.Open();
SqlCommand cmd = new SqlCommand ("delete from registration where Id=" + lblid.Text + "", con);
cmd.ExecuteNonQuery();
con.Close();
}
和 gridbind 功能是:
private void gridbind()
{
string sql = string.Empty;
string id = this.txtid.Text;
string gender = this.DropDownList1.SelectedValue;
if (gender =="male" || gender == "female")
sql = "Select * from registration WHERE Id like '%" + id + "%' AND gender = '"+gender+"'";
if (gender =="male" || gender == "female" && id == this.txtid.Text)
sql = " Select * from registration Where Id like '%" + id + "%' ";
if (gender == "all" && id == this.txtid.Text)
sql = " Select * from registration Where Id like '%" + id + "%' ";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@id",id);
cmd.Parameters.AddWithValue("@gender", gender);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}