Jump to content

combobox and database - help

- - - - -

  • Please log in to reply
2 replies to this topic

#1
globalmind

globalmind

    Newbie

  • Members
  • Pip
  • 1 posts
Hi!
I'm new here and new at C# programming.

I need code that will fill combobox from database.
I made something like this, but this doesn't work:
private void comboCategory_SelectedIndexChanged(object sender, EventArgs e)

        {

  

            comboCategory.Items.Clear();

            string SQLcombo = "SELECT CategoryName AS ComboList FROM Category";

            SqlCommand cmdCombo = new SqlCommand(SQLcombo, cnnStore);


            cnnStore.Open();

            SqlDataReader comboReader;

            comboReader = cmdCombo.ExecuteReader();

            while (comboReader.Read())

            { 

                comboCategory.Items.Add("CategoryList");

            }


        }

And when item from combo is selected, it need to show data from database in textbox. (ID, Name and Description)

Posted Image

Any help is apriciated!!
ThX

#2
elizabethmwashuma

elizabethmwashuma

    Newbie

  • Members
  • PipPip
  • 29 posts
hi, r u initially able to have the drop down items of the combo box from the database?

#3
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
you might consider downloading more of the table, and storing it in something like a Data Table.
Caching the information locally would have some great advantages, like latency. Chances are users will select one item, and then select another.

But to answer your question, you're likely going to want to store more than the name on the combo box, you're also going to want to store an identifier, so you know which record to pull the rest of the information from.

use ValueMember for that
so when the user selects something, you can then use the SelectedValue property, which you can then use in your query against the table, to pull the rest of the information.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users