Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all countries in comboBox

I want to fill the ComboBox with all the country names list by using

Using System.Globalization
Using System.Collections

here is my XAML coding for the ComboBox

<ComboBox x:Name="countryComboBox" Width="300"/>

In the C# coding I have add button. Here is that code, and I want to get the country from the ComboBox and store it in the MySql database. How can I do that?

private void addButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        string Query = @"INSERT INTO `bcasdb`.`tbl_student`
                         (`reg_id`,
                          `std_fname`,
                          `std_lname`,
                          `tbl_batch_batch_id`,
                          `gender`) 
                         VALUES (@regId, @fName, @lName, @bID, @gender, @country)";
        //This is command class which will handle the query and connection object.
        MySqlConnection conn = new MySqlConnection(BCASApp.DataModel.DB_CON.connection);
        MySqlCommand cmd = new MySqlCommand(Query, conn);
        conn.Open();
        cmd.Parameters.AddWithValue("@regId", this.regIDInput.Text);
        cmd.Parameters.AddWithValue("@fName", this.fnameInput.Text);
        cmd.Parameters.AddWithValue("@lName", this.lnameInput.Text);
        cmd.Parameters.AddWithValue("@bID", this.batchIDInput.Text);
        cmd.Parameters.AddWithValue("@gender",this.maleRadioButton);
        cmd.ExecuteNonQuery();
        conn.Close();
        successmsgBox();
    }
    catch (Exception)
    {
        errormsgBox();
    }
}

2 Answers

This method will return you ordered list of all countries:

public static List<string> GetAllCountrysNames()
{
    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

    return cultures
            .Select(cult => (new RegionInfo(cult.LCID)).DisplayName)
            .Distinct()
            .OrderBy(q => q) 
            .ToList();
}

Also you can replace DisplayName to EnglishName if you need.


countryComboBox.Items.AddRange(GetAllCountrysNames());
like image 159
Andrew Avatar answered Feb 04 '26 18:02

Andrew


You can fill the Combobox like this,

add using System.Globalization

Set the Combobox ItemsSource like this,

countryComboBox.ItemsSource = GetCountryList();

public static List<string> GetCountryList()
{
    List<string> cultureList = new List<string>();

    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

    foreach (CultureInfo culture in cultures)
    {
        RegionInfo region = new RegionInfo(culture.LCID);

        if (!(cultureList.Contains(region.EnglishName)))
        {
            cultureList.Add(region.EnglishName);
        }
    }
    return cultureList;
}

You can just get the selectedItem and pass it with the query,

cmd.Parameters.AddWithValue("@country", this.countryComboBox.SelectedItem.ToString());

EDIT: You have mentioned it is for Windows Store App, Check this link.How to load countryes

Replacement for CultureInfo.GetCultures in .NET Windows Store apps

If not, you have to write a custom way to load the countries from the database.

like image 25
Sajeetharan Avatar answered Feb 04 '26 16:02

Sajeetharan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!