Go to DotNetFunda.com
 Online : 1511 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Working with LINQ to SQL Classes and LinqDataSource Controls

Submit Article | Articles Home | Search Articles |

Working with LINQ to SQL Classes and LinqDataSource Controls

 Posted on: 1/11/2008 4:11:39 AM by SheoNarayan | Views: 4071 | Category: ASP.NET | Level: Beginner | Print Article
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.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

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.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Administrator]
Biography:Throughout 1st in all educational exams.
Major qualifications: HDCS, ADCA, MCA, MCTS
Developing and architecting applications in Microsoft technologies since year 2001.
Location: Hyderabad, India
 Latest post(s) from SheoNarayan

   ◘ Common operation with Files and Folders in ASP.NET posted on 9/4/2009 4:23:03 PM
   ◘ Working with CustomValidator control in ASP.NET posted on 8/19/2009 9:27:43 AM
   ◘ Backup and Restore database in ASP.NET posted on 8/7/2009 5:30:24 PM
   ◘ Passing data between layers using Generic list collection posted on 7/19/2009 7:28:40 AM
   ◘ jQuery and ASP.NET AJAX UpdatePanel posted on 7/17/2009 6:53:04 PM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)