Search This Blog

Thursday, March 15, 2012

Upload Product Information



Aspx File Source Code
<table class="style1">
                                                <tr>
                                                    <td class="style2">
                                                        Product Category
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:DropDownList ID="Ddl_Category" runat="server" AutoPostBack="True" ForeColor="#009933"
                                                            OnSelectedIndexChanged="Ddl_Category_SelectedIndexChanged">
                                                        </asp:DropDownList>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product SubCategory
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:DropDownList ID="Ddl_SubCategory" runat="server" AutoPostBack="True" ForeColor="#009933">
                                                        </asp:DropDownList>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Name
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductName" runat="server" Font-Bold="True" ForeColor="#009933"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Price
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductPrice" runat="server" Font-Bold="True" ForeColor="#009933"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Image
                                                    </td>
                                                    <td class="style3">
                                                        &nbsp;
                                                    </td>
                                                    <td>
                                                        <asp:FileUpload ID="FileUpload1" runat="server" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Color
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:DropDownList ID="Ddl_Color" runat="server" AutoPostBack="True" ForeColor="#009900">
                                                            <asp:ListItem Value="0">--select color--</asp:ListItem>
                                                            <asp:ListItem Value="White">White</asp:ListItem>
                                                            <asp:ListItem>Red</asp:ListItem>
                                                            <asp:ListItem>Green</asp:ListItem>
                                                            <asp:ListItem>Yellow</asp:ListItem>
                                                            <asp:ListItem>Black</asp:ListItem>
                                                            <asp:ListItem>Blue</asp:ListItem>
                                                            <asp:ListItem>Gray</asp:ListItem>
                                                            <asp:ListItem>Orange</asp:ListItem>
                                                            <asp:ListItem>Pink</asp:ListItem>
                                                        </asp:DropDownList>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Features
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductFeatures" runat="server" Font-Bold="True" ForeColor="#009933"
                                                            TextMode="MultiLine" Height="46px" Width="250px"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Specification
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductSpecification" runat="server" Font-Bold="True" ForeColor="#009933"
                                                            TextMode="MultiLine" Height="45px" Width="250px"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Description
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductDescription" runat="server" Font-Bold="True" ForeColor="#009933"
                                                            TextMode="MultiLine" Height="46px" Width="250px"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style5">
                                                        Product Warranty
                                                    </td>
                                                    <td class="style6">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td class="style7">
                                                        <asp:TextBox ID="Txt_ProductWarranty" runat="server" Font-Bold="True" ForeColor="#009933"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Available Quantity
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductQuantity" runat="server" Font-Bold="True" ForeColor="#009933"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        &nbsp;
                                                    </td>
                                                    <td class="style3">
                                                        &nbsp;
                                                    </td>
                                                    <td>
                                                        <asp:Button ID="Btn_Upload" runat="server" Text="Upload" OnClick="Btn_Upload_Click" />
                                                    </td>
                                                </tr>
                                            </table>


In aspx .cs File the Code To upload all the Information of Product

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

public partial class ProductUpload : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=TOPS21;Initial Catalog=MyExample;Integrated Security=True;Pooling=False");
    static string path = "";
    static string filename = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
        if (!IsPostBack)
        {
            Fill_DdlCategory();
        }
    }
    public void Fill_DdlCategory()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tbl_category",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Ddl_Category.DataSource = ds;
        Ddl_Category.DataValueField = "category_id";
        Ddl_Category.DataTextField = "category_name";
        Ddl_Category.DataBind();
        Ddl_Category.Items.Insert(0,new ListItem("--select category--","0"));
        Ddl_SubCategory.Items.Insert(0, new ListItem("--select subcategory--", "0"));
    }
    protected void Ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tbl_subcategory where category_id='"+Ddl_Category.SelectedValue+"'", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Ddl_SubCategory.DataSource = ds;
        Ddl_SubCategory.DataValueField = "subcategory_id";
        Ddl_SubCategory.DataTextField = "subcategory_name";
        Ddl_SubCategory.DataBind();
        Ddl_SubCategory.Items.Insert(0, new ListItem("--select subcategory--", "0"));   
    }
    protected void Btn_Upload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            filename = FileUpload1.FileName;
            path = "~//images//" + filename;
            FileUpload1.SaveAs(Server.MapPath(path));
            SqlCommand cmd = new SqlCommand("insert into tbl_product values('" + Ddl_Category.SelectedValue + "','" + Ddl_SubCategory.SelectedValue + "','" + Txt_ProductName.Text + "','" + Txt_ProductPrice.Text + "','" + path + "','" + Ddl_Color.SelectedValue + "','" + Txt_ProductFeatures.Text + "','" + Txt_ProductSpecification.Text + "','" + Txt_ProductDescription.Text + "','" + Txt_ProductWarranty.Text + "','" + Txt_ProductQuantity.Text + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            lblmsg.Text = "Product Uploaded...";
        }
        else
        {
            lblmsg.ForeColor = System.Drawing.Color.Red;
            lblmsg.Font.Bold.Equals(true);
            lblmsg.Text = "Please select the Product Image First.";
        }
    }
}

Output is

For that Make table in the database


1 comment: