BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
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.
- Data Classes in ASP.NET
- Our Example for this Data Classes Tutorial
- Storing and Retrieving Connection Strings
- ADO.NET: DataSets, DataTables and Data Adapters
- Consuming our Data Classes (this blog)
- A Class to Run a Stored Procedure and Return its Rows
- Consuming our Data Class on the Web Page
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.
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).
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](
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)
// 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
// 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
'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
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!