Friday, November 14, 2014

3 Tier Architecture with File Upload Controll

=============================
3 Tier Architecture
=============================
Designe - design file .aspx
=============================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="IUDFor.aspx.cs" Inherits="IUDFor" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <center>
   
    <div>
    <table>
    <tr>
        <td colspan="3" align="center"> EMPLOYEE FORM</td>
    </tr>
    <tr>
        <td>Name :</td>
        <td colspan="2"><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>City :</td>
        <td colspan="2"><asp:TextBox ID="txtCity" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Salary :</td>
        <td colspan="2"><asp:TextBox ID="txtSal" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>DOJ :</td>
        <td colspan="2"><asp:TextBox ID="txtDoj" runat="server"></asp:TextBox></td>
    </tr>

    <tr>
        <td>Photo Upload :</td>
        <td><asp:FileUpload ID="FilePhoto" runat="server" /></td>
        <td><asp:Image ID="Image1" runat="server" Height="30px" Width="50px" /></td>
    </tr>
    <tr>
        <td><asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />&nbsp;&nbsp;&nbsp;<asp:Button
                ID="btnUpdate" runat="server" Text="Update" onclick="btnUpdate_Click" /></td>
        <td><asp:Button ID="btnDelete" runat="server" Text="Delete"
                onclick="btnDelete_Click" />&nbsp;&nbsp;&nbsp;<asp:Button ID="btnCancel"
                runat="server" Text="Cancel" onclick="btnCancel_Click" /></td>
    </tr>
    </table>
   
    </div>
        <asp:GridView ID="GRVEMP1" runat="server" AutoGenerateColumns="False"
            BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
            CellPadding="3" EnableModelValidation="True" ForeColor="Black"
            GridLines="Vertical" DataKeyNames="Id" onrowdeleting="GRVEMP1_RowDeleting">
            <AlternatingRowStyle BackColor="#CCCCCC" />
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
           
            <Columns>
           
            <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="City">
            <ItemTemplate>
                <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>
           
            <asp:TemplateField HeaderText="Salary">
            <ItemTemplate>
                <asp:Label ID="lblSalary" runat="server" Text='<%# Eval("Salary") %>'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="DOJ" >
            <ItemTemplate >
                <asp:Label ID="lblDoj" runat="server" Text='<%# Eval("Doj","{0:d}") %>'   ></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Photo">
            <ItemTemplate>
                <asp:Image ID="ImagePhoto" runat="server" ImageUrl='<%# Eval("Photo")  %>' Height="30px" Width="50px" />
            </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Command">
            <ItemTemplate>
                <asp:Button ID="btnSelect" runat="server" Text="Select" CommandName="Delete" />
            </ItemTemplate>

            </asp:TemplateField>

            </Columns>
        </asp:GridView>
    </center>

    </form>
</body>
</html>
=============================
 Code file .aspx.cs
=============================
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;

public partial class IUDFor : System.Web.UI.Page
{
    BLL b = new BLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Fetch();
        }
        this.btnUpdate.Visible = true;
    }
    protected void Fetch()
    {
        GRVEMP1.DataSource = b.Select1();
        GRVEMP1.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //this.btnUpdate.Visible = true;
        //b.Id1 = Convert.ToInt32(tx.Text);
        b.Name1 = txtName.Text;
        b.City1 = txtCity.Text;
        b.Salary1 = Convert.ToInt32(txtSal.Text);
        b.Doj1 = Convert.ToDateTime(txtDoj.Text);
        string Serverpath = "~\\Upload/";
        if (FilePhoto.HasFile)
        {
            string name = FilePhoto.FileName;
            Serverpath = Serverpath +  name;
            FilePhoto.SaveAs(Server.MapPath(Serverpath));
        }
        b.Photo1 = Serverpath;
        b.Insert1();
        Fetch();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        b.Id1 = Convert.ToInt32(Session["id"].ToString());
        b.Name1 = txtName.Text;
        b.City1 = txtCity.Text;
        b.Salary1 = Convert.ToInt32(txtSal.Text);
        b.Doj1 = Convert.ToDateTime(txtDoj.Text);
        string Serverpath = "~\\Upload/";
        if (FilePhoto.HasFile)
        {
            string name = FilePhoto.FileName;
            Serverpath = Serverpath + name;
            FilePhoto.SaveAs(Server.MapPath(Serverpath));
        }
        b.Photo1 = Serverpath;
        b.Update1();
        Fetch();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        b.Id1 = Convert.ToInt32(Session["id"].ToString());
        b.Delete1();
        Fetch();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtCity.Text = "";
        txtSal.Text = "";
        txtDoj.Text = "";
        Image1.ImageUrl = "";
    }
    protected void GRVEMP1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id;
        id = Convert.ToInt16(GRVEMP1.DataKeys[e.RowIndex].Value);
        Session["Id"]= Convert.ToInt32(id);
        b.Id1 = Convert.ToInt32(id);
        DataSet ds = new DataSet();
        ds = b.SelectFetch();
        if (ds != null)
        {
            txtName.Text = ds.Tables[0].Rows[0][1].ToString();
            txtCity.Text = ds.Tables[0].Rows[0][2].ToString();
            txtSal.Text = ds.Tables[0].Rows[0][3].ToString();
            string dt = ds.Tables[0].Rows[0][4].ToString();
            string dt1 = dt.Substring(0,10);
            txtDoj.Text = dt1.ToString();
            Image1.ImageUrl = ds.Tables[0].Rows[0][5].ToString();

        }
    }

}
=============================
 BLL file - BLL.cs
