使用下拉列表从数据库中删除

时间:2013-07-13 15:48:35

标签: c# asp.net

嗨我使用下拉列表从表中获取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");

1 个答案:

答案 0 :(得分:0)

假设您的绑定逻辑在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()
}