Hi everyone!
As I'm new here I might say a few quick words about me and my programming skills..
I am a novice programmer and have studied ASP.NET with C# for a year now, so my programming skills are pretty limited.
Now to my problem.. I am trying to create a Very simple shopping cart, and it is going ok, but have gotten stuck on one detail.
I can put one product in the DataTable, and it even stays in there as I move on to other parts of the page, but if I try to put one more item in the cart, it just replaces the current item in the cart.
So hopefully you can help me understand what is wrong with my code, which I will post now(I'll post the code that is relevant):
public partial class ShowProduct : System.Web.UI.Page { public DataTable DT = new DataTable();
protected void Button1_Click(object sender, EventArgs e) { AddNewRow(); GridView1.DataSource = (DataTable)Session["mySession"]; GridView1.DataBind(); } private void AddNewRow() { int Artnr = Convert.ToInt32(Request.QueryString.Get("Artnr")); LuftcoPbrytfjader myProduct;
using (KlippexDataContext kc = new KlippexDataContext()) { myProduct = (from r in kc.LuftcoPbrytfjaders where r.Artnr == Artnr select r).Single();
DataColumn MyColumn;
MyColumn = new DataColumn("Artikelnummer"); DT.Columns.Add(MyColumn); MyColumn = new DataColumn("Produktnamn"); DT.Columns.Add(MyColumn); MyColumn = new DataColumn("Pris"); DT.Columns.Add(MyColumn);
DataRow MyRow = DT.NewRow();
MyRow[0] = myProduct.Artnr; MyRow[1] = myProduct.Produktnamn; MyRow[2] = myProduct.Pris;
DT.Rows.Add(MyRow); DT.AcceptChanges();
Session["mySession"] = DT; } }
}
|
MikaelPosted Jun 10, 2010, 4:22 AM
*Seemed like I was creating a new table everytime I added a product, so that's what I fixed.
LiyaPosted Jun 10, 2010, 4:19 AM
private void AddNewRow()
{
//put at the beginning
DT = Session["mySession"];
//add to DT with your code
//last row
Session["mySession"] = DT;
}
MikaelPosted Jun 8, 2010, 3:59 PM
I tried to put Session["mySession"] = null
both in my click event and in my AddNewRow() function.
CrishPosted Jun 8, 2010, 1:09 PM
make Session["mySession"] = null on every time