Total Pageviews

Tuesday 9 December 2014

Fill the filter area in online store from db

Fill the checkboxlist in the filter area:

IMAGE
----------


HTML
-------
<tr>
         <td>
                                <asp:CheckBoxList ID="brand_cbl" runat="server">
                                </asp:CheckBoxList>
          </td>
</tr>


CODE
--------
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

string con = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

public void show_filter()
    {
        SqlConnection items_con = new SqlConnection(con);

        try
        {
            items_con.Open();
//Call the stored procedure
            SqlCommand cmd = new SqlCommand("select_filter_brand_sp", items_con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Text = dr["brand"].ToString();
                brand_cbl.Items.Add(li);

            }



        }
        catch (Exception ex)
        {
           Response.Write(ex);
        }
        finally
        {
            items_con.Close();
        }
 
    }


Stored procedure
--------------------
ALTER PROCEDURE select_filter_brand_sp

AS

begin

SELECT DISTINCT brand,
                          (SELECT     COUNT(brand) AS c_b
                            FROM          Items AS x
                            WHERE      (brand = items.brand)) AS No_of_products
FROM         Items
ORDER BY brand

end
RETURN

Tuesday 25 November 2014

Shopping Cart in asp.net

IMAGE
---------

HTML
--------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cart.aspx.cs" Inherits="Shopping_cart_cart" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <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" ondeletecommand="dg_DeleteCommand">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate >
        <asp:Button ID="delete_btn" runat="server" Text="Delete" CommandName ="Delete"/>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    <br>
    <br>
    Total:
    <asp:Label ID="lblTotal" runat="server" />
    </form>
</body>
</html>


Code-Behind
-----------------
using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Shopping_cart_cart : System.Web.UI.Page
{
    DataTable objDT;
    DataRow objDR;


    public void Page_Load(object s, EventArgs e)
    {
        if (!IsPostBack)
        {
            makecart();
        }
    }
    public void makecart()
    {
        objDT = new DataTable("Cart");
        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 AddToCart(object s, EventArgs e)
    {


        objDT = (DataTable)Session["Cart"];
        object Product = ddlProducts.SelectedItem.Text;
        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();



        bool blnMatch = false;


        foreach (DataRow dr in objDT.Rows)
        {
            if ((objDR["Product"] == Product))
            {
                objDR["Quantity"] = txtQuantity.Text;
                blnMatch = true;
                break;
            }
        }

        if (!blnMatch)
        {
            objDR = objDT.NewRow();
            objDR["Quantity"] = Int32.Parse(txtQuantity.Text);
            objDR["Product"] = ddlProducts.SelectedItem.Text;
            objDR["Cost"] = Decimal.Parse(ddlProducts.SelectedItem.Value);
            objDT.Rows.Add(objDR);
        }

        lblTotal.Text = "RS " + GetItemTotal();

    }

    Decimal GetItemTotal()
    {
        int intCounter;
        Decimal decRunningTotal = 0;
        for (intCounter = 0; (intCounter <= (objDT.Rows.Count - 1)); intCounter++)
        {

            objDR = objDT.Rows[intCounter];
            decRunningTotal += decimal.Parse(objDR["Cost"].ToString()) * decimal.Parse(objDR["Quantity"].ToString());
        }
        return decRunningTotal;
    }



    protected void dg_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
        objDT = (DataTable)Session["Cart"];
        objDT.Rows[e.Item.ItemIndex].Delete();
        Session["Cart"] = objDT;
        dg.DataSource = objDT;
        dg.DataBind();
        lblTotal.Text = ("RS" + GetItemTotal());
    }
}