Hi, I'm having problems with a DataTable I'm storing in a session variable. I use this as a temporary store for a shopping cart until a customer goes through the checkout and the cart is saved to my database.
I have used DataTables stored in sessions before for shopping baskets with no problems. However, on this occasion I am only passing the AddHandler the product id and the quantity being added. The Handler then calls a getProductDetai l method to lookup the product name & price from the database. The handler then passes these details to the AddToDataTable method to add a new row to the DataTable stored in the session.
For some reason the process of retrieving data from the database seems to be killing the session variable. Meaning everytime I add an item to the DataTable the previous item(s) already in the basket are lost and only the most recent item added appears.
I can't for the life of me figure out what is wrong. If I call the AddToDataTable method directly then everything works fine and multiple rows can be added to the TempBasket.
Here my code. Hopefullly someone can point out some stupid mistake I have made. I really need to get this working ASAP as I'm working to a deadline.
I am happy to upload the backend access database this page uses if someone wants to test the code.
Thanks in advance for any help that can offered.
I have used DataTables stored in sessions before for shopping baskets with no problems. However, on this occasion I am only passing the AddHandler the product id and the quantity being added. The Handler then calls a getProductDetai l method to lookup the product name & price from the database. The handler then passes these details to the AddToDataTable method to add a new row to the DataTable stored in the session.
For some reason the process of retrieving data from the database seems to be killing the session variable. Meaning everytime I add an item to the DataTable the previous item(s) already in the basket are lost and only the most recent item added appears.
I can't for the life of me figure out what is wrong. If I call the AddToDataTable method directly then everything works fine and multiple rows can be added to the TempBasket.
Here my code. Hopefullly someone can point out some stupid mistake I have made. I really need to get this working ASAP as I'm working to a deadline.
Code:
<%@ Page Language="C#" Debug="true" %> <%@ import Namespace="System" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.OleDb" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server"> void bindTempBasket() { DataTable dtTempBasket = mGetTempBasket(); dgDebugTableDisplay.DataSource = dtTempBasket; dgDebugTableDisplay.DataBind(); } public DataTable mGetDataTable(string sSQL) { string sCString = ConfigurationSettings.AppSettings["ConnectionString"]; using (OleDbConnection myConnection = new OleDbConnection(sCString)) { OleDbCommand myCommand = new OleDbCommand(sSQL, myConnection); OleDbDataAdapter myAdapter = new OleDbDataAdapter(myCommand); DataTable dtTable = new DataTable("Table"); myAdapter.Fill(dtTable); return dtTable; } } //End mGetDataTable method public DataTable mGetProductDetails(int iID) { string sSQL = "SELECT PVariant_ID, Product_Name, Product_TradeCost FROM qryProductList WHERE PVariant_ID = " + iID + ";"; DataTable dtProductDetails = mGetDataTable(sSQL); return dtProductDetails; } //End mGetProductDetails method public DataTable mCreateTempBasketTable() { DataTable myTable = new DataTable("Basket"); DataColumn dtCol; dtCol = new DataColumn(); dtCol.DataType = System.Type.GetType("System.Int32"); dtCol.ColumnName = "PVariant_ID"; myTable.Columns.Add(dtCol); dtCol = new DataColumn(); dtCol.DataType = System.Type.GetType("System.String"); dtCol.ColumnName = "Product_Name"; myTable.Columns.Add(dtCol); dtCol = new DataColumn(); dtCol.DataType = System.Type.GetType("System.Int32"); dtCol.ColumnName = "IOrderLine_Quantity"; myTable.Columns.Add(dtCol); dtCol = new DataColumn(); dtCol.DataType = System.Type.GetType("System.Double"); dtCol.ColumnName = "IOrderLine_UnitCost"; myTable.Columns.Add(dtCol); DataColumn[] myPrimaryKeyColumns = new DataColumn[1]; myPrimaryKeyColumns[0] = myTable.Columns["PVariant_ID"]; myTable.PrimaryKey = myPrimaryKeyColumns; return myTable; }//End createBasketTable Method public DataTable mGetTempBasket() { if (Session["basket"] == null) { DataTable dtBasket = mCreateTempBasketTable(); Session["basket"] = dtBasket; } return (DataTable)Session["basket"]; }//End mGetBasket Menthod public void mSaveTempBasket(DataTable newTable) { Session["basket"] = newTable; } void mAddHandler(int iID, int iQuantity) { DataTable dtProductDetail = mGetProductDetails(iID); if (dtProductDetail.Rows.Count > 0) { int iPVariant_ID = Convert.ToInt32(dtProductDetail.Rows[0]["PVariant_ID"]); string sProduct_Name = Convert.ToString(dtProductDetail.Rows[0]["Product_Name"]); double dUnitCost = Convert.ToDouble(dtProductDetail.Rows[0]["Product_TradeCost"]); mAddToDataTable(iPVariant_ID, sProduct_Name, iQuantity, dUnitCost); } } void mAddToDataTable(int iID, string sProductName, int iQuantity, double dUnitCost) { DataTable dtBasket = mGetTempBasket(); DataRow dtRow; dtRow = dtBasket.NewRow(); dtRow["PVariant_ID"] = iID; dtRow["Product_Name"] = sProductName; dtRow["IOrderLine_Quantity"] = iQuantity; dtRow["IOrderLine_UnitCost"] = dUnitCost; dtBasket.Rows.Add(dtRow); mSaveTempBasket(dtBasket); } void AddItem_Click(object sender, EventArgs e) { //Temp method for debug int iID = Convert.ToInt32(ItemCode.Text); //mAddToDataTable(iID, "Test Product Name", 1, "", 99.99); mAddHandler(iID, 1); bindTempBasket(); } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Product Search</title> <link rel="Stylesheet" href="CSS/default.css" /> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="ItemCode" runat="server"></asp:TextBox> <asp:Button ID="AddItem" runat="server" Text="Button" onclick="AddItem_Click" /> <asp:datagrid id="dgDebugTableDisplay" runat="server" /> </form> </body> </html>
Thanks in advance for any help that can offered.
Comment