Try to search your question here, if you can't find : Ask Any Question Now ?

How can my combobox display a list of names, but use a unique ID associated with that name when the name is selected?

HomeCategory: stackoverflowHow can my combobox display a list of names, but use a unique ID associated with that name when the name is selected?
Avatargaurav asked 5 months ago

My Application uses a combobox which is filled with a of list names returned from an API. The API also returns an ID associated with each name. How can I have the combobox display the name, but when a user selects a name, the ID associated with that name is stored in a variable, or used to do something? I can get the combobox to fill with the names, but the names are not of any actual use, only for display purpose. I don’t know how to make the combobox display the name, but use the ID as its value.

Here is the json returned by the API.

[
{
    "BookId": "13a7dc2b-80da-4407-bd4d-806d7f4adc3b",
    "BookName": "Reckon API",
    "Bookstatus": 3,
    "Country": "AU"
},
{
    "BookId": "32c03594-1ecb-4f97-8453-5b28a03d26d9",
    "BookName": "Payroll Book",
    "Bookstatus": 3,
    "Country": "AU"
},
{
    "BookId": "51d16696-b98a-4b3b-ac67-f36559cff70b",
    "BookName": "Accounting Book",
    "Bookstatus": 3,
    "Country": "AU"
}
]

Here is the code I’ve used to fill the combobox with the names:

using (HttpClient httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            var response = httpClient.GetAsync("https://api.reckon.com/R1/cashbooks").Result;
            string responseText = response.Content.ReadAsStringAsync().Result;
            JArray parsedArray = JArray.Parse(responseText);
            foreach (JObject parsedObject in parsedArray.Children<JObject>())
            {
                foreach (var parsedProperty in parsedObject.Properties())
                {
                    if (parsedProperty.Name == "BookName")
                    {
                        comboBox2.Items.Add(parsedProperty.Value);
                    }
                }
            }
        }

I have a MYSQL database that I’ve stored my list of names and ID’s in thinking I could use the databsae as the datasource, the display member as the name and value member as the id, but when I run the application the combobox doesn’t populate but I have no idea why. It really doesn’t matter to me how I accomplish this task, I just need to find a way.

EDIT:

I’ve found this postthat I thought may help.: How to set mySQL data source for combo box (C#)

And added this code to my application:

using (MySqlConnection mySqlConnection = new MySqlConnection())
        {
            mySqlConnection.ConnectionString = "server=localhost;userid=root;password=;database=reckon api sample app dev;SslMode=none";
            mySqlConnection.Open();
            string sqlQuery = "SELECT BookId, BookName FROM cashbooks";
            MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sqlQuery, mySqlConnection);
            DataSet dataSet = new DataSet();
            mySqlDataAdapter.Fill(dataSet);
            comboBox2.DataSource = dataSet.Tables[0];
            comboBox2.DisplayMember = "BookName";
            comboBox2.ValueMember = "BookId";
        }

My combobox is now populating with the names which is great. When I select a name from the combobox, how do tell my application to get the id associated with that name and use it to do something? For example, I want to save the ID to a variable so I can use it in a future API call.

Thanks.

1 Answers
Best Answer
Avatarnaveen answered 5 months ago
Your Answer

0 + 16 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com