Why does the DropDownList consistently remove the initial value?

I am currently in the process of developing a recipe website focused on meals. I have created an admin panel where I can manage the meals added to the site. One issue I am facing is with deleting a meal that was previously added. The menu displays the number of meals in each category, such as Pasta(4) for example. As an admin, I am able to successfully add a new meal to the website by selecting a category. However, when it comes to deleting a meal, there seems to be an issue. It always deletes the first value in the DropDownList, which is Meat Foods. Can anyone assist me in finding a solution to this problem?

Below is the code snippet:

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";
    DropDownList1.DataValueField = "KategoriId";
    DropDownList1.DataSource = oku2;
    DropDownList1.DataBind();

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

//deletion operation
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>");
}

Answer №1

When passing an ID as a string from your query string to the parameter, you need to make sure it matches the correct data type. For example, if YemekId is an integer in your database, you can use the following code:

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

It's important to ensure that the data type of the parameter matches the value being passed in. In this case, the parameter should be an integer, not a string.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using .resx and .resource files in a custom server control in asp: a comprehensive guide

As I develop my own server-side control, I encounter an issue with using images stored in a .resx file. The code snippet below works seamlessly in a console application: ResXResourceReader rsxr = new ResXResourceReader("Resource1.resx"); ...

Prevent Previous/Next Buttons, Autofill, and Completion Bar on iPhone Web Forms

Can the bar on web forms that includes the (Previous|Next) Autofill and Done button be disabled or suppressed? I know there are solutions for native apps, but I am specifically working on a web app. I'm wondering if there is a simple attribute to add ...

Create a dynamic dropbox using JavaScript/JQuery in an HTML page

I am currently working on implementing 3 different drop down boxes that are triggered by 4 different buttons. These drop down boxes should appear within the #apple div. When Button B1 is clicked, the drop down options include Apple, Mango, and Papaya. Whe ...

What is the best way to create a div that maintains a consistent size while displaying images inside?

Struggling with a project and attempting to create a fixed div container for uniform picture sizes, I've tested various methods recommended by others but have had zero success in altering the size of my div container. Could it be due to my use of boot ...

Decoding JSON Array Data Sent via Ajax请求

Could someone provide guidance on parsing this Ajax response in jQuery? {"d":{"__type":"ListUsers+returnData","Type":"Success","Message":"User Added successfully."}} I am utilizing identical return types for various return data scenarios. ...

Attempting to serialize a form using Ajax and jQuery

I am facing an issue with submitting user input from a modal using jQuery and AJAX. The problem is that the AJAX call is not capturing the user input. Even though the AJAX call is successful, when I check on the server side, the user input appears to be bl ...

The background-size property in CSS does not seem to properly display on iPhones

Hello there! I'm facing an issue with my CSS code when it comes to displaying the image on Safari browser. It works perfectly fine on other browsers, but for some reason on Safari, the image doesn't show up correctly. I tried using a media query ...

Concealing a Specific Element with Text using jQuery

Is there a way to conceal the section that includes the element labeled as "Product Tags" on a webpage? ...

How to create a MySQL query within an HTML select dropdown field

For example, <select><option>Price</option></select> $sql=mysql_query("SELECT `rug_catagory_id` , `rug_price` FROM `rugs_all_data` WHERE `rug_catagory_id` =1 ...

Focusing on the initial element following CSS prioritization

Check out this codepen link: http://codepen.io/muji/pen/XpEYzO I am looking to target the first element after it has been sorted using a CSS "order" property and apply another CSS property to it. Is there a way to use jQuery or any other method to identi ...

Clicking on a span will allow you to alter the text within a paragraph located in a

I am hoping to update a paragraph of text with new content when a faux 'link' (span) at the bottom of the page is clicked. This is what I have so far <body> <p> This is the paragraph that should be changed after clicking on the sp ...

Adding rows dynamically to multiple tables on a single page using a single function

I am facing a situation where I have multiple tables and a function that adds rows to these tables: <table id="table1" style=" border-collapse: collapse;"> <tr id="table1row1">... <tr id="table1row2">... <table id="table2" style=" bor ...

When using jQuery and AJAX, the functions each, if, post, and .html work properly but occasionally erase data inexplicably

I have a PHP page containing FedEx and UPS tracking numbers stored in a MySQL database, which are displayed within DIVs with the class "trackingnumber". Using JavaScript, I loop through these divs to retrieve the ID and then send it to a PHP page that fetc ...

Trigger a dialogue box when a button is clicked to show dynamically inserted list elements

My goal is to have a collection of list items, each with its own button that triggers a unique dialog box displaying specific text. Below is the HTML code: <ul id="filter-list" class="scrollable-menu text-center"> @foreach (var filter in ...

The hamburger menu is malfunctioning and spilling over

// App.js import './App.css'; function App() { return ( <div className='navbar'> <label className="hamburger-menu"> <input type="checkbox" /> </label> <div class ...

Tips for embedding an SQL parameter within a textual query

When attempting to pass a parameter to a SQL query (in C#), I am facing an issue where the parameter is contained within a text block: SqlCommand cmd = new SqlCommand(@"EXEC sp_helptext N'dbo.@spname';"); cmd.Parameters.AddWithValue("@spname", " ...

The for loop does not cycle through every element

I'm working on a class project that involves creating custom HTML tags with JavaScript. I have a for loop set up to iterate over each use of the tag, but for some reason, it only seems to loop over every other tag. I'm specifically trying to crea ...

Enhanced button effect: image shaded on hover

Can someone help me figure out how to make the button and image darker when hovered over? I tried something but it didn't work... Here is a demonstration: http://jsfiddle.net/h2o8w5y6/ <!--- example code snippet --> <div class="col-lg-4 ...

implementing a time picker feature with jQuery

As I am not familiar with jQuery, I came across this script while searching for a time picker. If anyone could guide me on how to incorporate this code into my HTML page to enable the time picker functionality, I would greatly appreciate it. /* jQuery t ...

What is the best method for loading multiple HTML files into a Div container?

Recently, I made the decision to improve the look of an online manual I have been working on for my company by incorporating Bootstrap. The manual is structured with a tree-view that contains titles linking to HTML files with information and CSS stylesheet ...