=============================
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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;

/// <summary>
/// Summary description for BLL
/// </summary>
public class BLL
{
    DAL d = new DAL();
public BLL()
{
//
// TODO: Add constructor logic here
//
}
    private int Id;

    public int Id1
    {
        get { return Id; }
        set { Id = value; }
    }
    private string Name;

    public string Name1
    {
        get { return Name; }
        set { Name = value; }
    }
    private string City;

    public string City1
    {
        get { return City; }
        set { City = value; }
    }
    private int Salary;

    public int Salary1
    {
        get { return Salary; }
        set { Salary = value; }
    }
    private DateTime Doj;

    public DateTime Doj1
    {
        get { return Doj; }
        set { Doj = value; }
    }
    private string Photo;

    public string Photo1
    {
        get { return Photo; }
        set { Photo = value; }
    }

    public void Insert1()
    {
        d.Insert(Name, City, Salary, Doj, Photo);
    }

    public void Update1()
    {
        d.Update(Id, Name, City, Salary,Doj,Photo);
    }

    public void Delete1()
    {
        d.Delete(Id);
    }

    public DataSet Select1()
    {
        DataSet ds = new DataSet();
        ds = d.select();
        return ds;
    }

    public DataSet SelectFetch()
    {
        DataSet ds = new DataSet();
        ds = d.selectfetch1(Id);
        return ds;
    }
}
=============================
 DAL file - DAL.cs
=============================
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
public DAL()
{
//
// TODO: Add constructor logic here
//
}
    SqlConnection con = new SqlConnection("Data Source=VISION-PC;Initial Catalog=test;User ID=sa;Password=vision");

    public void CommanData(string str)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(str,con);
        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
        con.Close();
    }

    public void Insert(string Name,string City,int Salary,DateTime Doj,string Photo)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("InsertData", con);
        cmd.Parameters.AddWithValue("@Name", Name);
        cmd.Parameters.AddWithValue("@City", City);
        cmd.Parameters.AddWithValue("@Salary", Salary);
        cmd.Parameters.AddWithValue("@Doj", Doj);
        cmd.Parameters.AddWithValue("@Photo", Photo);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
        con.Close();
    }

    public void Update(int Id, string Name, string City, int Salary, DateTime Doj, string Photo)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("UpdateData", con);
        cmd.Parameters.AddWithValue("@Id", Id);
        cmd.Parameters.AddWithValue("@Name", Name);
        cmd.Parameters.AddWithValue("@City", City);
        cmd.Parameters.AddWithValue("@Salary", Salary);
        cmd.Parameters.AddWithValue("@Doj", Doj);
        cmd.Parameters.AddWithValue("@Photo", Photo);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
        con.Close();
    }

    public void Delete(int Id)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("DeleteData", con);
        cmd.Parameters.AddWithValue("@Id", Id);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
        con.Close();
    }

    public DataSet select()
    {
        con.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("Select * from Emp", con);
        da.Fill(ds);
        con.Close();
        return ds;
    }

    public DataSet selectfetch1(int Id)
    {
        con.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("Select * from Emp where Id = "+ Id +"", con);
        da.Fill(ds);
        con.Close();
        return ds;
    }
}
=============================
 Store Procedure
=============================
 Insert SP - InsertData
=============================
USE [test]
GO
/****** Object:  StoredProcedure [dbo].[InsertData]    Script Date: 07/03/2013 12:17:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[InsertData]
@Name Varchar(50),
@City Varchar(50),
@Salary int,
@Doj DateTime,
@Photo Varchar(Max)
AS
BEGIN
insert into  Emp values ( @Name, @City, @Salary ,Convert(Datetime,@Doj,105),@Photo)
END
=============================
 Update SP - UpdateData
=============================
USE [test]
GO
/****** Object:  StoredProcedure [dbo].[UpdateData]    Script Date: 07/03/2013 12:17:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[UpdateData]
@Id int,
@Name Varchar(50),
@City Varchar(50),
@Salary int,
@Doj DateTime,
@Photo Varchar(Max)
AS
BEGIN
Update Emp set Name=@Name, City=@City, Salary=@Salary,Doj=CONVERT(DateTime,@Doj,105),Photo=@Photo where Id=@Id
END
=============================
 Delete SP - DeeleteData
=============================
USE [test]
GO
/****** Object:  StoredProcedure [dbo].[DeleteData]    Script Date: 07/03/2013 12:17:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[DeleteData]
@Id int

AS
BEGIN
Delete from Emp where Id = @Id
END
=============================
The End
=============================
GUI Output
-------------








No comments:

Post a Comment

Demo for Repository Pattern in ASP.Net

----------------------------------------------------------- ----------------------------------------------------------- Repository Projec...