0
我需要從網格中刪除單行,但每次單擊網格中的刪除按鈕時,我的整個數據庫都將被刪除。請幫我糾正我的代碼謝謝。刪除網格中的單行
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();
}
lblid.Text您在哪裏設置此文本? –
爲什麼你綁定你的數據源,然後**刪除行? – Tim
另外,你的if語句沒有意義。 'id == this.txtid.Text'將始終計算爲true,因爲id初始化爲'string id = this.txtid.Text;'。 – Tim