Using SQL UNION queries to populate the first Item in a list type control
Part one of a two-part series of blogs

Adding a first or default item to a list control which gets its data from a table can be tricky. This blog gives an elegant solution using UNION queries, which works equally well for ASP.NET dropdown controls, Windows Forms dropdowns or Access combo boxes.

  1. Set the first item in a list type control (this blog)
  2. Creating the Union Query for a List Type Control

Posted by David Wakefield on 31 May 2011

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.

Set the first item in a list type control

On some controls (such as dropdowns in .NET, or combo or listboxes in Access) you may want to list data from a table, but you also want to see a sensible default value at the top.

List control with default values

The data in the underlying table does not contain "CHOOSE A SINGER" as a name, and nor should it.

Table of data with values

Some list controls may allow you to set an initial value for the control.  If the control allows you to do this then do so, and ignore the rest of this blog entry!

Drop down list with default value

An ASP.NET List control can get its data from ListItems and a datasource as long as you set AppendDataBoundItems=True

If you are working with a list control which does not allow initial values - such as an Access combobox - and you don't want to add unnecessary data in to your table, consider using an SQL Union Query as the datasource for your control, as described below.

  1. Set the first item in a list type control (this blog)
  2. Creating the Union Query for a List Type Control
This blog has 0 threads Add post