Solved

C# ASP.net Session Query

Posted on 2013-05-15
4
819 Views
Last Modified: 2013-05-20
Hi I wrote Add Cart method, its working fine. I alreday added session, my question is
How to i wrote viewcart using sessions.
for example after i add to several products, i need to click view cart and if i need to go back to another product and when i am comming all the added products should be in the Viewcart page.
if any one help me how to do that.

<form id="Form1" runat="server"> 

Product:<br> 

<asp:DropDownList id="ddlProducts" runat="server"> 

<asp:ListItem Value="4.99">Socks</asp:ListItem> 

<asp:ListItem Value="34.99">Pants</asp:ListItem> 

<asp:ListItem Value="14.99">Shirt</asp:ListItem> 

<asp:ListItem Value="12.99">Hat</asp:ListItem> 

</asp:DropDownList><br> 

Quantity:<br> 

<asp:textbox id="txtQuantity" runat="server" /><br><br> 

<asp:Button id="btnAdd" runat="server" Text="Add To Cart"  

onClick="AddToCart" /><br><br> 

<asp:DataGrid id="dg" runat="server" /><br><br> 

Total:  

<asp:Label id="lblTotal" runat="server" /> 

</form>


sing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Test : System.Web.UI.Page
{

    DataTable objDT = new DataTable("Cart");
    DataRow objDR;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            makeCart();

        }
    }
    protected void AddToCart(object sender, EventArgs e)
    {


        addCart();

    }
    public void makeCart()
    {
        
        objDT.Columns.Add("ID",typeof(int));

        objDT.Columns["ID"].AutoIncrement = true;

        objDT.Columns["ID"].AutoIncrementSeed = 1;



        objDT.Columns.Add("Quantity", typeof(int));

        objDT.Columns.Add("Product", typeof(string));

        objDT.Columns.Add("Cost", typeof(decimal));



        Session["Cart"] = objDT; 
    


    }

    public void addCart()
    {

     objDT = (DataTable)Session["Cart"];
     string Product="";
    Product = ddlProducts.SelectedItem.Text;

    Boolean blnMatch = false;
    
        foreach(DataRow s in objDT.Rows)
        {
            objDR = s;
          if(objDR["Product"]==Product)
          {
          objDR["Quantity"]+= txtQuantity.Text;
              blnMatch=true;
              break;
              
          }
        
        }

   
        
        if (blnMatch==false )
        {
            
    objDR = objDT.NewRow();

     objDR["Quantity"] = txtQuantity.Text;  

     objDR["Product"] = ddlProducts.SelectedItem.Text;  

     objDR["Cost"] = Decimal.Parse(ddlProducts.SelectedItem.Value);  

     objDT.Rows.Add(objDR);  
        }
     Session["Cart"] = objDT  ;

  

     dg.DataSource = objDT;

     dg.DataBind();  
    
    }

 

     

}

Open in new window

0
Comment
Question by:ukerandi
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Jitendra Patil
ID: 39170669
Hi ukerandi,

you can get it functional using session.

as you have already used session, you just need to create a new page called as viewcart, in this page you can access the session data just as you are binding your gridview data.

once you left viewcart page the data saved in session will be there till your session is active,
you can pass object, datatables in session through pages in your asp.net application.

hope this helps.
0
 
LVL 10

Author Comment

by:ukerandi
ID: 39170780
thank you very much for your prompt reply.
yes understand i need to create vew.aspx page, but i confused how to retrived data (session Data, if you can give me example i can understand quickly.
for example i know in the session["Cart"] all the records there
i just confused how to use while loop to access Session Data
0
 
LVL 12

Accepted Solution

by:
Jitendra Patil earned 500 total points
ID: 39174464
on the view.aspx page just write the below code in page load event

DataTable ObjDT= new DataTable();
 objDT = (DataTable)Session["Cart"];

foreach(DataRow s in objDT.Rows)
        {
 // do whatever you want.        
        }


suppose you ad a new gridview to the view.aspx then you can bind the gridview using the below code

just add the code on pageload of your view.aspx

if(!ispostback)
{
  DataTable Dt= new DataTable();
  dt=(DataTable)Session["Cart"];
 
 gridview1.DataSource=dt;
gridview1.DataBind();
}

so the above code will bind your gridview with the session data.

hope this helps.
0
 
LVL 10

Author Closing Comment

by:ukerandi
ID: 39182974
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now