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):
C#
publicpartialclassShowProduct : System.Web.UI.Page
{
public DataTable DT = new DataTable();
protectedvoidButton1_Click(object sender, EventArgs e)
{
AddNewRow();
GridView1.DataSource = (DataTable)Session["mySession"];
GridView1.DataBind();
}
privatevoidAddNewRow()
{
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;
}
}
}
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
CrishRank #1,486 · 1.1K reputationPosted Jun 8, 2010, 9:09 AM EDT
make Session["mySession"] = null on every time
Mikael5 reputationPosted Jun 8, 2010, 11:59 AM EDT
I tried to put Session["mySession"] = null
both in my click event and in my AddNewRow() function.
Liya41 reputationPosted Jun 10, 2010, 12:19 AM EDT
private void AddNewRow()
{
//put at the beginning
DT = Session["mySession"];
//add to DT with your code
//last row
Session["mySession"] = DT;
}
Mikael5 reputationPosted Jun 10, 2010, 12:22 AM EDT
*Seemed like I was creating a new table everytime I added a product, so that's what I fixed.