Hai guys if its possible to write foreach loop inside if and else condition possible
protected void btnAdd_Click(object sender, EventArgs e)
{
Button adding = sender as Button;
string count = ((TextBox)adding.Parent.FindControl("txtcount")).Text;
if (adding != null)
{
string[] commandargs = adding.CommandArgument.ToString().Split(new char[] { ',' });
ViewState["dtype"] = commandargs[0];
ViewState["productname"] = commandargs[1].TrimStart();
ViewState["productprice"] = commandargs[2];
ViewState["productdescription"] = commandargs[3];
}
DataTable adlist = (DataTable)ViewState["menu"];
foreach (DataRow row in adlist.Rows)
{
if (row["ProductName"].ToString() == ViewState["productname"].ToString())
{
adfilter();
DataRow[] grow = adlist.Select("[ProductName] ='" + ViewState["productname"].ToString() + "'");
decimal pricingname = Convert.ToDecimal(ViewState["adprice"]);
decimal adquant = Convert.ToDecimal(grow[0]["Quantity"]);
decimal adprice = Convert.ToDecimal(count);
decimal toadprice = adquant+adprice;
decimal highcal = toadprice * pricingname;
grow[0]["Quantity"] = toadprice;
grow[0]["ProductPrice"] = highcal.ToString();
adlist.AcceptChanges();
ViewState["menu"] = adlist;
rptordermenu.DataSource = adlist;
rptordermenu.DataBind();
calculation();
}
else
{
decimal pprice = Convert.ToDecimal(ViewState["productprice"]);
decimal cquant = Convert.ToDecimal(count);
decimal qcprice = cquant * pprice;
adlist = (DataTable)ViewState["menu"];
namescounter = Convert.ToInt32(ViewState["Number"]) + 1;
ViewState["Number"] = namescounter;
DataRow drow = adlist.NewRow();
drow[0] = namescounter;
drow["ProductName"] = ViewState["productname"].ToString();
drow["ProductPrice"] = qcprice.ToString();
drow["Quantity"] = count;
adlist.Rows.Add(drow);
ViewState["menu"] = adlist;
rptordermenu.DataSource = adlist;
rptordermenu.DataBind();
calculation();
}
}
}