简体   繁体   中英

deleting from DB using a dropdownlist

hi im using a dropdownlist to get id from a table and then delete the line with a delete buton when i run the page teh dropdownlist get all the id's in the table all fine but when i delete it always delete the last id even if i select another

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()

the delete buton:

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");

Assuming your binding logic is on page_load method. Try testing for 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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM