简体   繁体   English

DropDownList 总是删除第一个值?

[英]DropDownList always delete the first value?

i'm creating a recipe website for meals.我正在创建一个膳食食谱网站。 I make an admin panel and i want to delete a meal which i added before.我做了一个管理面板,我想删除我之前添加的一顿饭。 And there is a menu which includes number of the meal.还有一个菜单,其中包括餐点。 For example there are 4 meals in pasta category and it shows Pasta(4) at mainpage.例如,意大利面类别中有 4 餐,它在主页上显示 Pasta(4)。 Me as an admin, if i would like to add a new meal to my website, it's working.我作为管理员,如果我想在我的网站上添加新餐点,它可以工作。 I select a category and it's adding to that category.我 select 一个类别,它正在添加到该类别中。 But for deleting, it's working wrong.但是对于删除,它工作错误。 It always delete the first value in DropDownList which is Meat Foods.它总是删除 DropDownList 中的第一个值,即 Meat Foods。 Can you help me to find an answer?你能帮我找到答案吗?

Here is the code:这是代码:

if (Page.IsPostBack == false)
{
    id = Request.QueryString["YemekId"];
    islem = Request.QueryString["islem"];

    SqlCommand komut2 = new SqlCommand("Select *from Kategoriler", baglan.baglanti());
    SqlDataReader oku2 = komut2.ExecuteReader();
    DropDownList1.DataTextField = "KategoriAd"; //DropDownList'e kategorilerin adlarını text biçiminde oldukları için bu şekilde çektik.
    DropDownList1.DataValueField = "KategoriId"; //DropDownList'e kategorilerin id nolarını value değerinde oldukları için bu şekilde çektik.
    DropDownList1.DataSource = oku2;
    DropDownList1.DataBind();

    SqlCommand komut = new SqlCommand("Select *from Yemek", baglan.baglanti());
    SqlDataReader oku = komut.ExecuteReader();
    DataList1.DataSource = oku;
    DataList1.DataBind();
}

//silme işlemi
if (islem == "sil")
{
    SqlCommand komutsilme = new SqlCommand("Delete from Yemek where YemekId=@s1", baglan.baglanti());
    komutsilme.Parameters.AddWithValue("@s1", id);
    komutsilme.ExecuteNonQuery();
    baglan.baglanti().Close();

    SqlCommand kategorisil = new SqlCommand("Update Kategoriler set KategoriAdet=KategoriAdet-1 where KategoriId=@k1", baglan.baglanti());
    kategorisil.Parameters.AddWithValue("@k1", DropDownList1.SelectedValue);
    kategorisil.ExecuteNonQuery();
    baglan.baglanti().Close();

    Response.Write("<script> alert('Yemek başarıyla silindi!')</script>");
}

you are passing a id as a string from your query string to the parameter您正在将 id 作为字符串从查询字符串传递给参数

komutsilme.Parameters.AddWithValue("@s1", id);

I am assuming YemekId is an int in your database so try this我假设 YemekId 是您数据库中的一个 int ,所以试试这个

komutsilme.Parameters.AddWithValue("@s1", int.Parse(id));

AddWithValue will derive the type of the parameter of its value, so ensure that it's the correct type, in this case int not string. AddWithValue将导出其值的参数类型,因此请确保它是正确的类型,在这种情况下是 int 而不是 string。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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