Question Decrement value from SQL database bug

Bojan123

New member
Joined
Jun 19, 2021
Messages
3
Programming Experience
Beginner
I have 2 asp.net WebForms, WebForm1 contains a button that redirects into WebForm2 which contains a contact form that needs to be filled to proceed an order. I have a drop down list in it that is connected to the database, and depending on which product a button on the WebForm1 is clicked, the current quantity is displayed from the specific product from the database. After the ordering, no matter what quantity is selected from the drop down list, it always decrements the quantity in the database by 1. Can anyone see my code and tell me where is my mistake? I tried many different approaches and methods but every time is the same, it only deducts the quantity by 1.

How to make deduction in the database by the same value that is selected from the drop down list?

Here is my code:

C#:
string productName = Request.QueryString["productname"];
        txt_product13.Text = productName;
        var dictionary = new Dictionary<string, object>
        {
            {"@ProductName", productName }
        };
        var parameters = new DynamicParameters(dictionary);
        string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (var connection = new SqlConnection(CS))
        {
            int val = Convert.ToInt32(dropdownlist1.SelectedItem.Text);
            connection.Open();
            var sql = "UPDATE ProductsDB SET Quantity = Quantity - " + val + " WHERE ProductName = @ProductName";
            connection.Execute(sql, parameters);
            connection.Close();
        }

In case it matters, upper in the code I have the same approach for getting the currency, and getting the quantity from a specific product in a drop down list starting from 1 to its max quantity:

C#:
string productName = Request.QueryString["productname"];
        txt_product13.Text = productName;

        var dictionary = new Dictionary<string, object>
        {
    { "@ProductName", productName }
          };

        var parameters = new DynamicParameters(dictionary);
        string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

        using (var connection = new SqlConnection(CS))
        {
            connection.Open();
            var sql = "SELECT * FROM ProductsDB WHERE ProductName = @ProductName";
            var product = connection.QuerySingle<Product>(sql, parameters);
            CultureInfo EuroCulture = new CultureInfo("fr-FR");
            txt_productprice.Text = product.Price.ToString("c", EuroCulture);

            for (int i = 1; i <= product.Quantity; i++)
            {
                dropdownlist1.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,904
Location
Sydney, Australia
Programming Experience
10+
Did you debug your code? If the value is being decremented by 1 then it must be that val is 1. You should be confirming that first. If that's the case then none of that code is really relevant and the issue is that the SelectedItem is not what you think it should be. If it's not the case then the SQL you think you're executing must not be what's actually being executed, unless something else is broken.

By the way, since when does a SqlConnection object have an Execute method?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,904
Location
Sydney, Australia
Programming Experience
10+
Please post your questions in the most appropriate forum. This question has nothing to do with VS so it doesn't belong in the VS.NET General forum, which is for IDE questions. This is either a data access issue or a Web Forms issue. I'm guessing the latter and moving this thread there.
 

Bojan123

New member
Joined
Jun 19, 2021
Messages
3
Programming Experience
Beginner
Did you debug your code? If the value is being decremented by 1 then it must be that val is 1. You should be confirming that first. If that's the case then none of that code is really relevant and the issue is that the SelectedItem is not what you think it should be. If it's not the case then the SQL you think you're executing must not be what's actually being executed, unless something else is broken.

By the way, since when does a SqlConnection object have an Execute method?
I just debbuged and the val is 1, you are right. Any ideas how to fix that?
Btw I was trying some other methods and I forgot to delete that :D
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Make sure that your page load event handler is not accidentally overwriting the selected item when it's a post back.

If you are not use a post back, then how are you communicating between form 1 and form 2 so that form 2 picks up values selected from form 1?
 

Bojan123

New member
Joined
Jun 19, 2021
Messages
3
Programming Experience
Beginner
The clearing method of the drop down list dropdownlist1.SelectedIndex = -1; upper in the code was the problem.
However, thank you guys.
 
Top Bottom