Create Database Connection & Fill combo box

Discussion Create Database Connection & Fill combo box

  • Monday, February 10, 2003 11:18 AM
     
     
    I created a windows form that has a combo box that is supposed to be filled via fields from a database stored procedure. How do i di this? 

    1. Create the db connection?
    2. Fill the combo box from the SP?
    3. Do i create the connection in the combo box event handler? Please help me.

    I am new to this and would appreciate any guidance... This is a windows form.

    Thanks in advacve. 
     

All Replies

  • Monday, February 10, 2003 1:10 PM
     
     
    The steps would be more like this:

    1. Create the db connection
    2. Create a command object
    3. Set properties of the command object, including the connection object, sp name, etc.
    4. Set any parameters required by the sp
    5. Execute the sp, probably using the ExecuteReader method
    6. Bind the data reader to the combo box

    Sounds complicated, but it's really not. What database are you using? What parameters, if any, does the sp need?

    Don
  • Monday, February 10, 2003 2:28 PM
     
     
    i'm using sql 2000. The sp is a list of categories so it does not need any params.. I just don't know how to bind this to the combo box.. And yes iut does sound very complicated..  :-)

  • Monday, February 10, 2003 5:43 PM
     
     
    Not sure if you actually can bind to a DataReader in WindowsForms, use a DataTable (get a DataTable using the SqlDataAdapter [use it's .Fill Method])

    As for DataBinding it, it's really easy, just set the .DataSource Property of the ComboBox to the DataTable.  Then, (or before, shouldn't matter), set the .ValueMember and .DisplayMember Properties of the ComboBox to the fields you want to be the Value field and the Display field (respectively)

    Let us know if you still can't get it
  • Monday, February 10, 2003 8:20 PM
     
     
    Right. I mistyped. the data reader doesn't implement IList.

    Don
  • Friday, February 21, 2003 5:38 PM
     
     
    Also, check out the quickstarts at http://samples.gotdotnet.com/quickstart/winforms/ that have samples for binding a combobox and for filling your data from a database (check out the first grid sample for that).