Jay
asked on
Cannot reduce stock when button is clicked
Below is my code behind. When I click the add to cart button, the qty(eventParticipants) should decrease but it doesn't. Also, it says the name id does not exist in the current context for the id in the btnAdd_Click part below~~
string _connStr = ConfigurationManager.ConnectionStrings["EventContext"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
}
private void FillPage()
{
if(Request.QueryString["id"] == null)
{
Response.Redirect("Events.aspx");
}
else
{
//get selected event's data
if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
int id = Convert.ToInt32(Request.QueryString["id"]);
EventDetail Detail = new EventDetail();
EventDetail detail = Detail.getEventDetail(id);
if (detail == null)
{
Response.Write("<script>alert('No event found');</script>");
return;
}
//fill pg w data
lblDate.Text = detail.eventDate;
lblLocation.Text = detail.eventLocation;
lblStartTime.Text = detail.startTime;
lblEndTime.Text = detail.endTime;
lblParticipants.Text = detail.eventParticipants.ToString();
lblPrice.Text = "$" + detail.eventPrice;
lblTitle.Text = detail.eventDetailName;
lblDescription.Text = detail.eventDesc;
lblItemNr.Text = id.ToString();
imgEvent.ImageUrl = "~/EventImages/Events/" + detail.eventImage;
//fill amt ddl w numbers 1-20
int[] amount = Enumerable.Range(1, 20).ToArray();
ddlAmount.DataSource = amount;
ddlAmount.AppendDataBoundItems = true;
ddlAmount.DataBind();
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(_connStr);
if (con.State == ConnectionState.Open)
{
con.Close();
}
SqlCommand cmd1 = con.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "update EventDetail set eventParticipants=eventParticipants-" + lblParticipants.Text;
cmd1.ExecuteNonQuery();
Response.Redirect("Detail.aspx?id=" + id.ToString());
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you need to declare the int id at page level, not in the sub routine level.