Mates i have an issue in product_desc page.cs when i am adding this code if (!IsPostBack) plus and minus button works fine increase and decrease but the values r not inserting into database such as product_name product_images and product_desc rest of the values r inserting properly, but when i remove this code again if (!IsPostBack) all the values insert properly in the database perfectly but plus and minus button doest work.....help
What I have tried:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
public partial class Shopping_website_user_product_desc : System.Web.UI.Page
{
private string connectionString = ConfigurationManager.ConnectionStrings["shoppingmaster"].ConnectionString;
private int id;
private int qty;
private string product_name, product_desc, product_images;
protected Label l1;
protected Label l2;
protected Button b1;
protected Label t1;
protected Label lblPrice;
protected TextBox txtQuantity;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] == null)
{
Response.Redirect("display_item.aspx");
}
else
{
id = Convert.ToInt32(Request.QueryString["id"]);
if (!IsPostBack)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM product WHERE id = @id";
cmd.Parameters.AddWithValue("@id", id);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
product_name = reader["product_name"].ToString();
product_desc = reader["product_desc"].ToString();
product_images = reader["product_images"].ToString();
}
reader.Close();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
d1.DataSource = dt;
d1.DataBind();
con.Close();
}
}
qty = get_qty(id);
if (qty == 0)
{
l2.Visible = false;
txtQuantity.Visible = false;
b1.Visible = false;
l1.Text = "There is no available quantity of this item.";
}
foreach (RepeaterItem item in d1.Items)
{
TextBox txtQuantity = (TextBox)item.FindControl("t1");
if (txtQuantity != null)
{
if (IsPostBack)
{
int product_qty = Convert.ToInt32(txtQuantity.Text);
txtQuantity.Text = product_qty.ToString();
}
else
{
txtQuantity.Text = "1";
}
}
}
}
}
protected void btnMinus_Click(object sender, EventArgs e)
{
Button btnMinus = (Button)sender;
RepeaterItem item = (RepeaterItem)btnMinus.NamingContainer;
TextBox t1 = (TextBox)item.FindControl("t1");
int product_qty = Convert.ToInt32(t1.Text);
if (product_qty > 1)
{
product_qty--;
t1.Text = product_qty.ToString();
}
}
protected void btnPlus_Click(object sender, EventArgs e)
{
Button btnPlus = (Button)sender;
RepeaterItem item = (RepeaterItem)btnPlus.NamingContainer;
TextBox t1 = (TextBox)item.FindControl("t1");
int product_qty = Convert.ToInt32(t1.Text);
product_qty++;
t1.Text = product_qty.ToString();
}
private bool UserLoggedIn()
{
return Session["user"] != null;
}
protected void b1_Click(object sender, EventArgs e)
{
if (!UserLoggedIn())
{
Response.Redirect("login.aspx?returnUrl=product_desc.aspx?id=" + id);
return;
}
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
RepeaterItem item = (RepeaterItem)((Button)sender).NamingContainer;
TextBox t1 = (TextBox)item.FindControl("t1");
if (t1 != null && !string.IsNullOrEmpty(t1.Text))
{
int product_qty;
if (int.TryParse(t1.Text, out product_qty))
{
double product_price = GetProductPrice(id);
if (product_price == 0)
{
lblPrice.Text = "Error: Product price not found.";
return;
}
double totalPrice = product_price * product_qty;
double taxAmount = totalPrice * 0.15;
totalPrice += taxAmount;
if (lblPrice != null)
{
lblPrice.Text = "Total Price (including 15% tax): " + totalPrice.ToString("C2");
}
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM registration WHERE email=@email";
cmd.Parameters.AddWithValue("@email", Session["user"]);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string firstname = dr["firstname"].ToString();
string lastname = dr["lastname"].ToString();
string email = dr["email"].ToString();
string address = dr["address"].ToString();
string city = dr["city"].ToString();
string state = dr["state"].ToString();
string pincode = dr["pincode"].ToString();
string mobile = dr["mobile"].ToString();
string product_name = this.product_name;
string product_images = this.product_images;
if (!string.IsNullOrEmpty(product_name) && !string.IsNullOrEmpty(product_images))
{
SqlCommand cmd3 = con.CreateCommand();
cmd3.CommandType = CommandType.Text;
cmd3.CommandText = "INSERT INTO orders (firstname, lastname, email, address, city, state, pincode, mobile, DateOfOrder, payment_status, product_id, product_name, product_price, product_qty, total_price, product_images) " +
"VALUES (@firstname, @lastname, @email, @address, @city, @state, @pincode, @mobile, @DateOfOrder, @payment_status, @product_id, @product_name, @product_price, @product_qty, @total_price, @product_images)";
cmd3.Parameters.AddWithValue("@firstname", firstname);
cmd3.Parameters.AddWithValue("@lastname", lastname);
cmd3.Parameters.AddWithValue("@email", email);
cmd3.Parameters.AddWithValue("@address", address);
cmd3.Parameters.AddWithValue("@city", city);
cmd3.Parameters.AddWithValue("@state", state);
cmd3.Parameters.AddWithValue("@pincode", pincode);
cmd3.Parameters.AddWithValue("@mobile", mobile);
cmd3.Parameters.AddWithValue("@DateOfOrder", DateTime.Now);
cmd3.Parameters.AddWithValue("@payment_status", "Pending");
cmd3.Parameters.AddWithValue("@product_id", id);
cmd3.Parameters.AddWithValue("@product_name", product_name);
cmd3.Parameters.AddWithValue("@product_price", product_price);
cmd3.Parameters.AddWithValue("@product_qty", product_qty);
cmd3.Parameters.AddWithValue("@total_price", totalPrice);
cmd3.Parameters.AddWithValue("@product_images", product_images);
cmd3.ExecuteNonQuery();
}
else
{
dr.Close();
}
SqlCommand cmd1 = con.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "UPDATE product SET product_qty = product_qty - @quantity WHERE id = @id";
cmd1.Parameters.AddWithValue("@quantity", product_qty);
cmd1.Parameters.AddWithValue("@id", id);
cmd1.ExecuteNonQuery();
int orderId = GetLastInsertedOrderId();
if (Request.Cookies["aa"] == null)
{
Response.Cookies["aa"].Value = product_name + "," + product_desc + "," + product_price + "," + product_qty + "," + product_images + "," + id + "," + orderId;
Response.Cookies["aa"].Expires = DateTime.Now.AddDays(1);
}
else
{
Response.Cookies["aa"].Value = Request.Cookies["aa"].Value + "|" + product_name + "," + product_desc + "," + product_price + "," + product_qty + "," + product_images + "," + id + "," + orderId;
Response.Cookies["aa"].Expires = DateTime.Now.AddDays(1);
}
}
}
}
}
}
public int get_qty(int id)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT product_qty FROM product WHERE id=@id";
cmd.Parameters.AddWithValue("@id", id);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
qty = Convert.ToInt32(dr["product_qty"]);
}
dr.Close();
}
return qty;
}
public int GetLastInsertedOrderId()
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT TOP 1 Id FROM orders ORDER BY Id DESC";
int orderId = Convert.ToInt32(cmd.ExecuteScalar());
return orderId;
}
}
public double GetProductPrice(int id)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT product_price FROM product WHERE id=@id";
cmd.Parameters.AddWithValue("@id", id);
object result = cmd.ExecuteScalar();
if (result != null && double.TryParse(result.ToString(), out double price))
{
return price;
}
return 0;
}
}
}