SIGN UP MEMBER LOGIN:    
ARTICLE

Using LINQ to SQL Query

Posted by Lakshmanan P Articles | LINQ with C# May 04, 2010
Here in this article, you will get very basic idea of LINQ to SQL Query using code.
Reader Level:

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)

1.gif

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

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

2.gif

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

3.gif

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

4.gif

6. Insert the data into "emp" table

5.gif

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.

6.gif

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.

7.gif

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.

8.gif

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;
    GridView1.DataBind();
}

17. Build and run the application.

9.gif

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

very helpful, thx

Posted by Eugen May 26, 2012

Yes Your Demo is so good but if i want to access data from two related table and display in single GridView using LINQ If Possible send me demo on my email that is gunjalamol92@gmail.com

Posted by Amol Gunjal Jun 23, 2010

Hi..,
      Nice..

Posted by vijay kumar May 11, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor