ASP.NET: Get and bind all countries to Dropdownlist using Globalization - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

ASP.NET: Get and bind all countries to Dropdownlist using Globalization

In this article I am going to explain how to get all countries list using system.globalization and bind to dropdownlist in using C# and

System.Globalization namespace contains classes that define culture-related information, including language, country/region, calendars in use, and format patterns for dates, currency, and numbers, and sort order for strings.

HTML Markup:
  <fieldset style="width:50%">
    <legend>Bind Dropdownlist using System.Globalization</legend>
    Select Country:<asp:DropDownList ID="ddlcountry" runat="server"> </asp:DropDownList>

First of all import the namespace
C# code:
using System.Globalization; Code:
Imports System.Globalization

Get countries list
Create method to get countries list and call the method page load.
C# code:
protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

public void GetAllCountry()
            List<string> listcountry = new List<string>();
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

            foreach (CultureInfo cinfo in cultures)
                RegionInfo Rinfo = new RegionInfo(cinfo.LCID);
                if (!listcountry.Contains(Rinfo.EnglishName))
            ddlcountry.DataSource = listcountry;
            ddlcountry.Items.Insert(0, new ListItem("--Select Country--", "0"));
        catch (Exception ex)
    } Code:
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub

Public Sub GetAllCountry()
            Dim listcountry As New List(Of String)()
            Dim cultures As CultureInfo() = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
            For Each cinfo As CultureInfo In cultures
                Dim Rinfo As New RegionInfo(cinfo.LCID)
                If Not listcountry.Contains(Rinfo.EnglishName) Then
                End If
            ddlcountry.DataSource = listcountry
            ddlcountry.Items.Insert(0, New ListItem("--Select Country--", "0"))
        Catch ex As Exception
        End Try
    End Sub

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.