Sunday, April 1, 2012

Product Catelog App class in asp.net c# -6


add_product.aspx

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="add_product.aspx.cs" Inherits="add_product" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <table>
        <tr>
            <td width="63%" align="right" valign="top">
                <table width="90%" border="0" align="right" cellpadding="0" cellspacing="0" style="padding: 40px 0 0 0;
                    margin-top: 10px;">
                    <tr>
                        <td width="90%" align="right" valign="top">
                            <table width="90%" border="0" align="right" cellpadding="0" cellspacing="0" style="margin-bottom: 25px;">
                                <tr>
                                    <td align="left">
                                        <table width="98%" align="left" cellpadding="0" cellspacing="0" style="margin: 25px 0 0 0;">
                                            <tr>
                                                <td align="left" valign="top" class="style13">
                                                    ADD PRODUCT
                                                </td>
                                                <td width="198" height="35" align="right" valign="middle" class="style4">
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="left">
                                        <table width="535" border="0" align="center" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td height="10">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td width="520" align="left" valign="top">
                                                    <table width="90%" border="0" cellspacing="5" cellpadding="5">
                                                        <tr>
                                                            <td width="225" align="left" valign="middle" class="fields">
                                                                SELECT CATEGORY
                                                            </td>
                                                            <td width="147" align="left" valign="middle">
                                                                <asp:DropDownList ID="drpcategory" runat="server" Height="16px"
                                                                    style="width: 77px" Width="130px">
                                                                </asp:DropDownList>
                                                            </td>
                                                            <td width="142">
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="drpcategory"
                                                                    ErrorMessage="*" ValidationGroup="ok"></asp:RequiredFieldValidator>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td width="225" align="left" valign="middle" class="fields">
                                                                PRODUCT NAME:
                                                            </td>
                                                            <td width="147" align="left" valign="middle">
                                                                <asp:TextBox ID="txtproduct" runat="server" Width="147" class="fields"></asp:TextBox>
                                                            </td>
                                                            <td width="142">
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtcategory"
                                                                    ErrorMessage="*" ValidationGroup="ok"></asp:RequiredFieldValidator>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td width="225" align="left" valign="middle" class="fields">
                                                                PRODUCT CODE
                                                            </td>
                                                            <td width="147" align="left" valign="middle">
                                                                <asp:TextBox ID="txtproductcode" runat="server" Width="147" class="fields"></asp:TextBox>
                                                            </td>
                                                            <td width="142">
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtproductcode"
                                                                    ErrorMessage="*" ValidationGroup="ok"></asp:RequiredFieldValidator>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td width="225" align="left" valign="middle" class="fields">
                                                                PRODUCT DETAIL
                                                            </td>
                                                            <td width="147" align="left" valign="middle">
                                                                <asp:TextBox TextMode="MultiLine" ID="txtdetail" runat="server" Width="169px" class="fields"
                                                                    Height="69px"></asp:TextBox>
                                                            </td>
                                                            <td width="142">
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtdetail"
                                                                    ErrorMessage="*" ValidationGroup="ok"></asp:RequiredFieldValidator>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td width="225" align="left" valign="middle" class="fields">
                                                                IMAGE
                                                            </td>
                                                            <td width="147" align="left" valign="middle">
                                                                <asp:FileUpload ID="fileupload" runat="server" />
                                                            </td>
                                                            <td width="142">
                                                              <asp:Image ID="imgcon" runat="server" BorderStyle="None" Visible="false" />
                        &nbsp;<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click"
                            Visible="false">Remove Image</asp:LinkButton></td>
                                                        </tr>
                                                        <tr>
                                                            <td width="225" align="left" valign="middle" class="fields">
                                                                &nbsp;
                                                            </td>
                                                            <td width="147" align="left" valign="middle">
                                                                &nbsp;
                                                            </td>
                                                            <td width="142">
                                                                &nbsp;
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td height="15" colspan="3" align="left" valign="middle" class="fields">
                                                                &nbsp;
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td colspan="3" align="center" valign="middle">
                                                                <table width="82%" border="0" cellspacing="0" cellpadding="0">
                                                                    <tr>
                                                                        <td align="left" valign="middle">
                                                                            &nbsp;
                                                                        </td>
                                                                        <td width="60">
                                                                            <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click"
                                                                                Style="height: 26px" />
                                                                        </td>
                                                                        <td align="right" valign="middle">
                                                                            &nbsp;
                                                                        </td>
                                                                    </tr>
                                                                </table>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td height="60" colspan="3" align="left" valign="middle">
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="left" valign="middle">
                                                                &nbsp;
                                                            </td>
                                                            <td>
                                                                &nbsp;
                                                            </td>
                                                            <td align="left" valign="middle">
                                                                <a style="border: none;" class="thickbox">&nbsp;</a>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <table align="center" border="0" cellpadding="0" cellspacing="0" width="535">
                                                        <tr>
                                                            <td height="10">
                                                                &nbsp;
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="left" valign="top" width="520">
                                                                <table border="0" cellpadding="0" cellspacing="0" width="90%">
                                                                    <tr>
                                                                        <td align="center" colspan="3" height="15" valign="middle">
                                                                            &nbsp;
                                                                        </td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td align="center" colspan="3" valign="middle">
                                                                            &nbsp;
                                                                        </td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td align="left" colspan="3" height="60" valign="middle">
                                                                        </td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td align="left" valign="middle">
                                                                            &nbsp;
                                                                        </td>
                                                                        <td>
                                                                            &nbsp;
                                                                        </td>
                                                                        <td align="left" valign="middle">
                                                                            &nbsp;
                                                                        </td>
                                                                    </tr>
                                                                </table>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                &nbsp;
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="left" valign="top">
                                                                &nbsp;
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="left" valign="top">
                                                    &nbsp;
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</asp:Content>


