Monday 19 January 2015

Use sinple & light weight jquery editor in Web Application



HTML

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div id="sample">
            <script type="text/javascript" src="../nicEdit.js"></script>
            <script type="text/javascript">
                bkLib.onDomLoaded(function () { nicEditors.allTextAreas() });
            </script>
            <textarea id="txtmain" runat="server" name="area3" style="width: 300px; height: 100px;"></textarea>
        </div>
        <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" />
    </form>
</body>
</html>



C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        string a;
        a = txtmain.InnerText;
        Response.Write(a);
    }
}


Sunday 18 January 2015

Insert Into Sql Database


Make Class File Name Code.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

/// <summary>
/// Summary description for Code
/// </summary>
public class Code
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["yogesh"].ConnectionString);

    public void GETCONNECTION()
    {
        con.Open();
    }

    public void CLOSECONNECTION()
    {
        con.Close();
    }

    public int INSERTDATA(SqlParameter[] pr, string query)
    {
        GETCONNECTION();
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.AddRange(pr);
        int i = cmd.ExecuteNonQuery();
        CLOSECONNECTION();
        return i;     
    }

    public DataTable SELECTDATA(SqlParameter[] pr, string query)
    {
        GETCONNECTION();
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        cmd.Parameters.AddRange(pr);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        CLOSECONNECTION();
        return dt;
    }
}


Back Code To Insert Data

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    Code objcode = new Code();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            binddata();
        }
    }

    public void binddata()
    {
        string content = string.Empty;
        SqlParameter[] pr = new SqlParameter[0];
        string query = "select * from employee";
        DataTable dt = new DataTable();
        dt = objcode.SELECTDATA(pr, query);
        ddlbind.DataSource = dt;
        ddlbind.DataTextField = "name";
        ddlbind.DataValueField = "id";
        ddlbind.DataBind();
    }

    protected void btnsubmit_Click(object sender, EventArgs e)
    {       
        int i = 0;
        SqlParameter[] pr = new SqlParameter[2];
        pr[0] = new SqlParameter();
        pr[0].ParameterName = "@name";
        pr[0].Value = txtname.Text;
        pr[1] = new SqlParameter();
        pr[1].ParameterName = "@address";
        pr[1].Value = txtaddress.Text;
        string query = "insert into employee(name,address) values(@name,@address)";
        i = objcode.INSERTDATA(pr, query);
        if (i > 0)
        {
            Response.Redirect(Request.RawUrl, false);
        }        
    }
    protected void ddlbind_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlParameter[] pr = new SqlParameter[1];
        pr[0] = new SqlParameter();
        pr[0].ParameterName = "@id";
        pr[0].Value = Convert.ToInt32(ddlbind.SelectedItem.Value);
        string query = "select * from employee where id=@id";
        DataTable dt = new DataTable();
        dt = objcode.SELECTDATA(pr, query);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

Friday 16 January 2015

Dynamic Menu And Sub Menu From Database


Css
<div>
<ul id="nav" runat="server"></ul> 
</div>


Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.Configuration;

public partial class home : System.Web.UI.Page
{
    Code objcode = new Code();

    protected void Page_Load(object sender, EventArgs e)
    {
        menu();
    }

    public void menu()
    {
        string content = string.Empty;
        OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["datacon"].ConnectionString);
        con.Open();
        OleDbCommand cmd = new OleDbCommand("select * from menu", con);
        OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                string st="#";
                content += "<li><a href='" + st + "'>" + dr["menu_name"] + "</a>" + submenu(dr["menu_name"].ToString()) + "</li>";               
            }
            nav.InnerHtml = content;
        }
    }

    public string submenu(string str)
    {
        string content = string.Empty;
        content += "<ul>";
        if (str.Trim() == "About Us")
        {
            content += bindmainmenu();
        }

        if (str.Trim() == "Contact Us")
        {
            content += bindmainmenu2();
        } 
        content += "</ul>";
        return content;
    }


    public string bindmainmenu()
    {
        string content = string.Empty;
        OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["datacon"].ConnectionString);
        con.Open();
        OleDbCommand cmd = new OleDbCommand("select * from sub_menu", con);
        OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                content += "<li><a href='" + dr["menu_url"] + "?pid=" + dr["id"] + "'" + ">" + dr["menu_name"] + "</a></li>";
            }
        }        
        return content;
    }

    public string bindmainmenu2()
    {
        string content = string.Empty;
        OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["datacon"].ConnectionString);
        con.Open();
        OleDbCommand cmd = new OleDbCommand("select * from sub_menu2", con);
        OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                content += "<li><a href='" + dr["menu_url"] + "?pid=" + dr["id"] + "'" + ">" + dr["menu_name"] + "</a></li>";
            }
        }
        return content;
    }
}

Wednesday 14 January 2015

show limited text in label


<asp:Label ID="pro_typeLabel" runat="server" CssClass="ShortDesc" Text='<%# Eval("Model").ToString().Substring(0,Math.Min(8,Eval("Model").ToString().Length)) %>' />..                                            


<span style="font-weight: 300;">
                                        <asp:Label ID="briefLabel" runat="server" Text='<%# Eval("brief").ToString().Substring(0,Math.Min(78,Eval("brief").ToString().Length)) %>'></asp:Label>...</span>

Wednesday 26 November 2014

Asphelp: How To Show multiple Image in ul & li using database


  try
        {
            SqlConnection con = new SqlConnection(strcon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from main_menu", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    content += "<li><a href='" + dr["menu_url"] + "?pid=" + dr["id"] + "'" + ">" + dr["menu_name"] + "</a></li>";
                }
                nav.InnerHtml = content;
            }
            con.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

Asphelp: How To Create Dynamic Div Using Sql Server As Database


 for (int i = 0; i < dt.Rows.Count; i++)
            {                
                int pro_id = Convert.ToInt32(dt.Rows[i]["pro_id"].ToString());
                pro_name = dt.Rows[i]["pro_name"].ToString();
                pro_amount = dt.Rows[i]["pro_amount"].ToString();
                pro_image = dt.Rows[i]["pro_image"].ToString();
                string inputString = "Images/product/" + pro_image;
                HtmlGenericControl li = new HtmlGenericControl();
                li.TagName = "li";
                li.InnerHtml = "<a href=" + "view_detail.aspx?pid=" + pro_id + ">" + "<img src='" + inputString + "' width='" + "180" + "' height='" + "170" + "' runat='" + "Server" + "'s/>" + "</a><br />" + pro_name + "<br /> Rs. " + pro_amount + " PER KG";
                dynlatest.Controls.Add(li);               
            }

Friday 26 September 2014

Asphelp: Bind asp repeater with database

Download Source
In this article you will learn how to bind Sql server Table and show the record of table in asp repeater. Sql server used for database and visual studio for design and developing

Create Table
CREATE TABLE [dbo].[employee] (
    [Id]   INT          IDENTITY (1, 1) NOT NULL,
    [name] VARCHAR (50) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

HTML CODE
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        #tbl1 {
            border: 3px #fff solid;
            padding: 10px;
            margin: 5px;          
            width:400px;  
            box-shadow: 3px 5px 15px #b6b6b6;
        }

            #tbl1:hover {
                border: #ffd926 3px solid;
                padding: 10px;
                margin: 5px;                
                box-shadow: 3px 5px 15px #b6b6b6;
            }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div align="center">
            <asp:Repeater ID="rptbind" runat="server">
                <ItemTemplate>
                    <table id="tbl1" style="background-color: white">
                        <tr>
                            <td>Employee ID : <%#Eval("id") %>
                            </td>                           
                            <td align="left">Employee Name : <%#Eval("name") %>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>
</html>

C# CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Datalist
{
    public partial class Home : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=Servername;Initial Catalog=Databasename;Integrated Security=True");

        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from employee", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            rptbind.DataSource = ds.Tables[0];
            rptbind.DataBind();
            con.Close();
        }
    }
}


Output Preview

bind repeater | dotnetasphelp


Download Source

Wednesday 24 September 2014

Asphelp: Upload Image Add Watermark Then Download

Download Source
In this article you will learn how to upload image and show preview.there is a textbox which is used to add a watermark text in image.there is button which is used to download the watermark image. .mdf database is create in visual studio

HTML CODE
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="button.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <div align="center" style="font-size: 20px;">
            Upload Image And Add Watermark Text
        </div>
        <br />
        <div>
            <table style="margin-left: 350px;">
                <tr>
                    <td>
                        <div style="margin-top:-140px;">
                            <asp:FileUpload ID="fupload" runat="server" />
                            <br />
                            <input id="btnupload" runat="server" type="submit" value="Upload" class="shiny-button" onserverclick="btnupload_ServerClick" />
                        </div>
                    </td>
                    <td>
                        <asp:Image ID="img1" runat="server" Width="300px" Height="180px" />
                        <br />
                        <br />
                        <input type="text" id="txtwatermark" runat="server" class="textbox" />
                        <br />
                        <br />
                        <input type="submit" id="btndownload" runat="server" value="Download Image" class="shiny-button" onserverclick="btndownload_ServerClick" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>


C# CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;

namespace WatermarkImage
{
    public partial class Home : System.Web.UI.Page
    {
        public static string filename;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnupload_ServerClick(object sender, EventArgs e)
        {
            if (fupload.HasFile)    //uploading file
            {
                filename = Path.GetFileName(fupload.PostedFile.FileName);
                fupload.SaveAs(Server.MapPath("~/Upload/" + filename));   //save image in upload folder    
                img1.ImageUrl = "~/Upload/" + filename;
            }            
        }

        protected void btndownload_ServerClick(object sender, EventArgs e)
        {
            string fileName;
            int num;
            Random obj = new Random();      //random number genrate
            num = obj.Next(0, 1000000);
            fileName = (num + ".jpg");
            System.Drawing.Image upImage = System.Drawing.Image.FromFile(Server.MapPath(img1.ImageUrl));  
            using (System.Drawing.Graphics g = Graphics.FromImage(upImage))
            {
                int opacity = 120;              //show text to image
                SolidBrush brush = new SolidBrush(Color.FromArgb(opacity, Color.Black));
                Font font = new Font("Calibri", 50);
                g.DrawString(txtwatermark.Value, font, brush, new PointF(8, 15));
                upImage.Save(Path.Combine(Server.MapPath("~\\Download\\" + fileName)));
                g.Dispose();
                brush.Dispose();
                font.Dispose();
            }
            upImage.Dispose();           
            Response.Clear();       //download image from download folder
            Response.ContentType = "Application/jpg";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + "");
            Response.TransmitFile("~\\Download\\" + fileName);
            Response.Flush();
        }
    }
}

Output Preview

watermark image | dotnetasphelp


Download Source

Tuesday 23 September 2014

Asphelp: Cookies Add To Cart Asp.net C#

Download Source
In previous post we show you how to create add to cart using session. In this article you will learn how to create add to cart using cookies. accept all the details from user before add to cart in not necessary.

Create Table Product

table product | dotnetasphelp


HTML Homepage
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="assets/css/styles.css" rel="stylesheet" />
    <link href="assets/css/button.css" rel="stylesheet" />
    <style>
        #tbl1 {
            border: 3px #fff solid;
            padding: 10px;
            margin: 5px;
            min-height: 300px;
            min-width: 230px;
            box-shadow: 3px 5px 15px #b6b6b6;
        }


            #tbl1:hover {
                border: #ffd926 3px solid;
                padding: 10px;
                margin: 5px;
                min-height: 300px;
                min-width: 230px;
                box-shadow: 3px 5px 15px #b6b6b6;
            }


        #output {
            float: right;
            border: groove;
            width: 315px;
            margin: 11px;
            box-shadow: 3px 5px 15px #b6b6b6;
        }
    </style>
