Creating data classes for ASP.NET data controls
Part two 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 (this blog)
  3. Storing and Retrieving Connection Strings
  4. ADO.NET: DataSets, DataTables and Data Adapters
  5. Consuming our Data Classes
  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.

Our Example for this Data Classes Tutorial

The Single-Table Database of Movies

To keep things as simple as possible, this tutorial uses a single table called tblMovie containing limited details of 8 films:

The table of movies

The 8 films (or movies, if you must) in our table.


You can create this table yourself by copying and running the following SQL script:

CREATE TABLE [dbo].[tblMovie](

[MovieId] [int] NOT NULL,

[MovieName] [nvarchar](255) NULL,

[Oscars] [int] NULL,

[ReleaseDate] [date] NULL,



[MovieId] ASC




-- now insert records

INSERT [dbo].[tblMovie] ([MovieId], [MovieName], [Oscars], [ReleaseDate]) VALUES (1, N'Jurassic Park', 3, CAST(0xCF1A0B00 AS Date))

INSERT [dbo].[tblMovie] ([MovieId], [MovieName], [Oscars], [ReleaseDate]) VALUES (2, N'Spider-Man', 0, CAST(0x86270B00 AS Date))

INSERT [dbo].[tblMovie] ([MovieId], [MovieName], [Oscars], [ReleaseDate]) VALUES (3, N'King Kong', 3, CAST(0x862C0B00 AS Date))

INSERT [dbo].[tblMovie] ([MovieId], [MovieName], [Oscars], [ReleaseDate]) VALUES (5, N'Superman Returns', 0, CAST(0x5B2D0B00 AS Date))

INSERT [dbo].[tblMovie] ([MovieId], [MovieName], [Oscars], [ReleaseDate]) VALUES (6, N'Titanic', 11, CAST(0x43210B00 AS Date))

INSERT [dbo].[tblMovie] ([MovieId], [MovieName], [Oscars], [ReleaseDate]) VALUES (7, N'Evan Almighty', 0, CAST(0xDA2E0B00 AS Date))

INSERT [dbo].[tblMovie] ([MovieId], [MovieName], [Oscars], [ReleaseDate]) VALUES (8, N'Waterworld', 0, CAST(0xC31D0B00 AS Date))

INSERT [dbo].[tblMovie] ([MovieId], [MovieName], [Oscars], [ReleaseDate]) VALUES (9, N'Pearl Harbor', 1, CAST(0x0C260B00 AS Date))

Our Example Web Page

I've tried to keep this as simple as I possibly can!  The web page asks you to choose one of the films:

Choosing a movie by name

We'll apply a criteria to restrict the films to those winning at least 1 Oscar.


When you make your choice, you can then see this film's details:

The details for the chosen film

The details for the chosen film.


A Solution using Wizards

One way to solve this would be to use data sources.  For example, for the dropdownlist you could assign it to a data source:

Choosing a dropdownlist data source

Having added a dropdownlist, you can assign a data source to it.


You would end up with something like this:

The data source in HTML

The SELECT statement for the data source is contained within the HTML of the page.

Wizards are great.  They show you how to do really difficult things - but then they stop, and you're left scratching your head wondering how to tweak what they've left behind.  I have regretted using more wizards than Hogwarts has fed hot dinners to.  If you're developing anything more than a simple website, we'd recommend using data classes instead.

And with that justification out of the way, let's look at how to create data classes, beginning with how to store and retrieve connection strings.

This blog has 0 threads Add post