簡體   English   中英

使用下拉列表從數據庫中刪除

[英]deleting from DB using a dropdownlist

您好,即時通訊使用dropdownlist從表中獲取ID,然后在運行頁面teh dropdownlist時使用刪除按鈕刪除該行,所有表中的ID都很好,但是當我刪除它時,即使我選擇了它,也總是刪除最后一個ID另一個

List<classe_cv_langues> li6 = new List<classe_cv_langues>();
    SqlConnection con4 = new SqlConnection(@"Data Source=p5-pc\sqlexpress;Initial Catalog=recrutement_online_3;Integrated Security=True");
    SqlCommand cmd4 = new SqlCommand();
    cmd4.Connection = con4;
    con4.Open();
    cmd4.CommandText = "select id from cv_langues as cl inner join cv as c on cl.id_cv = c.id_cv where id_candidat= " + Session["Id_candidat"];
    SqlDataReader dr4 = cmd4.ExecuteReader();
    while (dr4.Read())
    {
        classe_cv_langues p6 = new classe_cv_langues();
        p6.Id = int.Parse(dr4[0].ToString());
        li6.Add(p6);


    }
    dr4.Close();
    con4.Close();


    DropDownList7.DataSource = li6;
    DropDownList7.DataTextField = "id";
    DropDownList7.DataValueField ="id";

    DropDownList7.DataBind()

刪除按鈕:

SqlConnection con = new SqlConnection(@"Data Source=p5-pc\sqlexpress;Initial Catalog=recrutement_online_3;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    con.Open();
    cmd.CommandText = "delete from cv_langues where id='"+DropDownList7.SelectedValue+"'";
    cmd.ExecuteNonQuery();
    con.Close();

    Server.Transfer("gestion_cv.aspx");

假設您的綁定邏輯在page_load方法上。 嘗試測試Page.IsPostBack

if (!Page.IsPostBack)
{
    List<classe_cv_langues> li6 = new List<classe_cv_langues>();
    SqlConnection con4 = new SqlConnection(@"Data Source=p5-pc\sqlexpress;Initial Catalog=recrutement_online_3;Integrated Security=True");
    SqlCommand cmd4 = new SqlCommand();
    cmd4.Connection = con4;
    con4.Open();
    cmd4.CommandText = "select id from cv_langues as cl inner join cv as c on cl.id_cv = c.id_cv where id_candidat= " + Session["Id_candidat"];
    SqlDataReader dr4 = cmd4.ExecuteReader();
    while (dr4.Read())
    {
        classe_cv_langues p6 = new classe_cv_langues();
        p6.Id = int.Parse(dr4[0].ToString());
        li6.Add(p6);


    }
    dr4.Close();
    con4.Close();


    DropDownList7.DataSource = li6;
    DropDownList7.DataTextField = "id";
    DropDownList7.DataValueField ="id";

    DropDownList7.DataBind()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM