4
Reply

More than 1 row in DataTable for shopping cart

Mikael

Mikael

Jun 8 2010 10:41 AM
3.8k
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;

}
}






}


Answers (4)