Showing posts with label cookie. Show all posts
Showing posts with label cookie. Show all posts

Wednesday, June 17, 2015

Shopping Cart with Form Web Application with Asp.Net (Part 1)

In this project, we will design a website that displays information and allow user to add products into shopping cart. We willl design four pages that hold product information.

Firstly we will add a class that's name is Product.cs.(Add New Item -> Class).This file contains information about the products.Let Visual Studio create the App_Code folder for Product.cs.

Define a constructor for the Product class  which takes all of its member variables as parameter.

Product class should be like below.


public class Product
{
    public int ProductID;
    public string Type;
    public string Vendor;
    public string Model;
    public string ImageUrl;

    public int globalvar;
    public  static ArrayList proCopy;
    public static int id;
    public Product(int ProductID, string Type, string Vendor, string Model, string ImageUrl)
    {
        this.ProductID = ProductID;
        this.Type = Type;
        this.Vendor = Vendor;
        this.Model = Model;
        this.ImageUrl = ImageUrl;
        
    }
    public Product()
    { 
    
    }

    public void AddIndex(int n)
    {
        id = n;
    
    }

    public int getIndex()
    {
        return id;
    }
    public void copyArray(System.Collections.ArrayList array)
    {
        proCopy = array;
        
    }
    public System.Collections.ArrayList getArray()
    {
        return proCopy;
    }
   
}
Design Default.aspx
In Default.aspx page check if the user logged in or not.We use cookie check.The cookie should contain only first name and last name of the user. If no such cookie is defined, then should be displayed a message then link to the Login.aspx.
Fig. 1 – Default.aspx when user is not logged in.


When Login button is clicked, first name and last name will be written into a cookie named UserInfo for one month and will redirect to the left of Default.aspx.
 In Login.aspx.cs click method.

 protected void btnLogin_Click(object sender, EventArgs e)
    {
        //Create new cookie
        HttpCookie cookie = new HttpCookie("UserInfo");
        
        // add name and lastname to the cookie
        cookie["UserInfo"] = txtFirstName.Text+" "+txtLastName.Text;
        if (txtFirstName.Text == null || txtLastName.Text == null)
        {
            //lblLogin.Text = "Please specify valid Name and LastName";
        }
        else
        {
          //Define cookie for one month

            cookie.Expires = DateTime.Now.AddDays(30);
            Response.Cookies.Add(cookie);

            Response.Redirect("Default.aspx");
        }
    }
In Default.aspx.cs file we get cookie data with Request.Cookies["UserInfo"]. If cookie is null, redirect to login page.If cookie store data of user info, allow the see product information.

 protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["UserInfo"];




        if (cookie == null )  // if no cookies
        {
            lblExistingCookie.Text = "You are not login  Please login ";
            hyperlink1.Visible = true;
            lnkBtnCart. Visible = false;
            btnLogout.Visible = false;
            linkSamC2.Visible = false;
            linkSamE2.Visible = false;
            linkToshiba.Visible = false;
            linkLenova.Visible = false;
            linkAcer.Visible = false;
            lblHyper.Visible = false;

        }
        else
        {
            
            //  lblExistingCookie.Visible = true;
            hyperlink1.Visible = false;
            lblExistingCookie.Text = "Welcome " + cookie["UserInfo"] + "!";
            lnkBtnCart. Visible = true;
            btnLogout.Visible = true;
            linkSamC2.Visible = true;
            linkSamE2.Visible = true;
            linkToshiba.Visible = true;
            linkLenova.Visible = true;
            linkAcer.Visible = true;
            lblHyper.Visible = true;

        }

        if (!Page.IsPostBack) // first load create five product
        {


            //create Product objects
            
            Product part1 = new Product(1,"Phone","Samsung","C270","samsung.jpg");
            Product part2 = new Product(2,"Phone","Samsung","E250i","samsungE2.jpg");
            Product part3 = new Product(3,"Laptop","Toshiba","L500","toshiba.jpg");
            Product part4 = new Product(4,"Laptop","Lenova","G550","lenova.jpg");
            Product part5 = new Product(5,"Netbook","Acer","FO200","acer.jpg");
        
            //Add objects to the session state

            Session["Product1"] = part1; 
            Session["Product2"] = part2;
            Session["Product3"] = part3;
            Session["Product4"] = part4;
            Session["Product5"] = part5;
        }
        if(btnLogout.Visible == true)
        {
            linkSamC2.Visible = true;
            linkSamE2.Visible = true;
            linkToshiba.Visible = true;
            linkLenova.Visible = true;
            linkAcer.Visible = true;
            lblHyper.Visible = true;

           
        }
    }
Default.aspx file when user entered with name and last name
We Design Login.aspx and Default.aspx page. Then we continue to design ProductInfo.aspx page that display information of product.Then design Cart.aspx. It will done for display all products that are added to shopping cart. See You..