Creating data classes for ASP.NET data controls
Part four 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 (this blog)
  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 ASP.NET training or 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.

ADO.NET: DataSets, DataTables and Data Adapters

ADO.NET is the technology used within .NET to connect to databases (if you're interested, ADO originally stood for ActiveX Data Objects - but you're probably not).

You can actually get away without understanding any of this page - just copy and paste the sample code towards the end of this blog - but I think a little theory is sometimes useful!

Referencing Namespaces

Your code is going to be making a lot of use of DataTables, DataRows and DataSets.  These all belong in the System.Data namespace.  It's a bit tedious to have to type this in every time, so it makes sense to reference this within our code files.  Here's how to do this in C#:

using System;

// allow use of DataTables, DataRows, etc.

using System.Data;

// allow use of SQL Server objects

using System.Data.SqlClient;

Here's the VB equivalent:

Imports System

'allow use of DataTables, DataRows, etc.

Imports System.Data

'allow use of SQL Server objects

Imports System.Data.SqlClient

I've also referenced here the System.Data.SqlClient namespace, so that we can reference objects like SqlCommand without having to fully qualify them.

DataSets, DataTables and DataAdapters

We start out with a table stored in SQL Server (or any other database, for that matter):

The SQL Server table

We want to get rows from this table into memory in our website.

 

To do this, we'll create a DataSet in memory within VB or C#, consisting of one or more DataTables:

A dataset and its datatables

A DataSet can contain lots of tables - although ours will only contain one.

 

To get the rows of data from the SQL Server table we'll use something called a DataAdapter, which is basically a set of SQL statements designed to select, insert, update and delete data.  Here's how this works:

DataAdapter linking tables

We'll use a DataAdapter to fill a table in a DataSet, thus transferring data from the underlying database's tblMovies table into a table of records in memory.

 

And that's about all that you need to know about ADO.NET to create data classes!

This blog has 0 threads Add post