Skip to content
Loading
+4
  • Sachin Singh
    +3
    This is what i mean
    1. var dictionary = new Dictionary { { "@ProductName",productName }  };    
    2.  var parameters = new DynamicParameters(dictionary);    
    3.     
    4. using (var connection = new SqlConnection(CS))        
    5.         {        
    6.             connection.Open();        
    7. var sql = "UPDATE ProductsDB SET Quantity = Quantity - " + dropdownlist1.SelectedValue + " WHERE ProductName = @ProductName'";        
    8.   connection.Execute(sql,parameters);      
    9.    
    10.             connection.Close();        
    11.         }       
    12.    
     
    +3
  • Sachin Singh
    +3
    please try to correct the syntax as
    1. var sql = "UPDATE ProductsDB SET Quantity = Quantity - " + dropdownlist1.SelectedValue + " WHERE ProductName = @ProductName";  
     
    +3
  • +2

    So what does it need to be in your 06 and 07 lines of code at my case?

    Because it does not update and decrement the quantity with this code..

    Sachin Singh

    +2
  • +2
    string productName = Request.QueryString["productname"];
    var dictionary = new Dictionary
    {
    {"@ProductName", productName }
    };
    var parameters = new DynamicParameters(dictionary);
    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (var connection = new SqlConnection(CS))
    {
    connection.Open();
    var sql = "UPDATE ProductsDB SET Quantity = Quantity - " + dropdownlist1.SelectedValue + " WHERE ProductName = @ProductName'";
    connection.Execute(sql, parameters);
    connection.Close();
    }
     
     
    With this code it throws an error:
     
    +2
  • +2
    Thank you sir. 
    The problem actually was because i had a selected hidden list item :
    to replace a placeholder of the drop down list and that was the error.
     
    Very grateful for your help, cheers. 
    +2
  • +2
    I just saw that whatever quantity form the drop down list I select, it decrements only 1 from the database.
    Ex. From one certain product I had 9 in the database and from the drop down list I selected 3, it needs to have 6 in the database now right? Well I don't know why but there is only one decremented so the number is 8.. 
    Any idea how to solve that?
    +2
  • Sachin Singh
    +2
    ok, test this
    C#
    var sql = "UPDATE ProductsDB SET Quantity = Quantity - " + dropdownlist1.SelectedItem.Value+ 
    " WHERE ProductName = @ProductName";
     
    +2
  • +2
    Still the same, only decrements one, any other ideas?
    +2
  • Sachin Singh
    +2
    show me your dropdown list
    +2
  • +2
      So depending on which product i pressed a button on the main webform the drop down list changes, for example this is the mobile Meizu Pro, for which I have 6 in quantity in database, here it is how it looks.
    Whatever value i choose and proceed with order, it only decrements one.
    For example if i continue with my order like this there will be 5 in the database. 
     
     
    +2
  • +2

    I can't get it what is the problem, it is again the same..

    Here is my whole C# code on the page, I may be wrong somewhere upper on the code with some string or integer so it may confuses it..

    Here is my code:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Configuration;

    using System.Drawing;

    using System.Net;

    using System.Net.Mail;

    using System.Text;

    using System.Data.SqlClient;

    using Dapper;

    using System.Globalization;

    namespace GraduationThesis

    {

        public partial class ShopNowPage : System.Web.UI.Page

        {

            protected void Page_Load(object sender, EventArgs e)

            {

                string productName = Request.QueryString["productname"];

                txt_product13.Text = productName;

                var dictionary = new Dictionary

                {

            { "@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(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()));

                    }

                }

             

            }

            public class Product

            {

                public int ProductID { get; set; }

                public string ProductName { get; set; }

                public decimal Price { get; set; }

                public int Quantity { get; set; }

            }

            protected void button11_Click(object sender, EventArgs e)

            {

                try

                {

                    var from = "email1";

                    var to = "email2";

                    const string Password = "password";

                    string mail_subject = txt_subject13.Text.ToString();

                    string mail_message = "Name and Surname: " + txt_nameandsurname13.Text + "\n";

                    mail_message += "Your telephone number: " + txt_phone13.Text + "\n";

                    mail_message += "Your address: " + txt_address13.Text + "\n";

                    mail_message += "Your e-mail: " + txt_email13.Text + "\n";

                    mail_message += "Product name: " + txt_product13.Text + "\n";

                    mail_message += "Product price: " + txt_productprice.Text + "\n";

                    mail_message += "Quantity of the product: " + dropdownlist1.SelectedValue + "\n";

                    mail_message += "Payment method: " + dropdownlist13.SelectedValue + "\n";

                    var smtp = new SmtpClient();

                    {

                        smtp.Host = "smtp.gmail.com";

                        smtp.Port = 587;

                        smtp.EnableSsl = true;

                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

                        smtp.Credentials = new NetworkCredential(from, Password);

                        smtp.Timeout = 20000;

                    }

                    smtp.Send(from, to, mail_subject, mail_message);

                    confirm.Text = "Thank you for your order, our team will deliver the product you ordered at your home address as soon as possible.";

                    txt_nameandsurname13.Text = "";

                    txt_phone13.Text = "";

                    txt_address13.Text = "";

                    txt_email13.Text = "";

                    txt_subject13.Text = "";

                    txt_product13.Text = "";

                    dropdownlist1.SelectedIndex = -1;

                    dropdownlist13.SelectedIndex = -1;

                }

                catch (Exception)

                {

                    confirm.Text = "The order can't be proceed at the moment, please try again later.";

                    confirm.ForeColor = Color.Red;

                }

                string productName = Request.QueryString["productname"];

                txt_product13.Text = productName;

                var dictionary = new Dictionary

                {

                    {"@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();

                }

            }

        }

    }

    +2
  • +2

    The clearing method of the drop down list dropdownlist1.SelectedIndex = -1; upper in the code was the problem.
    However, thank you.

    +2