Binding ObjectDataSource to LINQ to SQL Query in C#


Server Intellect


Binding ObjectDataSource to LINQ to SQL Query in C#

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.

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

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 static IEnumerable<tblPeople> Select()
{
PeopleDataContext dBase = new PeopleDataContext();
return dBase.tblPeoples.OrderBy(p=>p.name);
}

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:

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Generic;

/// <summary>
/// Summary description for People
/// </summary>
public class People
{
public static IEnumerable<tblPeople> Select()
{
PeopleDataContext dBase = new PeopleDataContext();
return dBase.tblPeoples.OrderBy(p=>p.name);
}

public People()
{
//
// TODO: Add constructor logic here
//
}
}

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

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

public tblPeople()
{
OnCreated();
}

[Column(Storage="_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int id
{
get
{
return this._id;
}
set
{
if ((this._id != value))
{
this.OnidChanging(value);
this.SendPropertyChanging();
this._id = value;
this.SendPropertyChanged("id");
this.OnidChanged();
}
}
}

[Column(Storage="_name", DbType="VarChar(50)")]
public string name
{
get
{
return this._name;
}
set
{
if ((this._name != value))
{
this.OnnameChanging(value);
this.SendPropertyChanging();
this._name = value;
this.SendPropertyChanged("name");
this.OnnameChanged();
}
}
}

[Column(Storage="_city", DbType="VarChar(50)")]
public string city
{
get
{
return this._city;
}
set
{
if ((this._city != value))
{
this.OncityChanging(value);
this.SendPropertyChanging();
this._city = value;
this.SendPropertyChanged("city");
this.OncityChanged();
}
}
}

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 migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

<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
casinos de jeux gratis said:

Windows Forms Application that will first define an array of numbers, and then we will use LINQ to Objects to interact with this collection of numbers. We will create buttons to display the results of calculations of the numbers, demonstrating the built-in functions of LINQ, that we can perform on most any collection

Posted 01/21/2010 at 2:54 AM
huntsville web design said:

Really nice series. I went through all of it, and I can't be more happy that this is really live. I'm currently downloading the Beta 2 to test myself. I'm curious if the changes recorded by the DataContext will be then sent to the database in the same order that they happen (not like DataSet/TableAdapters where they were sent one table after the other, from top to down in each table). I just HOPE that it records changes and applies them in the appropriate order!

Posted 03/01/2010 at 6:21 PM
bad credit car loan said:

the if (rdr2.Read()) statement reads the first row, and you don't do anything with it.

Then you enter the while loop which reads the 2 records you see. If you take out that if statement, the code should work.

Posted 06/12/2010 at 12:33 AM
placement argent said:

This was a useful post and I think it is rather easy to see from the other comments as well that this post is well written and useful. I bookmarked this blog a while ago because of the useful content and I am never being disappointed. Keep up the good work

Posted 08/10/2010 at 2:57 AM
xiaopohai said:
Posted 08/26/2010 at 9:17 PM

Leave a Comment