Solved

C# ASP.net Session Query

Posted on 2013-05-15
4
844 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

820 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