Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# ASP.net Session Query

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Every business owner understands the significance of online customer reviews and the impact it can have on sales and revenues. With technology advancing at such a rapid pace, getting online reviews has never been easier, especially when many regions…
While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
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

972 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