Click here to Skip to main content
15,568,681 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Can somebody help me please?

Binding a list into a ComboBox from a Database table with game names. When the user selected one of the list in the ComboBox then save after not storing only this data. Still empty this selected item.

The following link then the second Solution was helped me and followed how to bind list to ComboBox.

Fill ComboBox From Sql Server On C#[^]

What I have tried:

Bind a list to ComboBox from Database table:
C#
public static void BindComboBox(ComboBox comboBox_CauseOfGame)
...
comboBox_CauseOfGame.Items.Add(reader["Name"].ToString());
...


ComboBox click action:
C#
private void comboBox_CauseOfGame_Click(object sender, EventArgs e)
BindComboBox(comboBox_CauseOfGame);

int selectedIndex = comboBox_CauseOfGame.SelectedIndex;
Object selectedItem = comboBox_CauseOfGame.SelectedItem;



The User selected an item from the list then want to save to Database into another table:
C#
funGames = new funGames(..., (int)comboBox_CauseOfGame.SelectedIndex, ..., ...,
Posted
Updated 20-Nov-22 3:20am
v8
Comments
Graeme_Grant 20-Nov-22 6:44am    
What database? What have you tried? What error are you seeing? What are you trying to store? Remember, we can't see your code or your screen. Please update your question with more information.

When working with data, data is the key. The UI is just another method of accessing the data, just like reading and writing to a file or a database.
ernteSKY 20-Nov-22 6:47am    
What you mean what database? Database where store data and so on. I'm not well understand your question. Also you can see what I am tried at the bottom of the question. The error message you can read also at the top of my question.
Graeme_Grant 20-Nov-22 6:56am    
MS Sql, MySql, Maria DB, Access DB, SQLite, etc... every DB is slightly different. As for the error " Specified cast is not valid" where is this happening, and what line? The error is saying you are trying to use one variable type as another without casting.
ernteSKY 20-Nov-22 7:06am    
You right and now is clear! This one using the MS Sql and the error message come up at line of "(int)comboBox_CauseOfGame.SelectedItem". This one of the line collecting the data then send to the database manager for store into the database.

1 solution

C#
(int)comboBox_CauseOfGame.SelectedItem

This throws the error "Specified type conversion is invalid what is mean the Specified cast is not valid." because the value is converted to an integer is not of that type, is a string display name from:
C#
comboBox_CauseOfGame.Items.Add(reader["Name"].ToString());

You want the SelectedIndex which is an integer index of the SelectedItem. Read more here: ComboBox.SelectedItem Property (System.Windows.Forms) | Microsoft Learn[^]

UPDATED

This is fundamentals of a ComboBox, but I will make an example for you:
1. Create a new project
2. Add the following 4 controls:
C#
private ComboBox comboBox1;
private TextBox txtSelectedItem;
private TextBox txtSelectedIndex;
private TextBox txtCollectionIndexOf;

3. Add the following to the code-behind:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        GenerateNames();
        comboBox1.DataSource = Names;
    }

    public List<string> Names { get; set; } = new();

    private void GenerateNames()
    {
        for (int i = 0; i < 100; i++)
        {
            Names.Add($"Name {i}");
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtSelectedIndex.Text = comboBox1.SelectedIndex.ToString();
            
        txtSelectedItem.Text = comboBox1.SelectedItem.ToString();

        txtCollectionIndexOf.Text = Names
            .IndexOf(comboBox1.SelectedItem.ToString() 
                     ?? "no item selected")
            .ToString();
    }
}

When you select items in the ComboBox, you will see the results in the TextBoxes. This is exactly what has been discussed.
 
Share this answer
 
v2
Comments
ernteSKY 20-Nov-22 7:26am    
The ComboxBox list is filled with list a strings what is simply Names (just 1 table filled up with string texts) of smomething. Then convert to integer. SelectedIndex is not working because have no index of them. I want just the string but I can't convert to string. How to set the following line as a string? (string)comboBox_CauseOfGame.SelectedItem - Cannot implicitly convert type 'string' to 'int'
Graeme_Grant 20-Nov-22 7:31am    
Did you look at the Microsoft example? You cant convert a name/string field to an int. You need to get the string and find the index in the collection/db list using something like the IndexOf method of the collection used. ref: Collection<T>.IndexOf(T) Method (System.Collections.ObjectModel) | Microsoft Learn[^]
ernteSKY 20-Nov-22 7:36am    
Yes of corse but looks difficult what I really want. I will look... But now I do not want to name/string field to an int! But I don't know where or how transformed to int? From the database to combobox is still a string then convert to int but really dont know how.
Graeme_Grant 20-Nov-22 7:43am    
You have 2 choices: 1. Use the SelectedIndex of the ComboBox or get the indexOf of the SelectedItem of the ComboBox in the collection. You can not convert the SelectedItem of the ComboBox to an integer UNLESS the SelectedItem is of a numerical value that can be converted to an integer.
Graeme_Grant 20-Nov-22 8:47am    
see my updated answer. I can't make it any easier for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900