Binding ObjectDataSource to LINQ to SQL Query in VB


Server Intellect


Binding ObjectDataSource to LINQ to SQL Query in VB

This tutorial was created with Microsoft Visual Studio .NET 2008. However, if you are using 2005, you can implement LINQ by downloading Microsoft's LINQ Community Technology Preview release from here.

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

In this tutorial, we will be looking at using LINQ to SQL Classes in conjunction with the ObjectDataSource control. We will be using a SQL database, and then representing that with a LINQ to SQL class. We will then build our own class that will use the LINQ to SQL class to interact with our database. We can then reference the method we build by our ObjectDataSource.
It may seem like a long process, but Visual Studio.NET does most of the work for us.

We will start out by creating a database - or if you have your own, you can use that. For this example, we will be using a SQL database with one table, and three columns - id, name, city.

Once we have our database set up and have added some sample data to it, we can create our LINQ to SQL class. Right-click our project in Solution Explorer and choose Add New Item, LINQ to SQL Classes. In this example, we named it People.dbml
This creates a class to represent our database.
Once this has been created, we can drag onto the designer area our table from Server Explorer, then Save.

Now we want to add our own class (Right-click project, Add New Item, Class). We want it in the App_Code folder. In this class, we will write a method to select the data from the database, order it, and then return. Our method will look something like this:

Public Shared Function [Select]() As IEnumerable(Of tblPeople)
Dim dBase As New PeopleDataContext()
Return dBase.tblPeoples.OrderBy(Function(p) p.name)
End Function

Here, we use LINQ to first instantiate our LINQ to SQL class, and then return an ordered collection of data.
The entire class looks like this:

Imports Microsoft.VisualBasic

Public Class People
Public Shared Function [Select]() As IEnumerable(Of tblPeople)
Dim dBase As New PeopleDataContext()
Return dBase.tblPeoples.OrderBy(Function(p) p.name)
End Function
End Class

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

A snippet from our LINQ to SQL class (which is generated by VS), which represents our database table follows:

Public Sub New()
MyBase.New
OnCreated
End Sub

<Column(Storage:="_id", AutoSync:=AutoSync.OnInsert, DbType:="Int NOT NULL IDENTITY", IsPrimaryKey:=true, IsDbGenerated:=true)> _
Public Property id() As Integer
Get
Return Me._id
End Get
Set
If ((Me._id = value) _
= false) Then
Me.OnidChanging(value)
Me.SendPropertyChanging
Me._id = value
Me.SendPropertyChanged("id")
Me.OnidChanged
End If
End Set
End Property

<Column(Storage:="_name", DbType:="VarChar(50)")> _
Public Property name() As String
Get
Return Me._name
End Get
Set
If (String.Equals(Me._name, value) = false) Then
Me.OnnameChanging(value)
Me.SendPropertyChanging
Me._name = value
Me.SendPropertyChanged("name")
Me.OnnameChanged
End If
End Set
End Property

<Column(Storage:="_city", DbType:="VarChar(50)")> _
Public Property city() As String
Get
Return Me._city
End Get
Set
If (String.Equals(Me._city, value) = false) Then
Me.OncityChanging(value)
Me.SendPropertyChanging
Me._city = value
Me.SendPropertyChanged("city")
Me.OncityChanged
End If
End Set
End Property

We can then simply implement this into our web application using a GridView and an ObjectDataSource control. The DataSource will use the Select Method we just created, when we specify its SelectMethod. The TypeName refers to the class we created. The ASPX page will look something like this:

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

<form id="form1" runat="server">
Alphabetically Ordered By Name using LINQ and ObjectDataSource:<br />
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1"
Width="408px" />

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="People" SelectMethod="Select" />
</form>

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
Gratis Casino Spiele said:

I like the syntax better..With the empty get; set; it feels too much like an interface. I'm sure whatever we end up with will become second nature within a couple days anyway. It's nice of them to include such features that save me from typing a couple extra of lines of code.Thanks for such a great code...

Posted 02/26/2010 at 1:43 AM
Call Center Software said:

Even though LINQ became so important in the field of web development the tutorials on it are rare unlike that of others such as SQL, MySQL etc. that’s why the entire tutorials on LINQ that are available presently became precious for computer students and programmers. This article consists of such a LINQ tutorial. This article shows us how binding of ObjectDataSource to LINQ-to-SQL query can be done in VB. In this tutorial the author shows the LINQ to SQL Classes in conjunction with the ObjectDataSource. The entire tutorial is in simple language and it is easy to understand by all computer students and programmers. These sort of articles will help others to learn about LINQ and it surely will result in the increasing the usage of LINQ instead of conventional data querying technique since it have many advantages over them.

Posted 05/06/2010 at 12:05 PM
Download Free Games said:

selectively display records in a recordset, the feature to use is SQL - Structured Query Language. By creating a query (a text string which tells VB what to include in a recordset or what actions to take against the data in a recordset) you can greatly simplify the code you have to write in an application that utilizes databases.

Posted 06/03/2010 at 2:11 AM
Agile Dinesh said:

The ObjectDataSource control works with any business object that encapsulates data and provides data-management services. The ObjectDataSource control uses the standard data source control object model, so that data-bound controls, such as the GridView control, can bind to it.

Posted 06/03/2010 at 5:50 AM
kuber infotek said:

the availability of ever more powerful programming tools and environments such as Visual Basic and Visual Studio.NET, as well as the availability of powerful database engines such as the free SQL Server 2005 Express Edition, more and more people find themselves having to learn the basics of SQL queries and statements.

Posted 06/12/2010 at 3:08 AM
town car said:

the availability of ever more powerful programming tools and environments such as Visual Basic and Visual Studio.NET, as well as the availability of powerful database engines such as the free SQL Server 2005 Express Edition, more and more people find themselves having to learn the basics of SQL queries and statements. Sometimes they are professional developers who are experienced in other types of programming, and sometimes they are individuals whose expertise lies in other areas, but they suddenly find themselves programming database applications for fun and/or profit. If you fall into one of these categories, or are just curious about database programming, then this article is for you.

Posted 06/24/2010 at 7:06 AM
Hydraulic jacks said:

Once we have our database set up and have added some sample data to it, we can create our LINQ to SQL class. Right-click our project in Solution Explorer and choose Add New Item, LINQ to SQL Classes. In this example, we named it People.dbml

This creates a class to represent our database. Make sure you save it after draggin on the table we will be using.

Posted 07/22/2010 at 4:40 PM
Hydraulic jacks said:

Now, to add our own class, Right-click the project in Solution Explorer, Add New Item, Class. We want it in the App_Code folder. In this class, we will write a method to select the data from the database, order it, and then return.

Posted 07/22/2010 at 4:41 PM
Hydraulic jack said:

We can then simply implement this into our web application using a GridView and an ObjectDataSource control. The DataSource will use the Select Method we just created, when we specify its SelectMethod.

Posted 07/22/2010 at 4:44 PM
maison de credit said:

This is a good site. I hope to keep on reading such a wonderful next time!

Posted 08/25/2010 at 4:39 AM

Leave a Comment