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

Storing and Retrieving Connection Strings

A connection string tells your web page three things:

  • Where to find the database you're trying to connect to;
  • What format this database is in; and
  • What it's called.

Storing Connection Strings

In ASP.NET 4.0 the best place to store them (as recommended by Microsoft) is in the connectionStrings section of the web.config file.  Here's an example:

ConnectionStrings in web.config

Here there is one connection string stored in the web.config file.

This connection string will connect to the SQL Server database called Movies on the server called XXX, using Windows authentication.

For everything you could conceivably want to know about connection strings, go to  The tagline is irresistible: "Forgot that connection string? Get it here!".

Getting at Connection Strings in Code

The modern way to retrieve a connection string from a web.config file is via a ConfigurationManager object.  This is typical .NET gobbledegook; all you need to know is that the following code will give you the connection string called csMovies in your web.config file:


Later in this blog we'll use this to get the connection string to use to access data for our web page.

Having learnt how to get at connection strings to connect to a database, the next thing is to understand about data tables, datasets and data adapters.

This blog has 0 threads Add post