Skip to main content

Get country list in .net using CultureInfo

Get country list in .net using Culture Info


You have to create a web site.

  • Go to Visual Studio 2010
  • New-> Select a website application
  • Click OK
img5.gif
Now add a new page to the website.
  • Go to the Solution Explorer
  • Right-click on the Project name
  • Select add new item
  • Add new web page and give it a name
  • Click OK
img6.gif
Now drag and drop a DropDownList control from the Toolbox to the page. A DropDownList is used to show a list of the countries. Let's take a look at a practical example. The .aspx code will be as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Countrywithdropdown.aspx.cs"
    Inherits="Countrywithdropdown" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <span style="color:Black"><strong>Select Country :</strong></span>&nbsp;&nbsp;
        <asp:DropDownList ID="DropDownList1" runat="server" BackColor="Brown" ForeColor="#66FF66">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>
Add the following Namespace.
using System.Globalization;
CultureInfo class

The CultureInfo class contains culture-specific information, such as the language, country/region, calendar, and cultural conventions. The CultureInfo class specifies a unique name for each culture. The CultureInfo class specifies a unique name for each culture. 

GetCultures method

The GetCultures method retrieves a complete list of all cultures.
Now double-click on the page and write the following code to get a list of all countries and bind to a DropDownList.
public List<string> GetCountry()
    {
        List<string> list = new List<string>();
        CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures | CultureTypes.SpecificCultures);
        foreach (CultureInfo info in cultures)
        {
            RegionInfo info2 = new RegionInfo(info.LCID);
            if (!list.Contains(info2.EnglishName))
            {
                list.Add(info2.EnglishName);
            }
        }

        return list;
    }

Now call this GetCountry method on your page load that will bind and display countries in the DropDownList.

protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.DataSource = GetCountry();
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, "Select");
    }

In code-behind write the following complete code.
Code-behind

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

public partial class Countrywithdropdown : System.Web.UI.Page
{
    public List<string> GetCountry()
    {
        List<string> list = new List<string>();
        CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures | CultureTypes.SpecificCultures);
        foreach (CultureInfo info in cultures)
        {
            RegionInfo info2 = new RegionInfo(info.LCID);
            if (!list.Contains(info2.EnglishName))
            {
                list.Add(info2.EnglishName);
            }
        }

        return list;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.DataSource = GetCountry();
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, "Select");
    }
}

Now run the application and test it.

img1.gif

Now click on the DropDownList to show the list of all countries.


img2.gif

Comments

Popular posts from this blog

Creating package in Oracle Database using Toad For Oracle

What are Packages in Oracle Database A package is  a group   of procedures, functions,  variables   and  SQL statements   created as a single unit. It is used to store together related objects. A package has two parts, Package  Specification  and Package Body.

Resolving 'Setup Account Privileges' error while installing SQL Server

A new installation of Microsoft SQL Server 2012 or Microsoft SQL Server 2008 R2 fails You see the following error message when you try to install a new instance of SQL Server 2012 or SQL Server 2008 R2: Rule "Setup account privileges" failed.

Creating Oracle stored Procedures using TOAD for Oracle

In a database management system, a  stored procedure  is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database in compiled form so that it can be shared by a number of programs. The use of  stored procedures  can be helpful in controlling  access to data, preserving  data integrity  and  improving  productivity.