Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Displaying an empty value in a combo box in a C# Windows application using MS Access

Rate me:
Please Sign up or sign in to vote.
1.87/5 (10 votes)
19 Oct 2006CPOL2 min read 81.3K   561   18   6
Display an empty value in a combo box within a C# Windows application using a SQL query.

Sample image

Introduction

Welcome to my very first article in CodeProject!

This is a very simple C# Windows application that explains how to display a combo box without showing the first value in the database but an empty value.

I was working on a Windows application where I needed to display a combo box that would load with an empty field instead of displaying the first value in the table. I needed to do this with out physically inserting an empty field into the table that populates the combo box, which is bad practice.

Unfortunately, as I'm still learning, I was unable to find a good source on the internet that was able to help me. I can only assume this was because I probably did not type a keyword, or the article did not really deliver the information that I needed. I'm hoping my article will help those who are also searching for ways to modify the way their combo box displays.

Background

I have created a very simple Windows Form with two combo boxes on it. The reason I put two combo boxes was to better show the difference between a combo box with an empty field and one without. As you can see from the image above, the combo box on the right contains an empty field when the form loads. (I will focus on the combo box on the right in my article.)

I've developed this little application using a three layer model. (Not really necessary for this demonstration, but I thought it would make the flow of the program a little clearer.) When the form loads, the combo box populates by calling the GetCountries2 method in the Countries class. This method contains a SQL query that is passed into the DataAccessLayer object and executed by a method within the DataAccess class.

Code

Populating the combo box with an empty value is done by the GetCountries2 method:

C#
public static DataTable GetCountries2()
{
    try
    {
          DataAccess.OpenDatabaseConnection();
          string strSQLCountries;
          strSQLCountries = "SELECT Countries.[Common Name] ";
          strSQLCountries += "FROM Countries";
          strSQLCountries += " UNION SELECT '' FROM [Countries] WHERE [Countries].[ID] = 1 ";
          DataTable dtCountries;
          dtCountries = DataAccess.ExecuteDataTable(strSQLCountries);
          return dtCountries;
    }
    catch(Exception ex)
    {
        throw(ex);
    }
    finally
    {
        DataAccess.CloseDatabaseConnection();
    }
}

The SQL query starts off by getting all the countries in the Countries table.

SQL
SELECT Countries.[Common Name] FROM Countries

The next part of the SQL query simply selects (or sets) an empty string where the ID is equal to 1. This adds both results: a dataset with all the countries, and a dataset with an empty string added.

SQL
UNION SELECT '' FROM [Countries] WHERE [Countries].[ID] = 1

Setting up using the source files

  1. Unzip the files into a specific location.
  2. You will need to change the path of the database in the App.config file before you can run the application.
  3. Compile the DataAccessLayer object and then ClassObjectlayer before finally compiling the solution file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAlternative solution Pin
Member 82912274-Apr-12 19:46
Member 82912274-Apr-12 19:46 
GeneralMy vote of 1 Pin
cnunezm18025-Dec-08 8:33
cnunezm18025-Dec-08 8:33 
GeneralItems.Insert Pin
dwatkins@dirq.net20-Oct-06 3:34
dwatkins@dirq.net20-Oct-06 3:34 
AnswerRe: Items.Insert Pin
dwatkins@dirq.net23-Oct-06 3:52
dwatkins@dirq.net23-Oct-06 3:52 
GeneralRe: Items.Insert Pin
Gh0stman27-Oct-06 3:46
Gh0stman27-Oct-06 3:46 
GeneralYou might want to... Pin
Colin Angus Mackay19-Oct-06 22:44
Colin Angus Mackay19-Oct-06 22:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.