Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29975 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Working with LINQ to SQL Classes and LinqDataSource Controls

Working with LINQ to SQL Classes and LinqDataSource Controls

Article posted by SheoNarayan on 1/11/2008 | Views: 11072 | Category: ASP.NET | Level: Beginner red flag


As you all know LINQ (Language Integrated Query) is the new way to communicate with database that has been introduced in .NET Framework 3.5. In this article, I am going to explain you how to work with LINQ to SQL Classes and LinqDataSource control. For this, you will need .NET Framework 3.5, Visual Web Developer 2008, and SqlServer Express Database.

In order to work with LinqDataSource control, let’s create a database first.



Right click the App_Data folder and select Add New Item … . Select Sql Server Database and click Add. Go to database explorer and explore the Database > Tables. Right click Tables and click Add New Table. Create following fields as displayed in the image below.




Now its time to create a Linq to Sql classes. This can work as your Data Access Layer of the application. For this, right click the project folder in the solution explorer and select Add ASP.NET Folder … and Select App_Code. Right click App_Code folder and select Linq to SQL Classes and add it. You will see that you will have a DataClasses.dbml file into the App_Code folder. Double click DataClasses.dbml to open it into Object Relational Designer (ORD) window. Now its time to drag the Address table into ORD from Database Explorer. Go to Database Explorer and drag the Address table to the ORD window. This will create two more file into App_Code related with DataClasses.dbml named DataClasses.dbml.layout and DataClasses.designer.cs or .vb depending upon what language you have selected. Save DataClasses.dbml.




Till now we have created LINQ to SQL Classes and linked it with the database table. You can open DataClasses.designer.cs file and see what sorts of code have been generated automatically to deal with the Address database table.





Now we can link our LinqDataSource control with this LINQ to SQL Classes. To do that, create a new .aspx page, go to Design view and drag LinqDataSource control from data tab of the Toolbox. Open the smart tag of LinqDataSource control and select Configure Data Source …. In the Context Object list, select DataClassesDataContext from the dropdown and click Next. Now select Addresses (Table<Address>) from the table dropdown list and click Finish. To allow manipulating the data of the database, again open the smart tag of the LinqDataSource control and select Enable Insert, Enable Delete and Enable Update checkboxes.




Now your LinqDataSource control is ready to be attached with any Data controls like GridView, ListView etc. Just drag a GridView from Data tab and specify the DataSource property to the LinqDataSource control. Run your .aspx page and you will see that your GridView is populating records from the database through LinqDataSource control efficiently and easily.




Should you require any clarification or facing any problem, please write your feedback.
Happy Coding and Take Care !!!.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

>> Write Response - Respond to this post and get points
Related Posts

This is a simple tip to validate user's input, restricting any dagerious html tagas entering etc.

This article describes how to save an image file into the database and show them in the GridView.

The article discusses one of the new feature introduced with .NET 4.0 which enable you to Redirect your page for Search Engine

In this artcle , I will be explaining the common and mostly used web.config tags, their different sections and also dicuss about securing the the config file.

Mostly time Developers do not use Editable grid-view because of lot’s of reason… Developers prefer manually coding but editable gridview is good way to update database. I am using Gridview by Wizard do Update. I am submitting all process to step by step first fill then create bool function and go to assemble row editing & updating Event.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:57:35 AM