Tutorial RSS
 
Navigator: Home - Display - Binding LINQ to SQL Query to ObjectDataSource in VB

Binding LINQ to SQL Query to ObjectDataSource in VB

This tutorial will show you how to use LINQ in conjunction with the ObjectDataSource to enable you to sort the data in a descending format. VB version.

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.


Looking for the C# version? Click here!

Looking for more .NET Database Tutorials? Click Here!

In this tutorial, we will be looking at how we can use 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.
Although it may seem like a long process, Visual Studio.NET actually 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. Make sure you save it after draggin on the table we will be using.

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. 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

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.


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:

<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>


Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!




Looking for the C# version? Click here!

Looking for more .NET Database Tutorials? Click Here!

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!

411asp.net123aspxDotNetFreaksServer Intellect