Click here to Skip to main content
15,867,756 members
Articles / Web Development / ASP.NET

Accessing Data using Language Integrated Query (LINQ) in ASP.NET WebPages – Part 2

Rate me:
Please Sign up or sign in to vote.
2.94/5 (7 votes)
7 Feb 2008CPOL5 min read 49.3K   23   2
Access various types of data using LinqDataSource control ASP.NET

Introduction

Part 2, sequel and the last part of the article Accessing data using Language Integrated Query(LINQ) in ASP.NET WebPages - Part 1 explains how to create entity classes to represent SQL Server database and tables using Object Relational Designer and display data in a web page using LinqDataSource control.

LinqDataSource Control

Using declarative markup, you can create a LinqDataSource control that connects to data from a SQL database table, and configure it to handle updating, inserting, and deleting data without having to write the SQL commands to perform these tasks. The LinqDataSource class also provides an event model that enables you to handle customized scenarios. To use LinqDataSource control to query a database, entity objects must be created to represent the database and its tables. You can use the Object Relational Designer or the SqlMetal.exe utility to generate these objects.

The ContextTypeName property must be set to the object that represents the database and the TableName property to the object that represents the database table.

Object Relational Designer

The Object Relational Designer (O/R Designer) provides a visual design surface for creating LINQ to SQL entity classes and associations (relationships) based on objects in a database. In other words, the O/R Designer is used to create an object model in an application that maps to objects in a database. The O/R Designer also provides functionality to map stored procedures and functions to DataContext methods for returning data and hydrating entity classes. The O/R Designer also provides the ability to design inheritance relationships between entity classes. The O/R Designer currently supports only SQL Server 2000 and later, and SQL Server Express databases.

The O/R Designer has two distinct areas on its design surface: the entities pane on the left, and the methods pane on the right. The entities pane is the main design surface that displays the entity classes defined in the file. The methods pane is the design surface that displays the DataContext methods that are mapped to stored procedures and functions. You can hide the methods pane. Using methods pane is out of the scope of this article.

Code Generation tool (SqlMetal.Exe)

The SqlMetal command-line tool is used to generate code and mapping for the LINQ to SQL component of the .NET Framework. By applying options as listed below, you can instruct SqlMetal to perform a number of different actions, including the following:

From a database, generate source code and mapping attributes or a mapping file.
From a database, generate an intermediate DBML file for customization.
From a DBML file, generate code and mapping attributes or a mapping file.
The file is located by default at [drive:]\program files\microsoft sdks\windows\v6.0A\bin.

>sqlmetal [options] [<input file>]

Example 1: To generate a .dbml file with extracted SQL metadata, issue the following command in the Visual Studio Command Prompt window.

>sqlmetal /server:myserver /database:northwind /dbml:mymeta.dbml

Example 2: To generate a .dbml file with extracted SQL metadata from a .mdf file with SQL Server Express, issue the following command:

>sqlmetal /dbml:mymeta.dbml mydbfile.mdf

Walkthrough 2

Connecting Sql Server Express Database using O/R Designer and LinqDataSource and perform queries on a table to display data in a ASP.NET webpage

Add a new Webpage (Default2.aspx) to the existing WebSite project you have just created to test the sample Walkthrough 1.

In the WebSite Menu, select “Add New Item” command to choose the “Linq to SQL Classes” from the available list of templates in the dialog. It creates a new entity class’s file called DataClasses.dbml in the website’s App_Code folder.

After you add the new LINQ to SQL file to the project, the empty design surface represents a DataContext ready to be configured. The DataContext name corresponds to the name that you provided for the .dbml file. For this walkthrough, because you named the LINQ to SQL file DataClasses.dbml, the DataContext is named DataClassesDataContext. You can verify this by clicking an empty area on the designer and inspecting the Properties window.

The next step is to create LINQ to SQL classes that are mapped to database tables by dragging tables from Server Explorer/Database Explorer onto the O/R Designer. The result is a LINQ to SQL entity class that maps to the table in the database.

linqdatapic5.jpg

In Server Explorer/Database Explorer, locate the tables in the SQL Server version of the sales.dbf sample database used in this article.

Drag the Product node from Server Explorer/Database Explorer onto the O/R Designer surface.

An entity class named Product is created to represent a single product from the Product table. It has properties that correspond to the columns in the Product table.

Drag a LinqDataSource control (ID=LinqDataSource1) into Default2.aspx page from the data tab in the toolbox and configure it to access the data sources and queries you have just created in the project as below in the Configure Data Source Wizard.

  1. Choose “DataClassDataContext” as a Context object.

linqdatapic6.jpg

  1. Choose “products” as Table in the Data Selection and check all the fields that appear in the Select list.
  2. Click the “Finish” button to complete the configuration of LinqDataSource control.

Drag a GridView control (ID=GridView1) into the page from the data tab in the tool box and set Data Source property to LinqDataSource1.

That’s it. Now run the webpage and see the output as displayed below.

linqdatapic7.jpg

Conclusion

This two-part article explained basically how to use LinqDataSource control available with the new version of ASP.NET in Visual Studio 2008 to connect to two different data sources namely Object data collection and SQL Server Express Database table. It also detailed how to define object and entity classes and execute queries to display data. In the coming articles, I would explore more about using LINQ with other data sources as well.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder BB Systems CIT-GPNP
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Qadri Jillani11-Jul-13 11:25
Qadri Jillani11-Jul-13 11:25 
GeneralGreat article. Pin
Rajib Ahmed8-Feb-08 4:07
Rajib Ahmed8-Feb-08 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.