Sunday, August 26, 2012

How to get Country list without using Database in C#?

 Do you know we can get Country List without using Database! Here i am showing how to get Country list by simply using the CultureInfo class .

This function ca be use for returning Country List by an region like here i used "de-DE" for Germany. for that you have to bake this function parametrized function by passing a string as regional code

/// Function Name: FillCountries
        /// Bishnu Tewary
        /// 01-02-2012
        /// <summary>
        /// This methode will set _countryList with country name in Germany
        /// </summary>
        private void FillCountries( )
        {
            this._countryList = new List<Country>();
            try
            {
                foreach (CultureInfo objCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
                {
                    ////de-DE is for German and NativeName will return countryname in Germany
                    RegionInfo objRegionInfo = new RegionInfo("de-DE");
                    Country objCountry = new Country { CountryName = objRegionInfo.NativeName, CountryCode = objRegionInfo.TwoLetterISORegionName };
                    if (!this._countryList.Contains(objCountry))
                    {
                        this._countryList.Add(objCountry);
                    }
                }
            ////Sort this._countryList in ascending order
                this._countryList = (from c in this._countryList orderby c.CountryName ascending select c).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }