Creating data classes for ASP.NET data controls
Part five of a seven-part series of blogs

Yes, ASP.NET provides DataSource controls, but wizards only get you so far. Share the secret of how Wise Owl create all our ASP.NET websites, using a single, simple data class.

  1. Data Classes in ASP.NET
  2. Our Example for this Data Classes Tutorial
  3. Storing and Retrieving Connection Strings
  4. ADO.NET: DataSets, DataTables and Data Adapters
  5. Consuming our Data Classes (this blog)
  6. A Class to Run a Stored Procedure and Return its Rows
  7. Consuming our Data Class on the Web Page

This blog is part of a larger online ASP.NET online tutorial.  For courses in VB programming or programming in C#, see the separate pages.

Posted by Andy Brown on 07 August 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Consuming our Data Classes

We haven't even created the data classes we'll need yet, but it can be useful with classes to first have a look at where we want to end up. 

Initial dropdownlist

Initially we want the dropdownlist to appear, populated by the list of films.


The HTML for the Page

The HTML for the dropdownlist will look something like this (this is the C# version - the VB one wouldn't need to set the onselectedindexchanged property).

The HTML for the dropdownlist

Notice that we set the DataTextField and DataValueField here, to specify which field will be displayed, and which will be stored.

The SQL Stored Procedure for the Dropdownlist

To get our class to work, we'll call the following stored procedure:

CREATE PROC [dbo].[spListMovies](

@MinOscars int=0












This will select all of the films winning more than a given number of Oscars.

How the Class Should Look

Here's what we'd like to do in C# when the page loads:

protected void Page_Load(object sender, EventArgs e)


if (!this.IsPostBack)


// on first loading the page, create a new "query"

aaaQuery sp = new aaaQuery("spListMovies", "csMovies");

// add a parameter so that we only show films winning

// at least 1 Oscar

sp.AddParameter("MinOscars", 1);

// get at the data table returned from this "query"

DataTable dt = sp.DtSelectedRecords;

// use this to populate our dropdown list

ddlMovieId.DataSource = dt;




The VB equivalent should look something like this:

Protected Sub Page_Load(sender As Object,

e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then

'on first loading the page, create a new "query"

Dim sp As New aaaQuery("spListMovies", "csMovies")

'add a parameter so that we only show films winning

'at least 1 Oscar

sp.AddParameter("MinOscars", 1)

'get at the data table returned from this "query"

Dim dt As DataTable = sp.DtSelectedRecords

'use this to populate our dropdown list

ddlMovieId.DataSource = dt


End If

End Sub

In both cases, what we're doing is creating a new object of type aaaQuery, applying the AddParameter method to it and then returning the DtSelectedRecords property as a DataTable.  The only remaining problem is that the aaaQuery class doesn't - yet - exist.  It's time to remedy that!

This blog has 0 threads Add post