</head>
<body style="background-color: #F7F5EE">
    <form id="form1" runat="server">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <div>
                    <div style="float: right; margin-right: 50px;">
                        <input type="submit" runat="server" id="btncart" value="View Cart" class="shiny-button" onserverclick="btncart_ServerClick" />
                        &nbsp;&nbsp;<asp:Label ID="lblcount" runat="server" Font-Size="Large"></asp:Label>
                    </div>
                    <table>
                        <tr>
                            <td>
                                <img id="img2" runat="server" width="300" src="~/image/shoplogo.png" /></td>
                        </tr>
                    </table>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
        <div style="float: left; margin-left: 150px;">
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <br />
                    <asp:DataList ID="Datalist1" runat="server" RepeatDirection="Horizontal" RepeatColumns="4" OnItemCommand="Datalist1_ItemCommand">
                        <ItemTemplate>
                            <table id="tbl1" style="background-color: white">
                                <tr style="height: 10%;">
                                    <td style="text-align: center;" colspan="2"><b>
                                        <asp:Label ID="lblname" Font-Size="22px" runat="server" Text='<%#Eval("ProductName") %>'></asp:Label></b><hr />
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" style="height: 80%;">
                                        <img id="img1" class="img1" runat="server" src='<%#Eval("ProductImage") %>' /></td>
                                    <td></td>
                                </tr>
                                <tr style="height: 10%;">
                                    <td>Rs:<asp:Label ID="lblprice" runat="server" Text='<%#Eval("Amount") %>'></asp:Label></td>
                                    <td>
                                        <input type="hidden" id="hiddenid" value='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' runat="server" />
                                        <input type="hidden" id="hiddenname" value='<%# DataBinder.Eval(Container.DataItem, "ProductName") %>' runat="server" />
                                        <input type="hidden" id="hiddenamount" value='<%# DataBinder.Eval(Container.DataItem, "Amount") %>' runat="server" />
                                        <input type="hidden" id="hiddenimg" value='<%# DataBinder.Eval(Container.DataItem, "ProductImage") %>' runat="server" />
                                        <asp:Button ID="btnwishlist" runat="server" Text="Add To cart" CommandName="Add" CssClass="shiny-button" /></td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:DataList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>


C# Hompage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Collections;

namespace CookiesAddToCart
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Binddata();
                count();
            }          
        }

        private void Binddata()
        {
            SqlConnection con = new SqlConnection("Data Source=Servername;Initial Catalog=databasename;Integrated Security=True");
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from Product", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Product");
            Datalist1.DataSource = ds;
            Datalist1.DataBind();
        }

        public void count()
        {
            if (Request.Cookies["ShoppingCart"] != null)
            {
                var value = Request.Cookies["ShoppingCart"].Values.Count;
                lblcount.Text = value.ToString();
            }
            else
            {
                var value = "0";
                lblcount.Text = value.ToString();
            }
        }

        protected void Datalist1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                string id = ((HtmlInputHidden)e.Item.FindControl("hiddenid")).Value;
                string name = ((HtmlInputHidden)e.Item.FindControl("hiddenname")).Value;
                string amount = ((HtmlInputHidden)e.Item.FindControl("hiddenamount")).Value;
                string image = ((HtmlInputHidden)e.Item.FindControl("hiddenimg")).Value;

                HttpCookie c = null;
                if (HttpContext.Current.Request.Cookies["ShoppingCart"] == null)
                    c = new HttpCookie("ShoppingCart");
                else
                    c = HttpContext.Current.Request.Cookies["ShoppingCart"];
                string itemdetails;
               itemdetails = id + "|" + name + "|" + amount + "|" + image;
                c.Values[id] = itemdetails;
                c.Expires = DateTime.Now.AddDays(2);
                Response.Cookies.Add(c);
                Response.Redirect(Request.RawUrl);
            }           
        }

        protected void btncart_ServerClick(object sender, EventArgs e)
        {
            HttpCookie myCookie = new HttpCookie("ShoppingCart");
            myCookie = Request.Cookies["ShoppingCart"];
            if (myCookie != null)
                Response.Redirect("Cart.aspx");
            else
                Response.Cookies["ShoppingCart"].Expires = DateTime.Now.AddDays(-1);
        }
    }
}

Cart Page HTML
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="assets/css/button.css" rel="stylesheet" />
    <style>
        #output {            
            border: groove;
            width: 480px;
            box-shadow: 3px 5px 15px #b6b6b6;
            border-radius: 13px 13px 13px 13px;
            background-color: white;        
            margin:0px auto;        
            margin-top:65px;                 
        }
    </style>
</head>
<body style="background-color: #F7F5EE">
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server"></asp:ScriptManager>
        <div id="output">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <table>
                        <tr>
                            <td>
                                <img src="image/cart.png" width="45" height="45" /></td>
                            <td>
                                <asp:Label ID="lblcount" runat="server" Font-Size="X-Large"></asp:Label></td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
            <hr />
            <div style="border-bottom: groove 1px; min-height: 220px;">
                <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                    <ContentTemplate>
                        <asp:Repeater ID="rptcart" runat="server" OnItemCommand="rptcart_ItemCommand">
                            <ItemTemplate>
                                <table style="width: 100%">
                                    <tr>
                                        <td style="width: 20%">
                                            <asp:Label ID="lblid" runat="server" Text='<%#Eval("ProductID") %>'></asp:Label></td>
                                        <td style="width: 20%">
                                            <img id="img1" class="img1" runat="server" width="40" height="40" src='<%#Eval("ProductImage") %>' /></td>
                                        <td style="width: 20%">
                                            <asp:Label ID="lblqty" runat="server" Text='<%#Eval("ProductName") %>'></asp:Label></td>
                                        <td style="width: 20%">
                                            <asp:Label ID="lblprice" runat="server" Text='<%#Eval("Amount") %>'></asp:Label></td>
                                        <td style="width: 20%">
                                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "quantity") %>' Columns="3"></asp:TextBox>
                                            <asp:LinkButton ID="saveqty" runat="server" Font-Size="12px" CommandName="countqty" OnClick="saveqty_Click">Save</asp:LinkButton>
                                        </td>
                                        <td>
                                            <input type="hidden" id="hiddenid" value='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' runat="server" />
                                            <input type="hidden" id="hiddenqty" value='<%# DataBinder.Eval(Container.DataItem, "quantity") %>' runat="server" />
                                            <input type="hidden" id="hiddenamount" value='<%# DataBinder.Eval(Container.DataItem, "Amount") %>' runat="server" />
                                            <asp:ImageButton CommandName="delete" ID="btndelete" runat="server" ImageUrl="~/image/close.png" CommandArgument='<%#Eval("ProductID") %>' Width="15" Height="15" />
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:Repeater>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
            <div style="height: 125px;">
                <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                    <ContentTemplate>
                        <table width="98%">
                            <tr>
                                <td style="text-align: center; width: 50%">
                                    <br />
                                    <b style="font-size: 15px">Estimated Total : </b>
                                    <b>
                                        <asp:Label ID="lbltotalamt" runat="server" Font-Size="15px"></asp:Label></b></td>
                            </tr>
                            <tr>
                                <td style="text-align: center; width: 50%;">
                                    <br />
                                    <input type="submit" id="btnempty" runat="server" style="font-size: 15px" value="Clear Cart" class="shiny-button" /></td>
                                <td style="text-align: center; width: 50%">
                                    <br />
                                    <input type="submit" id="btnproceed" runat="server" style="font-size: 15px" value="Place Order" class="shiny-button" /></td>
                            </tr>
                        </table>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </div>
    </form>
</body>
</html>

Cart Page C#
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CookiesAddToCart
{
    public partial class Cart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillCartFromCookies();
            }
        }

        private void FillCartFromCookies()
        {
            if (Request.Cookies["ShoppingCart"] != null)
            {
                HttpCookie c = HttpContext.Current.Request.Cookies["ShoppingCart"];
                ArrayList items = new ArrayList();

                for (int i = 0; i < c.Values.Count; i++)
                {
                   string[] vals = c.Values[i].Split('|');
                    shopdetail item = new shopdetail();
                    item.ProductID = int.Parse(vals[0]);
                    item.ProductName = vals[1];
                    item.Amount = decimal.Parse(vals[2]);
                    item.ProductImage = vals[3];
                    item.Quantity = 1;
                    items.Add(item);
                }

                rptcart.DataSource = items;
                rptcart.DataBind();                           
                saveqty_Click(null, null);
                
            }
            else
            {
                Response.Redirect("Home.aspx");
            }
        }

        protected void rptcart_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                string id = ((HtmlInputHidden)e.Item.FindControl("hiddenid")).Value;
                HttpCookie c = HttpContext.Current.Request.Cookies["ShoppingCart"];
                c.Values.Remove(id);
                Response.Cookies.Add(c);
                FillCartFromCookies();
                if (Request.Cookies["ShoppingCart"] != null)
                {
                    var value = Request.Cookies["ShoppingCart"].Value;
                    if (value == "")
                    {
                        Response.Cookies["ShoppingCart"].Expires = DateTime.Now.AddDays(-1);
                    }                   
                }                
            }
        }

        protected void saveqty_Click(object sender, EventArgs e)
        {
            decimal total = 0;
            try
            {
                foreach (RepeaterItem rpt in rptcart.Items)
                {
                   
                    if (rpt.ItemType == ListItemType.Item || rpt.ItemType == ListItemType.AlternatingItem)
                    {
                        TextBox t = (TextBox)rpt.FindControl("TextBox2");
                        Label l = (Label)rpt.FindControl("lblprice");
                        int quantity = int.Parse(t.Text);
                        decimal unitprice = Decimal.Parse(l.Text);
                        total = total + (unitprice * quantity);
                    }
                }
            }
            catch
            {
            }

            lbltotalamt.Text = total.ToString();
        }
    }
}