add_product.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
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 add_product : System.Web.UI.Page
{
    BALProductMaster bal = new BALProductMaster();
    DALProductMaster dal = new DALProductMaster();
    BALCategoryMaster catbal = new BALCategoryMaster();
    DALCategoryMaster catdal = new DALCategoryMaster();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            drpcategory.DataSource = catdal.SelectCategory();
            drpcategory.DataBind();
            drpcategory.SelectedItem.Text = "category";
            drpcategory.SelectedValue = "id";

            if (Request.QueryString["id"] != null)
            {
                int ProductID = int.Parse(Request.QueryString["id"].ToString());
                bal.ProductID = ProductID;
                DataTable dt = dal.select_ProductMasterByProductId(bal);
                if (dt.Rows.Count != 0)
                {
                    drpcategory.SelectedValue = dt.Rows[0]["cate_id"].ToString();
                    txtproduct.Text = dt.Rows[0]["ProductName"].ToString();
                    txtproductcode.Text = dt.Rows[0]["Productcode"].ToString();
                    txtdetail.Text = dt.Rows[0]["detail"].ToString();
                    imgcon.Visible = true;
                    imgcon.ImageUrl = "CreateThumbnail.aspx?image=upload/product/" + dt.Rows[0]["image"] + "&width=100";
                }
            }

        }
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        string strimage, path;
        strimage = fileupload.FileName;
        path = Server.MapPath("upload/product/");
        if (!fileupload.FileName.Equals(""))
        {
            Session["image"] = strimage;
        }
        else
        {

        }

        if (Request.QueryString["id"] != null)
        {
            bal.ProductName = txtproduct.Text.Trim().ToString().ToUpper();
            bal.ProductCode = txtproductcode.Text.Trim().ToString();
            bal.CategoryId = int.Parse(drpcategory.SelectedValue.ToString());
            bal.Image = Session["image"].ToString();
            bal.ProductID = int.Parse(Request.QueryString["id"].ToString());
            bal.Detail = txtdetail.Text.ToString();
            dal.updateR_ProductMaster(bal);
            Response.Redirect("view_product.aspx");
        }
        else
        {

            bal.ProductName = txtproduct.Text.Trim().ToString().ToUpper();
            bal.ProductCode = txtproductcode.Text.Trim().ToString();
            bal.CategoryId = int.Parse(drpcategory.SelectedValue.ToString());
            bal.Image = Session["image"].ToString();
            bal.Detail = txtdetail.Text.ToString();
            dal.insert_ProductMaster(bal);
            Response.Redirect("view_product.aspx");
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        bal.ProductID = int.Parse(Request.QueryString["id"].ToString());
        dal.Image_remove(bal);
    }
}