Using LinqDataSource control in ASP.net


In this article I'm trying to explain how to work with LinqDataSource control for displaying database data to our DataControls. During this process we can able to learn how to configure LinqDataSource control and how to connect with DataControl. This is very simple to communicate with our Database.

Using LinqDataSource control in ASP.net:



In this article I'm trying to explain how to work with LinqDataSource control for displaying database data to our DataControls. During this process we can able to learn how to configure LinqDataSource control and how to connect with DataControl. This is very simple to communicate with our Database.

Follow below steps to configure LinqDataSource control in our Application.

Step-1:



Create a WebApplication by right click on the solution and choose Webform and give a name for that as LinqDataSource.aspx

1

Step-2:



Right click on project and choose Add New Item then a new window will appear then choose Linq To Sql Classes.

2

When you click Ok button then one window will appear for confirmation message purpose.

Step-3:



Click yes, to create Linq classes under App_code folder in a project.

3

Step-4:



After creating Linq classes under App_code folder then create connection for that by right clicking the server explorer then right click on the Data Connections then choose Add connection option for adding connection to the project.

4

Step-5:



A new popup window will appear for creating new connection string purpose give a server name and choose Database name.

5

Step-6:



Click TestConnection button if connection created successfully then click on Ok button.

Step-7:



Now, based on your database selection what are the tables are listed out all are presented under Server Explorer DataBase name and expand tables you can able to find the list of tables under that particular database.

6

Step-8:



Now, simply drag & drop the table to the design surface of DataClasses.dbml file.

7

Step-9:



Go to application page design mode and simply drag and drop LinqDataSource control available under Data controls tab.

8

Step-10:



A new window will appear for configuring Linq DataSource control.

9

Step-11:



Click on Next button then its redirect to Configure Data section window then choose columns names by default its target to all columns, then click on Finish button.

Step-12:



Now, just drag and drop one Gridview control in design page and select LinqDataSource as a Choose Data Source list out in dropdown.

10

Step-13:



Choose LinqDataSource1 datasource, after that observe the Gridview design.

11

Step-14:



Then observe the Gridview source code part how it’s became changed.

<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext" EntityTypeName=""
Select="new (Emp_Name, Emp_job, Emp_Dep)" TableName="tblEmployees">
</asp:LinqDataSource>




<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

DataSourceID="LinqDataSource1"

>
<Columns>
<asp:BoundField DataField="Emp_Name" HeaderText="Emp_Name" ReadOnly="True" SortExpression="Emp_Name" />
<asp:BoundField DataField="Emp_job" HeaderText="Emp_job" ReadOnly="True" SortExpression="Emp_job" />
<asp:BoundField DataField="Emp_Dep" HeaderText="Emp_Dep" ReadOnly="True" SortExpression="Emp_Dep" />
</Columns>
</asp:GridView>


Step-15:



After complete this process just execute the code and see the output in output window just click on F5 button on key board.

12

Conclusion:



I hope this article will help you those who are looking for the same and those who are new to implement LinqDataSource control in your applications.

About Author:



This is Naveen, I completed my MCA and I have around 3 years of experience in ASP.net while implement Web Applications. For past 3 years I learn no.of Technologies while implement my applications apart from that I try to learn new technologies and I try to implement that while implement my project.


Comments

Author: vikram08 Jan 2014 Member Level: Bronze   Points : 0

thank you naveen its helpful for us.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: