web 2.0

Get Countries Name in .Net

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.

Comments

DotNetShoutout , on 7/12/2009 10:51:27 PM Said:

trackback

Get Countries Name in .Net

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetKicks.com , on 7/12/2009 10:58:28 PM Said:

trackback

Get Countries Name in .Net

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Sobin India, on 7/12/2009 11:11:53 PM Said:

Sobin

Good stuff ... thanks a lot for this piece of information  

thx Poland, on 7/13/2009 12:31:28 AM Said:

thx

excellent snippet, thx

thx Poland, on 7/13/2009 12:36:02 AM Said:

thx

RSS on your site is broken, can you fix it? I would like to subscribe your blog, thx and Greetings!

argg United States, on 7/14/2009 6:41:19 AM Said:

argg

Saves me an extra step, thanks!

Guy United Kingdom, on 7/14/2009 10:20:59 AM Said:

Guy

Very useful snippet.

thanks

DotNetBurner - .net Framework , on 7/14/2009 12:01:53 PM Said:

trackback

Get Countries Name in .Net

DotNetBurner - burning hot .net content

Yoann B. France, on 7/14/2009 8:17:27 PM Said:

Yoann B.

Great post !

Thanks.

Jobi United States, on 7/14/2009 8:40:11 PM Said:

Jobi

Very interesting. Nice to see that.

Cherian India, on 7/14/2009 8:43:48 PM Said:

Cherian

use System.Collections.Generic.SortedDictionary<TKey,TValue> instead of the sort...

khalid United States, on 7/15/2009 5:57:14 AM Said:

khalid

very helpful snippet.

Agha Usman Islamic Republic of Pakistan, on 7/15/2009 8:42:00 PM Said:

Agha Usman

@thx
Thanks for the interest, RSS has been fixed now. You can now subscribe.

@Cherian
Thanks for the suggestion, that would definitely skip out the LinQ Stuff from code. Thanks mate

Joakim Westin Sweden, on 7/17/2009 9:16:56 AM Said:

Joakim Westin

Thanks for a wellw ritten post.

This is the type of functionality that most people donät understand is available in the NET Frameowkr.

But it also makes you wonder why Microsoft havent added a <asp:CountryList xxx /> control in ASP.NET.

Anyway, thanks for the great writeup!

Ross Hawkins , on 7/29/2009 1:42:50 AM Said:

trackback

A short collection of links

A short collection of links

Junaid Islamic Republic of Pakistan, on 8/6/2009 2:37:16 AM Said:

Junaid

That's really informative
Thanks

Nick United States, on 8/22/2009 9:19:35 PM Said:

Nick

I like your findings. I LINQ'd it up a bit, and present you with the following. It runs about 3x faster for a single execution:
            
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

            var countries = (from r in
                                    from c in cultures
                                    select new RegionInfo(c.LCID)
                                orderby r.EnglishName ascending    
                                select new ListItem (r.EnglishName, r.TwoLetterISORegionName))
                             .Distinct();
                                  
            foreach (ListItem countryItem in countries)
            {
                CountryList.Items.Add(countryItem);
            }

Johnny Peru, on 10/17/2009 8:47:56 AM Said:

Johnny

Hi Agha Usman!!

Thank you very much for your contribution, is really excellent, and even more by using LINQ, Yeahhhh!!! Laughing

Bytes!!

Johnny B.

Andrei Rinea Romania, on 11/5/2009 8:08:08 AM Said:

Andrei Rinea

Excellent! Thank you.

kamini India, on 12/24/2009 10:09:49 PM Said:

kamini

hello ,

in datarepeater control is it posible like repeate column propery of repeater control in asp.net.my application is window base and want to display image list in 3 column

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading