Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# ASP.net Session Query

Posted on 2013-05-15
4
Medium Priority
?
909 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 2000 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
How important is it to take extra precautions to protect your online business? These are some steps you can take to make sure you're free of any cyber crime.
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Suggested Courses

704 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