Search...

Wednesday, March 14, 2012

Linq join example in Asp.Net c#


*************************************************************************

/*Add Stylesheet in your application names StyleSheet.css*/

 body {border:1px solid #000;padding:40px;}
.gridView{width:400px;background:#ffffff;border:1px solid #476db6; border-collapse:separate;}
.gridView th{border:0px;background:url('Images/hfbg.jpg') repeat-x;color:#ffffff;text-align:center;font-size:18px;}
.gridView td{padding-left:10px;border:1px solid #b5b5b5}

.altRow{background:url('Images/alt.png') repeat-x}

.gridPager{background:url('Images/hfbg.jpg') repeat-x;text-align:center;}
.gridPager table{margin:0px auto;}
.gridPager table td{width:25px;border:0px;}
.gridPager table span{cursor:pointer;font-weight:bold;color:#ffffff;}
.gridPager table a{cursor:pointer;text-decoration:none;color:#ffffff;}
.gridPager table a:hover{text-decoration:underline;}
.gridPager:hover{background:url('Images/hfbg.jpg') repeat-x;text-align:center;}

*************************************************************************

<%@ 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>Linq join example in Asp.Net c#</title>
    <link href="Resources/StyleSheet/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <p class="heading">Read Department</p>
    <asp:GridView ID="GridDept" runat="server"
    CssClass="gridView"
    AlternatingRowStyle-CssClass="altRow" >
    </asp:GridView>
 
    </div>
    </form>
</body>
</html>

*************************************************************************

using System;
using System.Linq;

public partial class _Default : System.Web.UI.Page
{
    private DataClassesDataContext ctx = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindEmployee();
        }
    }

    private void BindEmployee()
    {
        ctx = new DataClassesDataContext();
        var data = from emp in ctx.GetTable<Employee>()
                   join dept in ctx.GetTable<Department>()
                   on emp.Department_Id equals dept.Id
                   select new { dept.DepartmentName, emp.EmployeeName, emp.Salary };

        GridDept.DataSource = data;
        GridDept.DataBind();
    }
}

*************************************************************************

Go
Create database softwarekaffee

Go
use softwarekaffee

Go
create table Department
(
Id int identity primary key,
DepartmentName nvarchar(30)
)
Insert Into Department Values ('Sales');
Insert Into Department Values ('Production');
Insert Into Department Values ('Purchase');
Insert Into Department Values ('Stock');

Go
create table Employee
(
Id int identity primary key,
EmployeeName nvarchar(30),
Salary decimal(7,2),
Department_Id int references Department(Id)
)

Insert Into Employee Values ('Kaushik',41000,1);
Insert Into Employee Values ('Rahul',25000,2);
Insert Into Employee Values ('Sumit',26000,3);
Insert Into Employee Values ('Ravi',40000,4);
Insert Into Employee Values ('Rajeev',41000,1);
Insert Into Employee Values ('Amit',25000,2);
Insert Into Employee Values ('Gautam',26000,3);
Insert Into Employee Values ('Ranveer',40000,4);
Insert Into Employee Values ('Devendra',40000,4);
Insert Into Employee Values ('Sunil',40000,4);
Insert Into Employee Values ('Manas',41000,1);
Insert Into Employee Values ('Mishri Lal',25000,2);
Insert Into Employee Values ('Avinash',26000,3);
Insert Into Employee Values ('Alok',40000,4);
Insert Into Employee Values ('Pooja',41000,1);
Insert Into Employee Values ('Geeta',25000,2);
Insert Into Employee Values ('Lalita',26000,3);
Insert Into Employee Values ('Meenakshi',40000,4);
Insert Into Employee Values ('Rajendra',40000,4);
Insert Into Employee Values ('Sunita',40000,4);
Insert Into Employee Values ('Sandeep',26000,3);
Insert Into Employee Values ('Vinod',40000,4);
Insert Into Employee Values ('Remo',40000,4);

*************************************************************************

*************************************************************************


No comments: