Search...

Tuesday, March 13, 2012

LINQ for beginners tutorials in Asp.Net


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

<%@ 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 demo</title>
    <style type="text/css">
    fieldset
    {
    width:500px;
    padding:0px 0px 20px 20px;
    }
    legend
    {
    padding:20px 0px 20px 20px;
    }
    table
    {
    width:400px;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <fieldset>
    <legend>Read All Department</legend>
    <asp:GridView ID="gridviewDept" runat="server">
    </asp:GridView>
    </fieldset>
    </div>
 
    <div>
    <fieldset>
    <legend>Read All Employee</legend>
    <asp:GridView ID="gridEmp" runat="server">
    </asp:GridView>
    </fieldset>
    </div>
    </form>
</body>
</html>

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

using System;
using System.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataClassesDataContext ctx = new DataClassesDataContext();
            gridviewDept.DataSource = ctx.GetTable<Department>().ToList();
            gridviewDept.DataBind();

            var data = from dep in ctx.GetTable<Department>()
                       join emp in ctx.GetTable<Employee>()
                       on dep.Id equals emp.Department_Id
                       select new { emp.Id, emp.EmployeeName, emp.Salary, dep.DepartmentName };
            gridEmp.DataSource = data.ToList();
            gridEmp.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: