Geeks With Blogs

News

Agha Usman Ahmed JILAWATAN

Introduction:

In this post, I will explain you how can we get the countries name filled in any collection using .net without using any database.

It is a regular task, which we all as developers did some past day but the difference is we used database table or xml file to hold the country names. But .net framework provide us with all the countries information in Globalization namespace.

So, here is the code for that

Dictionary<string,string> objDic = new Dictionary<string,string>();
 
foreach (CultureInfo ObjCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
    RegionInfo objRegionInfo = new RegionInfo(ObjCultureInfo.Name);
    if (!objDic.ContainsKey(objRegionInfo.EnglishName))
    {
        objDic.Add(objRegionInfo.EnglishName, objRegionInfo.TwoLetterISORegionName.ToLower());
    }
}
 
var obj = objDic.OrderBy(p => p.Key );
foreach (KeyValuePair<string,string> val in obj)
{
    ddlCountries.Items.Add(new ListItem(val.Key, val.Value));
}

 

Explanation:

Notice that, we have used typed dictionary object to store the name and the values of the countries.

Then, we use CultureInfo.GetCultures to get the cultural information of the countries.

Later on, we use RegionInfo to get the regional information of that  culture.

Since, there can be multiple cultures of the same country that is why there is a condition which check either the country is already added in dictionary. If not, then simply add the country name and country two letter name. (Note : We are treating the two letter country name as the value)

After the loop, I used some LinQ stuff to sort county names, and then iterate through the returned object to add the values in drop down list.

That’s it. Now you are not only limited to show the English name of the country but you can also show the native name. For example, the name of my country in English is “Islamic Republic of Pakistan” but the native name is ???????.

Also, you can get the following country information using RegionInfo

 

sc_clbn_1

Some developers are habitual of using country id along with the country name. if they still want to use some id to save the country information they can use the GeoId property of the RegionInfo.

Posted on Sunday, July 26, 2009 1:43 AM Asp.net , .net | Back to top


Comments on this post: Get Countries Name in .Net

# re: Get Countries Name in .Net
Requesting Gravatar...
Whilst this will get some countries, it does not return all. For example Andorra (AD, AND, 020) is not included. So I recommend you continue to use a lookup file from ISO 3166-1.
Left by Allan on Jul 28, 2009 1:49 PM

# re: Get Countries Name in .Net
Requesting Gravatar...
Hi Agha Usman Ahmed,

It is a nice post. Just want to add two things.

1. Why not just put dictionary as data source and bind it.

2. For returning from a function dictionary can be returned as IQueryable.

thanks

Yawer
Left by Yawer Iqbal Qadri on Apr 01, 2011 7:36 PM

# re: Get Countries Name in .Net
Requesting Gravatar...
Really nice, just what I had in mind, except that I didn't think of the sorting first...
One thing: why didn't you use the TwoLetterISORegionName or even the GeoID as key for the dictionary? That would be faster to check for the Contains() call and in case of the TwoLetterISORegionName the key you would normally query for (as it is what's stored in a database field and needs to be looked up...). Then you could store the DisplayName as the Value of the Dictionary and you would get localized items in your drop down list automatically. Sorting the items could be done at the end with ddlCountries.Sorted = true;

Best regards, Max
Left by Max on Nov 15, 2011 11:05 PM

# re: Get Countries Name in .Net
Requesting Gravatar...
Using EnglishName as key you get two entries for ISO-2 PH: 'Philippines' & 'Republic of the Philippines'
Left by Max on Nov 16, 2011 12:34 AM

# re: Get Countries Name in .Net
Requesting Gravatar...
Excellent post. Very simple and very useful. Thanks very much.
Left by Bruce on Mar 15, 2012 9:36 PM

# re: Get Countries Name in .Net
Requesting Gravatar...
Great code indeed, but the only problem is that it does not return all the country names. Lesotho is not there on the list, Zambia, Uganda is not there, Angola, the DRC is just a lot of countries that i picked up at a glance.

The code you used returns the same results the one i have used, pasted below

public List<string> GetCoutries()
{
List<string> list = new List<string>();

foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
RegionInfo inforeg = new RegionInfo(info.LCID);
if (!list.Contains(inforeg.EnglishName))
{
list.Add(inforeg.EnglishName);
list.Sort();
}
}
return list;
}
Left by Teboho Makibile on Jul 17, 2014 11:13 PM

# re: Get Countries Name in .Net
Requesting Gravatar...
This code is really helpful while scripting where datatable is not a requirement. It worked perfect for me . Thanks for this post.
Left by Anjali on May 14, 2015 7:55 PM

Your comment:
 (will show your gravatar)


Copyright © Agha Usman Ahmed | Powered by: GeeksWithBlogs.net