Skip to main content

Get All Coutries From System.Globalization Namespace | List All Countries In DropDownList

In this tutorial i am going to explain about how to get all coutries in the world from System.Globalization namespace or get all countries in asp.net and C#.Net or get all countries and bind it to the dropdownlist in asp.net.

In my previous article i have explained about Web Api Model Validation Using Validation Filters , Convert String To Upper,Lower & Title(Proper) Case Using TextInfo Class Build Products Comparision Table/Grid In ASP.Net , Store Custom Objects In Configuration File , Create Simple Mathmatical Captcha In ASP.Net , Visual Studio Keyboard Shortcuts and many articles in ASP.Net,C#.Net,VB.Net,Grid View,Javascript,jQuery,SQL Server and many other topics.

This tutorial will get the country list from the System.Globalization namespace and bind it to the first drop down list in the file. And then it will sort the countries and bind it to the second dropdownlist. Below is the aspx code i used.

ASPX Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="margin: 150px;">
            <h2>Random Country List</h2>
            <asp:DropDownList ID="ddlCountries" runat="server">
                <asp:ListItem Value="0">Select Country</asp:ListItem>
            </asp:DropDownList>

            <h2>Sorted Country List</h2>
            <asp:DropDownList ID="ddlSortedCountries" runat="server">
                <asp:ListItem Value="0">Select Country</asp:ListItem>
            </asp:DropDownList>
            Source: <a href="http://www.dotnetpickles.com" target="_blank">.Net Pickles</a>
        </div>
    </form>
</body>
</html>

Below is the code behind code.

C#.Net Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Declaring dictionary object to add coutries from the System.Globalization namespace
        Dictionary<string, string> objCountries = new Dictionary<string, string>();

        //Getting coutries from CultureInfo class and adding it to objCountries
        foreach (CultureInfo objCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
        {
            RegionInfo objRegionInfo = new RegionInfo(objCultureInfo.Name);
            if (!objCountries.ContainsKey(objRegionInfo.GeoId.ToString()))
            {
                objCountries.Add(objRegionInfo.GeoId.ToString(), objRegionInfo.EnglishName);
            }
        }

        //Looping dictionary object and binding it to drop down
        foreach (var key in objCountries.Keys)
        {
            ddlCountries.Items.Add(new ListItem(objCountries[key], objCountries[key]));
        }

        //Sorting the dictionary object based on value
        List<KeyValuePair<string, string>> mySortedList = objCountries.ToList();
        mySortedList.Sort(
            delegate(KeyValuePair<string, string> firstPair,
            KeyValuePair<string, string> nextPair)
            {
                return firstPair.Value.CompareTo(nextPair.Value);
            }
        );

        //Binding sorted dictionary object to dropdownlist
        foreach (KeyValuePair<string, string> objKeyValuePair in mySortedList)
        {
            ddlSortedCountries.Items.Add(new ListItem(objKeyValuePair.Value, objKeyValuePair.Key));
        }
    }
}

VB.Net Code:


Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Globalization
Imports System.Collections

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        ' Declaring dictionary object to add coutries from the System.Globalization namespace
        Dim objCountries As New Dictionary(Of String, String)()

        'Getting coutries from CultureInfo class and adding it to objCountries
        For Each objCultureInfo As CultureInfo In CultureInfo.GetCultures(CultureTypes.SpecificCultures)
            Dim objRegionInfo As New RegionInfo(objCultureInfo.Name)
            If Not objCountries.ContainsKey(objRegionInfo.GeoId.ToString()) Then
                objCountries.Add(objRegionInfo.GeoId.ToString(), objRegionInfo.EnglishName)
            End If
        Next

        'Looping dictionary object and binding it to drop down
        For Each key As var In objCountries.Keys
            ddlCountries.Items.Add(New ListItem(objCountries(key), objCountries(key)))
        Next

        'Sorting the dictionary object based on value
        Dim mySortedList As List(Of KeyValuePair(Of String, String)) = objCountries.ToList()
        mySortedList.Sort(Function(firstPair As KeyValuePair(Of String, String), nextPair As KeyValuePair(Of String, String)) firstPair.Value.CompareTo(nextPair.Value))

        'Binding sorted dictionary object to dropdownlist
        For Each objKeyValuePair As KeyValuePair(Of String, String) In mySortedList
            ddlSortedCountries.Items.Add(New ListItem(objKeyValuePair.Value, objKeyValuePair.Key))
        Next
    End Sub
End Class

If you run the application now then you will get the following output.

Un Sorted Country List:
Output 1 of Get All Coutries From System.Globalization Namespace | List All Countries In DropDownList
Sorted Country List:
Output 2 of Get All Coutries From System.Globalization Namespace | List All Countries In DropDownList
Source Code:
Sourcecode of Get All Coutries From System.Globalization Namespace | List All Countries In DropDownList
If you like this article then share with your friends and comment your valuable feedback.. Happy coding..

Comments

Popular posts from this blog

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     {         sb.Append( "Minus " );         inputNo = -inputNo;     }     string [] words0 = { "" , "One " ,

C# code to send mail using smtp from gmail,yahoo mail and live mail

Introduction: In my previous article I have explained about   How to bind/Unbind events in jQuery . In this article I am going to explain about how to send mail from ASP.Net using gmail,yahoomail and live mail credentials. Explanation: First Include the below namespaces in your code behind file. using System; using System.Net; using System.Net.Mail;

Geckofx Browser in Winform Application

Bored with IE browser in your winform application ? Want to do everything as you doing in your Firefox or Chrome Browser ? Play with automation ? Then here is your choice . Introduction:  GeckoFX is a Windows Forms control written in clean, commented C# that embeds the Mozilla Gecko browser control in any Windows Forms Application. It also contains a simple class model providing access to the HTML and CSS DOM . GeckoFX was originally created by Andrew Young for the fast-growing visual CSS editor, Stylizer . It is now released as open-source under the Mozilla Public License.  You can download it here :   Geckofx 22.0 And the supporting file Xulrunner here :   Xulrunner Files Hope you have downloaded above two files. Here our journey going to start. Create your winform application in visual studio and do the following: right click the toolbox -> Choose items -> Browse the "Geckofx-winforms.dll" and click "yes" for “Load it anyw