Categories
PHP
Javascript
MySQL
C#
VB
VB.NET
ASP.NET
Regex
Packaging & compression
General Web Tech
Tech Speak


Google


This website looks best on firefox.
 
Resource Center : ASP.NET : <Adding a Country List combo box/Dropdown list in ASP.NET>

Adding a Country List combo box/Dropdown list in ASP.NET

Posted by: Floresense Team
Downloads
countrylist.csv 32.5 KB
Pages: 1  

In webforms that involve address or contact details entry, we commonly put a Combobox / dropdown list of countries which the users choose from. And in the backend, we store a two-letter or three-letter ISO standard country code instead of storing the full name of the country. This is to keep with standards, and to eliminate storing constants like country name in every record which eventually might not be considered good implementation.

In this article, we will implement such a DropDownList box for an ASP.NET page.
Though C# is used as coding language, the steps are same for any other programming language.

STEP 1: Download / create a CSV country list to use.

This could be the difficult part especially with many different possible country lists on the Net.

But, Andrew Patton makes it easy for us, for he has researched different sources and compiled a list of countries with country code, country's official currency, the TLD subdomain allotted for the country etc., I should say Andrew Patton's list is very comprehensive, and he further posts on his website an XML and a CSV version of the list. You could download the CSV / XML versions from here.

Below code uses a reduced CSV list of countries from Andrew Patton's list.. for I have removed the disputed territories/countries from the list, and also shortened the names of a few countries to keep the width of the final DropDownList within optimal size. You can download my copy of the CSV list from the top of this article.

STEP 2: Get a connection string and set its properties

We will use ADO.NET to open the CSV file as a Table and read data.. this helps do a sql query to extract data.. What not, later you could do a join on this and other text or CSV files with normal sql queries.

The connection string we will use is: (source: connectionStrings.com)

Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:txtFilesFolder;
Extended Properties="text;
HDR=Yes;FMT=Delimited";


Data Source, should point to the folder which contains the txt or csv file. Yes file extensions don't matter. Any file could would be tried and read as a text file.
HDR=Yes, says that first row of the file contains Header information. If 'No', then you will have to do with column indexes in your code instead of column names.
FMT=Delimited, says that format of the files in the folder is 'delimited text'...whether Comma delimited or Tab delimited is determined based on your server's settings(default is Comma if extension is CSV), or, based on a Schema.ini file (if present) in the same folder.

Schema.ini

As mentioned above, usually the server defaults to comma delimited format if we use a .csv file or, defaults to tab delimited format for any other file.

To control this, or explicitly mention the properties of each file in the textFiles folder, you can create a Schema.ini file like below and put it in the same folder (here.. its textFiles folder).

Sample schema.ini file for our use:
[countrylist.csv]
Format=CSVDelimited
ColNameHeader=True
MaxScanRows=0
CharacterSet=ANSI


STEP 3: Extract the data from the file

(Explanation below code)

public static DataSet getCountryList()
{
DataSet tmpDS = new DataSet();
OleDbConnection tmpCon = null;
try
{
tmpCon = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source="+
"c:temptxtFiles;"+
"Extended Properties="text;
HDR=Yes;FMT=Delimited";");

tmpCon.Open();
OleDbDataAdapter tmpDA =
new OleDbDataAdapter("
select * from countrylist.csv",
tmpCon);
tmpDA.Fill(tmpDS);
}
catch(Exception ex)
{}
finally
{
tmpCon.Close();
tmpCon=null;
}
return tmpDS;
}

All that we do to implement the read operation is:

1. Open the folder as a ADO.NET oleDBconnection (remember to import the System.Data.OleDb namespace at the top of your code like "using System.Data.OleDb;")

2. Perform a query on the file we are interested in, and extract the data to a DataSet.

The code above is a standard operation for extracting any database query result to a DataSet. Nothing new as you see. So this implementation could be used for working with both comma or tab delimited text files, and do joins on the files, all without the use of any database engine.

As you can see, we are done implementing the extraction part.. We now, only have to bind the country data to a Combobox or in to a ASP.NET DropDownList control.

STEP 4: Bind the data to an ASP.NET DropDownList control

The sample source to do this binding is as below.
ddlCountryList is the ID of the DropDownList control on my ASP.NET page.

private void loadCountryList()
{
DataSet tmpDS = Data.getCountryList();
if (tmpDS.Tables.Count == 1)
{
DataView tmpVw =
tmpDS.Tables[0].DefaultView;
tmpVw.Sort = "Common Name";

ddlCountryList.DataSource =
tmpVw;
ddlCountryList.DataTextField =
"Common Name";
ddlCountryList.DataValueField =
"ISO 3166-1 2 Letter Code";
ddlCountryList.DataBind();
}
}


We are done.. Test the code..

References:


Andrew Patton: country list in csv / xml formats
Texfile connection strings - ConnectionStrings.com
Fabrice's weblog: Manipulating CSV files
Pages: 1  

Advertisement

2005 - 2008 © Floresense.com