Sunday, 7 April 2013

three layer and EDM in asp.net

BO :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for BO
/// </summary>
public class BO
{
    public BO()
    {
       
    }
    public int EID
    {
        get;
        set;
    }
    public int? DID
    {
        get;
        set;
    }
    public string NAME
    {
        get;
        set;
    }
    public string DNAME
    {
        get;
        set;
    }
}
DAL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DatabaseModel;

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
    #region
    SwashEntities o = new SwashEntities();
    int i = 0;
    #endregion
    public string insert(BO p)
    {
        EMP q = new EMP();
        q.EID = p.EID;
        q.NAME = p.NAME;
        q.DID = p.DID;
        o.EMPs.AddObject(q);
        o.SaveChanges();
        return "One record saved.";
    }
    public string update(BO p)
    {
        i=Convert.ToInt32( p.EID);
        o.EMPs.Where(b => b.EID == i).First().NAME = p.NAME;
        o.SaveChanges();
        return "One record updated.";
    }
    public string delete(BO p)
    {
        i = Convert.ToInt32(p.EID);
        o.EMPs.DeleteObject(o.EMPs.Where(b => b.EID == i).First());
        o.SaveChanges();
        return "One record deleted.";
    }
    public List<BO> view()
    {
        var m = (from x in o.EMPs
                 join y in o.DEPTs
                 on x.DID equals y.DID
                 select new {x.EID,x.NAME,x.DID,y.DNAME }).ToList();
        List<BO> p=new List<BO>();
        for (i = 0; i < m.Count; i++)
        {
            BO q = new BO();
            if(m[i].EID>0)
            q.EID = m[i].EID;
            if (!string.IsNullOrEmpty(m[i].NAME))
                q.NAME = m[i].NAME;
            if (m[i].DID > 0)
                q.DID = m[i].DID;
            if (!string.IsNullOrEmpty(m[i].DNAME))
                q.DNAME = m[i].DNAME;
            p.Add(q);
        }
        return p;
    }
    public List<BO> dview()
    {
        var m = (from x in o.DEPTs
               
                 select new { x.DID, x.DNAME }).ToList();
        List<BO> p = new List<BO>();
        for (i = 0; i < m.Count; i++)
        {
            BO q = new BO();
            if (m[i].DID > 0)
                q.DID = m[i].DID;
            if (!string.IsNullOrEmpty(m[i].DNAME))
                q.DNAME = m[i].DNAME;
            p.Add(q);
        }
        return p;
    }
}
BAL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DatabaseModel;

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
    #region
    SwashEntities o = new SwashEntities();
    int i = 0;
    #endregion
    public string insert(BO p)
    {
        EMP q = new EMP();
        q.EID = p.EID;
        q.NAME = p.NAME;
        q.DID = p.DID;
        o.EMPs.AddObject(q);
        o.SaveChanges();
        return "One record saved.";
    }
    public string update(BO p)
    {
        i=Convert.ToInt32( p.EID);
        o.EMPs.Where(b => b.EID == i).First().NAME = p.NAME;
        o.SaveChanges();
        return "One record updated.";
    }
    public string delete(BO p)
    {
        i = Convert.ToInt32(p.EID);
        o.EMPs.DeleteObject(o.EMPs.Where(b => b.EID == i).First());
        o.SaveChanges();
        return "One record deleted.";
    }
    public List<BO> view()
    {
        var m = (from x in o.EMPs
                 join y in o.DEPTs
                 on x.DID equals y.DID
                 select new {x.EID,x.NAME,x.DID,y.DNAME }).ToList();
        List<BO> p=new List<BO>();
        for (i = 0; i < m.Count; i++)
        {
            BO q = new BO();
            if(m[i].EID>0)
            q.EID = m[i].EID;
            if (!string.IsNullOrEmpty(m[i].NAME))
                q.NAME = m[i].NAME;
            if (m[i].DID > 0)
                q.DID = m[i].DID;
            if (!string.IsNullOrEmpty(m[i].DNAME))
                q.DNAME = m[i].DNAME;
            p.Add(q);
        }
        return p;
    }
    public List<BO> dview()
    {
        var m = (from x in o.DEPTs
               
                 select new { x.DID, x.DNAME }).ToList();
        List<BO> p = new List<BO>();
        for (i = 0; i < m.Count; i++)
        {
            BO q = new BO();
            if (m[i].DID > 0)
                q.DID = m[i].DID;
            if (!string.IsNullOrEmpty(m[i].DNAME))
                q.DNAME = m[i].DNAME;
            p.Add(q);
        }
        return p;
    }
}
UI :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
    <style>
    .A
    {
        list-style:none;
         width:940px;
    }
    .B
    {
        width:360px;
         text-align:right;
         float:left;
         padding-top:-5px;
    }
    .C
    {
        width:20px;
        text-align:center;
        float:left;
        padding-top:-5px;
        font-weight:900;
    }
    .D
    {
        width:560px;
        text-align:left;
        padding-top:-5px;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="up" runat="server" ClientIDMode="Static">
    <ContentTemplate>
      <div align="center">
    <ul class="A">
    <li class="B">EID</li>
    <li class="C">:</li>
    <li class="D"><asp:TextBox ID="tb" runat="server"></asp:TextBox></li>
    </ul>
     <ul class="A">
    <li class="B">NAME</li>
    <li class="C">:</li>
    <li class="D"><asp:TextBox ID="tb1" runat="server" Width="200"></asp:TextBox></li>
    </ul>
     <ul class="A">
    <li class="B">DEPARTMENT NAME</li>
    <li class="C">:</li>
    <li class="D"><asp:DropDownList ID="ddl" runat="server"></asp:DropDownList></li>
    </ul>
     <ul class="A">
    <li class="B">&nbsp;</li>
    <li class="C">&nbsp;</li>
    <li class="D"><asp:Button ID="btn" runat="server" Text="Save" Width="80"
            oncommand="btn_Command" />&nbsp<asp:Button ID="Button1" runat="server" Text="Reset" Width="80" /></li>
    </ul>
    <ul class="A">
    <li class="B">&nbsp;</li>
    <li class="C">&nbsp;</li>
    <li class="D">
    <asp:DataList ID="dl" runat="server" oneditcommand="dl_EditCommand"
            oncancelcommand="dl_CancelCommand" onupdatecommand="dl_UpdateCommand"
            ondeletecommand="dl_DeleteCommand">
    <HeaderTemplate>
    <table>
    <tr><th>UPDATE</th><th>DELETE</th><th>EID</th><th>NAME</th><th>DNAME</th></tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr><td><asp:LinkButton ID="lb" Font-Underline="false" runat="server" CommandName="edit" Text="Edit" CommandArgument='<%#Eval("eid") %>'></asp:LinkButton></td><td><asp:LinkButton ID="lb1" CommandName="Delete" runat="server" Font-Underline="false" Text="Delete" CommandArgument='<%#Eval("eid") %>'></asp:LinkButton></td><td><%#Eval("eid") %></td><td><%#Eval("name") %></td><td><%#Eval("dname") %></td></tr>
    </ItemTemplate>
    <EditItemTemplate>
    <tr><td><asp:LinkButton ID="lb2" CommandName="cancel" runat="server" Font-Underline="false" Text="Cancel" CommandArgument='<%#Eval("did") %>'></asp:LinkButton></td><td><asp:LinkButton ID="lb3" CommandName="update" Font-Underline="false" runat="server" Text="Update" CommandArgument='<%#Eval("eid") %>'></asp:LinkButton></td><td><%#Eval("eid") %></td><td><asp:TextBox ID="tb2" runat="server" Text='<%#Eval("name") %>'></asp:TextBox></td><td><asp:DropDownList ID="ddl1" runat="server"></asp:DropDownList></td></tr>
    </EditItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:DataList>
    </li>
    </ul>
    </div>
    </ContentTemplate>
    </asp:UpdatePanel> 
    </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;


public partial class _Default : System.Web.UI.Page
{
    #region
    BO p = new BO();
    BAL o = new BAL();
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddl.DataSource = o.dview().ToList();
            ddl.DataTextField = "dname";
            ddl.DataValueField = "did";
            ddl.DataBind();
            fill();
        }
    }
    void fill()
    {
        dl.DataSource = o.view();
        dl.DataBind();
    }
    protected void btn_Command(object sender, CommandEventArgs e)
    {
        p.EID = Convert.ToInt32(tb.Text);
        p.NAME = tb1.Text;
        p.DID = Convert.ToInt32(ddl.SelectedValue);
        ScriptManager.RegisterStartupScript(this, GetType(), "x", "alert('"+o.insert(p)+"')", true);
        fill();
    }
   
    protected void dl_EditCommand(object source, DataListCommandEventArgs e)
    {
        dl.EditItemIndex = e.Item.ItemIndex;
        fill();
        ((DropDownList)dl.Items[e.Item.ItemIndex].FindControl("ddl1")).DataSource = o.dview();
        ((DropDownList)dl.Items[e.Item.ItemIndex].FindControl("ddl1")).DataTextField = "dname";
        ((DropDownList)dl.Items[e.Item.ItemIndex].FindControl("ddl1")).DataValueField = "did";
        ((DropDownList)dl.Items[e.Item.ItemIndex].FindControl("ddl1")).DataBind();
        ((DropDownList)dl.Items[e.Item.ItemIndex].FindControl("ddl1")).SelectedValue = ((LinkButton)dl.Items[e.Item.ItemIndex].FindControl("lb2")).CommandArgument;
    }
    protected void dl_CancelCommand(object source, DataListCommandEventArgs e)
    {
        dl.EditItemIndex = -1;
        fill();
    }
    protected void dl_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        p.EID = Convert.ToInt32(((LinkButton)dl.Items[e.Item.ItemIndex].FindControl("lb3")).CommandArgument);
        p.NAME = ((TextBox)dl.Items[e.Item.ItemIndex].FindControl("tb2")).Text;
        p.DID = Convert.ToInt32(((DropDownList)dl.Items[e.Item.ItemIndex].FindControl("ddl1")).SelectedValue);
        dl.EditItemIndex = -1;
        ScriptManager.RegisterStartupScript(this, GetType(), "x", "alert('" + o.update(p) + "')", true);
        fill();
    }
    protected void dl_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        p.EID = Convert.ToInt32(((LinkButton)dl.Items[e.Item.ItemIndex].FindControl("lb1")).CommandArgument);
        ScriptManager.RegisterStartupScript(this, GetType(), "y", "alert('" + o.delete(p) + "')", true);
        fill();
    }
}
PAGE LOOKS LIKE :

No comments:

Post a Comment