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:
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:
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()));
}