Solved

C# ASP.net Session Query

Posted on 2013-05-15
4
880 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 13

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 13

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…

617 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