and i like to make delete to any of the products so i make that code
protected void Page_Load(object sender, EventArgs e)
{
grdcart.DataSource = (DataTable)Session["cart"];
grdcart.DataBind();
double total = 0;
int x = 0;
for (x = 0; x < grdcart.Rows.Count; x++)
{
total += Convert.ToDouble(grdcart.Rows[x].Cells[5].Text);
}
lbltotal.Text = "Total of order(s) equal : " + total.ToString() + "of" + x.ToString() + "product(s)";
}
protected void grdcart_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable tbl;
if (Session["cart"] != null)
{
tbl = (DataTable)Session["cart"];
string[] Pk = { grdcart.Rows[e.RowIndex].Cells[0].Text, grdcart.Rows[e.RowIndex].Cells[1].Text };
DataRow row = tbl.Rows.Find(Pk);
if (row != null)
row.Delete();
}
but it doesnt delet it retun me error
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Source Error:
Line 34: { Line 35: tbl = (DataTable)Session["cart"]; Line 36: string[] Pk = { grdcart.Rows[e.RowIndex].Cells[0].Text, grdcart.Rows[e.RowIndex].Cells[1].Text }; Line 37: DataRow row = tbl.Rows.Find(Pk); Line 38: if (row != null)
whyyyyyyyyyyy ? and what is the solve for that problem
and if the customer choose the same product it get error how to avoid that or gethim message that product is choosen before the error when he choose the same product again is
Column 'CatNo, ProductId' is constrained to be unique. Value '2, 1' is already present.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.ConstraintException: Column 'CatNo, ProductId' is constrained to be unique. Value '2, 1' is already present.
Source Error:
Line 62: row[4] = Txtquantity.Text; Line 63: row[5] = Convert.ToInt16(Txtquantity.Text) * Convert.ToDouble(lblprice.Text); Line 64: tbl.Rows.Add(row); Line 65: Line 66: Session["cart"]=tbl;
thanks alot but error still found i add grdcart.DataBind(); and when try i discoverd that the momorey still save the last selected row so it appear despite it deleted so it get that error
alaae
Member
6 Points
46 Posts
Index was out of range
Aug 11, 2012 01:19 PM|LINK
hi every body i build shoppingcart where the customer can select products and i store his choosen in Datatable
this mycode
protected void Ibtncart_Click(object sender, ImageClickEventArgs e) { DataTable tbl; if (Session["cart"] == null) { tbl = new DataTable(); tbl.Columns.Add("CatNo"); tbl.Columns.Add("ProductId"); tbl.Columns.Add("ProName"); tbl.Columns.Add("Price"); tbl.Columns.Add("Quantity"); tbl.Columns.Add("SubTotal"); // primary keys DataColumn[]cols = {tbl.Columns[0],tbl.Columns[1]}; tbl.Constraints.Add("cart_pk",cols,true); } else tbl=(DataTable) Session["cart"]; // Session.Timeout = 50; DataRow row = tbl.NewRow(); row[0] = lblcat.Text; row[1] = lblproductid.Text; row[2] = lblproname.Text; row[3] = lblprice.Text; row[4] = Txtquantity.Text; row[5] = Convert.ToInt16(Txtquantity.Text) * Convert.ToDouble(lblprice.Text); tbl.Rows.Add(row); Session["cart"]=tbl; Response.Redirect("AllProducts.aspx"); }protected void Page_Load(object sender, EventArgs e) { grdcart.DataSource = (DataTable)Session["cart"]; grdcart.DataBind(); double total = 0; int x = 0; for (x = 0; x < grdcart.Rows.Count; x++) { total += Convert.ToDouble(grdcart.Rows[x].Cells[5].Text); } lbltotal.Text = "Total of order(s) equal : " + total.ToString() + "of" + x.ToString() + "product(s)"; } protected void grdcart_RowDeleting(object sender, GridViewDeleteEventArgs e) { DataTable tbl; if (Session["cart"] != null) { tbl = (DataTable)Session["cart"]; string[] Pk = { grdcart.Rows[e.RowIndex].Cells[0].Text, grdcart.Rows[e.RowIndex].Cells[1].Text }; DataRow row = tbl.Rows.Find(Pk); if (row != null) row.Delete(); }Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source Error:
Line 34: {
Line 35: tbl = (DataTable)Session["cart"];
Line 36: string[] Pk = { grdcart.Rows[e.RowIndex].Cells[0].Text, grdcart.Rows[e.RowIndex].Cells[1].Text };
Line 37: DataRow row = tbl.Rows.Find(Pk);
Line 38: if (row != null)
whyyyyyyyyyyy ? and what is the solve for that problem
and if the customer choose the same product it get error how to avoid that or gethim message that product is choosen before the error when he choose the same product again is
Column 'CatNo, ProductId' is constrained to be unique. Value '2, 1' is already present.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.ConstraintException: Column 'CatNo, ProductId' is constrained to be unique. Value '2, 1' is already present.
Source Error:
Line 62: row[4] = Txtquantity.Text;
Line 63: row[5] = Convert.ToInt16(Txtquantity.Text) * Convert.ToDouble(lblprice.Text);
Line 64: tbl.Rows.Add(row);
Line 65:
Line 66: Session["cart"]=tbl;
thanks for help
Careed
All-Star
18764 Points
3637 Posts
Re: Index was out of range
Aug 11, 2012 02:55 PM|LINK
In the if-block where Session["cart"] is null, you do not set Session["cart"] after you initialize the DataTable.
DataTable tbl; if (Session["cart"] == null) { tbl = new DataTable(); tbl.Columns.Add("CatNo"); tbl.Columns.Add("ProductId"); tbl.Columns.Add("ProName"); tbl.Columns.Add("Price"); tbl.Columns.Add("Quantity"); tbl.Columns.Add("SubTotal"); // primary keys DataColumn[]cols = {tbl.Columns[0],tbl.Columns[1]}; tbl.Constraints.Add("cart_pk",cols,true); Session["cart"] = tbl; } else ... ... ..."The oxen are slow, but the earth is patient."
alaae
Member
6 Points
46 Posts
Re: Index was out of range
Aug 11, 2012 10:15 PM|LINK
thanks alot but error still found i add grdcart.DataBind(); and when try i discoverd that the momorey still save the last selected row so it appear despite it deleted so it get that error
alaae
Member
6 Points
46 Posts
Re: Index was out of range
Aug 12, 2012 11:46 AM|LINK
up help
alaae
Member
6 Points
46 Posts
Re: Index was out of range
Aug 13, 2012 12:21 AM|LINK
thanksssssssssss i have solved it thanks for every reply