Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Bind All Countries to Dropdownlist in Asp.net using System.Globalization in C#, VB.NET

Aug 4, 2015
Introduction

Here I will explain how to bind all countries to dropdownlist in
asp.net using c#, vb.net with example or get all countries or display countries in dropdownlist in asp.net using c#, vb.net. By using System.Globalization namespace we can get all countries list in asp.net using c#, vb.net.

Description:
  
In previous articles I explained Angularjs get dropdownlist selected value & text, display gridview based on dropdownlist selection in asp.net, bind dropdownlist in gridview in asp.net,
gridview examples in asp.net and many articles relating to dropdownlist, gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to bind all countries to dropdownlist in asp.net using c#, vb.net with example.

To get all countries in asp.net first open your aspx page and write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>binding all countries to dropdownlist in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Select Country:</b><asp:DropDownList ID="ddlCountries" runat="server"/>
</div>
</form>
</body>
</html>

Now add following namespaces in code behind

C# Code


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

After completion of adding namespaces you need to write the code like as shown below


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> objcountries = new List<string>();
CultureInfo[] objculture = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo getculture in objculture)
{
RegionInfo objregion = new RegionInfo(getculture.LCID);
if (!(objcountries.Contains(objregion.EnglishName)))
{
objcountries.Add(objregion.EnglishName);
}
}
objcountries.Sort();
ddlCountries.DataSource = objcountries;
ddlCountries.DataBind();
}
}

VB.NET Code


Imports System.Web
Imports System.Globalization
Imports System.Collections.Generic
Imports System.Web.UI.WebControls
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim objcountries As New List(Of String)()
Dim objculture As CultureInfo() = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
For Each getculture As CultureInfo In objculture
Dim objregion As New RegionInfo(getculture.LCID)
If Not (objcountries.Contains(objregion.EnglishName)) Then
objcountries.Add(objregion.EnglishName)
End If
Next
objcountries.Sort()
ddlCountries.DataSource = objcountries
ddlCountries.DataBind()
End If
End Sub
End Class

Demo

Bind All Countries to Dropdownlist in Asp.net using System.Globalization in C#, VB.NET
Download Sample Code Attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

Unknown said...

Nice sir

Unknown said...

Thanks for this post.
It is really informative one. Get law essay writing service help.

Anonymous said...

Why it doesn't include 'Myanmar' ?

Anonymous said...

Excellent work. I was finding the same in different sites , but yours only fulfilled my requirement.

Unknown said...

Could anyone tell how to store the selected country in MS Sql server?

Unknown said...

hello can anyone tell me the code in above code to show select as first option in list item not a country name as first list item.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.