Click here to Skip to main content
6,632,253 members and growing! (18,987 online)
Email Password   helpLost your password?
Desktop Development » Combo & List Boxes » General     Intermediate License: The Code Project Open License (CPOL)

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

By Johann Lazarus

Display an empty value in a combo box within a C# Windows application using an SQL query
C# 1.0, Windows, .NET 1.1VS.NET2003, Dev
Posted:19 Oct 2006
Views:32,790
Bookmarked:14 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 1.58 Rating: 1.87 out of 5
6 votes, 85.7%
1

2
1 vote, 14.3%
3

4

5

Sample image

Introduction

Welcome to my very first article in Code Project!

This is a very simple C# windows application that explains of 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 and 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 key word or the article did not really deliver the information that I needed. I'm hoping my article will help those who are also searching of 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 an 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.

The GetCountries2 method:

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.

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 and empty string together.

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 the 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)

About the Author

Johann Lazarus


Member
Johann graduated in 2002 from Massey University in Auckland, New Zealand with a Bachelor of Information Science degree.
Soon after graduating Johann moved overseas to Dubai, United Arab Emirates where he worked for three and a half years as an Applications Developer for Siemens LLC, a German multinational
company looking after various software systems as well developing and customising Microsoft based applications.

In 2006 Johann returned to Auckland where he spent 3 years working as an Analyst Programmer for Augen Software Group working in both the .NET Windows and Web space.

In 2009 Johann spent 3 months contracting to Rio Tinto leading a junior development team based out of the UK developing and customising web applications in ASP.NET as well as developing
applications in WPF.

Johann joined Visionstream NZ Ltd in late 2009 assisting the Visionstream transition team with the implementation and roll out of one of Visionstream's key web applications as well as maintaining and optimizing databases and customising and enhancing various web and windows applications.

Specialities:

Project Management; budget preparation, requirements analysis, risk analysis, project progress reporting, client relationship management.

Systems analysis and development; .NET web and windows development. Database development.

Microsoft Technologies (Sharepoint, SQL Server, Visual Studio .NET), Microsoft Development Technologies (.NET, .NET Compact Framework, C#, ASP.NET, WPF, Windows Services), Crystal Reports 9/10, DotNetNuke, Oracle 8
Occupation: Software Developer
Company: Visionstream NZ Ltd
Location: New Zealand New Zealand

Other popular Combo & List Boxes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralMy vote of 1 Pinmembercnunezm18029:33 5 Dec '08  
GeneralItems.Insert Pinmemberdrkwtkns4:34 20 Oct '06  
AnswerRe: Items.Insert Pinmemberdrkwtkns4:52 23 Oct '06  
GeneralRe: Items.Insert PinmemberGh0stman4:46 27 Oct '06  
GeneralYou might want to... PinmemberColin Angus Mackay23:44 19 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Oct 2006
Editor:
Copyright 2006 by Johann Lazarus
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project