Output Preview

add to cart cookies | dotnetasphelp


Download Source

Friday 19 September 2014

Asphelp: Cascade Dropdownlist Using C#

Download Source

In this article you will learn how to make cascading dropdownlist.

Create Table Automobile

table automobile | dotnetasphelp


Create Table Brand

table brand | dotnetasphelp


Create Table Model

table model | dotnetasphelp


HTML CODE
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        .textbox {
            background: white;
            border: 1px double #DDD;
            border-radius: 5px;
            box-shadow: 0 0 5px #333;
            color: #666;
            outline: none;
            height: 25px;
            width: 230px;
        }
    </style>

</head>
<body style="background-color: #F5FAFA">
    <form id="form1" runat="server">
        <div align="center">
            <asp:ScriptManager runat="server"></asp:ScriptManager>
            <asp:UpdatePanel runat="server">
                <ContentTemplate>
                    <table>
                        <tr>
                            <td align="center" style="font-size: large">Cascading Dropdown list<br />
                                <br />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:DropDownList ID="ddlautomobile" runat="server" CssClass="textbox" AutoPostBack="true" OnSelectedIndexChanged="ddlautomobile_SelectedIndexChanged" Font-Size="Medium"></asp:DropDownList><br />
                                <br />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:DropDownList ID="ddlbrand" runat="server" CssClass="textbox" OnSelectedIndexChanged="ddlbrand_SelectedIndexChanged" AutoPostBack="true" Font-Size="Medium"></asp:DropDownList><br />
                                <br />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:DropDownList ID="ddlmodel" runat="server" CssClass="textbox" Font-Size="Medium"></asp:DropDownList></td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>


C# CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace CascadingDropdownlist
{
    public partial class Home : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=Servername;Initial Catalog=Databasename;Integrated Security=True");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindautomobile();
                ddlbrand.Items.Insert(0, new ListItem("--------------   Select   --------------", "0"));
                ddlmodel.Items.Insert(0, new ListItem("--------------   Select   --------------", "0"));
            }
        }

        public void bindautomobile()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from automobile", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            ddlautomobile.DataSource = ds.Tables[0];
            ddlautomobile.DataValueField = "autoid";
            ddlautomobile.DataTextField = "autoname";
            ddlautomobile.DataBind();
            ddlautomobile.Items.Insert(0, new ListItem("--------------   Select   --------------", "0"));
        }     

        protected void ddlautomobile_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from brand where autoid='" + ddlautomobile.SelectedItem.Value + "'", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            ddlbrand.DataSource = ds.Tables[0];
            ddlbrand.DataValueField = "brandid";
            ddlbrand.DataTextField = "brandname";
            ddlbrand.DataBind();
            ddlbrand.Items.Insert(0, new ListItem("--------------   Select   --------------", "0"));
        }

        protected void ddlbrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from model where brandid='" + ddlbrand.SelectedItem.Value + "'", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            ddlmodel.DataSource = ds.Tables[0];
            ddlmodel.DataValueField = "modelid";
            ddlmodel.DataTextField = "modelname";
            ddlmodel.DataBind();
            ddlmodel.Items.Insert(0, new ListItem("--------------   Select   --------------", "0"));
        }
    }
}


Output Preview

 cascade dropdownlist | dotnetasphelp


Download Source