Working with LINQ to SQL Classes and LinqDataSource Controls

Posted by in ASP.NET category on for Beginner level | Views : 31372 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 !!!.
Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)