Using LINQ to SQL Query

Here you get very basic idea of LINQ to SQL Query using code.

Step by step Linq to SQL

1. Open visual studio 2008 and create a new project --> Web --> ASP.NET Web Application --> LinqBasic(Project Name)


2. Once project is load we need to create a database.

3. Right Click the App_data --> Add --> New Item


4. Select SQL Server Database --> give the database name as "Employee.mdf" and Click the "Add" button


5. Now open the "Server Explorer" go to employee database create a new table with following fields. 

Empid Int,
Employeename nvarchar(50),
Designation nvarchar(50),
DOJ  datetime.
Table name: emp


6. Insert the data into "emp" table


7. Now we can add a LINQ to SQL Class

8. Right click on "LinqBasic" Application > Add > New Item 

9. Select "LINQ to SQL Classes" Name as "LinqtoEmp.dbml" and click add.


10. Now you will get "Object Relational Designer " window

11. Drag and drop the "emp" table from "server explorer" to "Object Relational Designer". It should appear as follows.


12. Save all and build.

13. Now open default.aspx and change title tag as you like.

14. Switch to design view. Drag and drop the "GridView" from Toolbox to designer view.


15. Right click and select  code view.

16. Paste this code I your page load event.

protected void Page_Load(object sender, EventArgs e)
    LinqtoEmpDataContext LinqEmp = new LinqtoEmpDataContext();
    var emplist = from em in LinqEmp.emps select em;
    GridView1.DataSource = emplist;

17. Build and run the application.