locked
Populating A Drop Down List Combo Box (C#, SQL) RRS feed

  • Question

  • Hi, All.  I'm hoping someone might be able to help me out here.  I searched and couldn't find any users that had this problem.

     

    I'm trying to populate a form in a C# application based on values retrieved from a SQL Database Table.  I'm not having any problems connecting to the Database, nor am I having problems retreiving data from my data table.  Most of the form is populating just fine.

     

    I'm trying to populate a drop down list combo box with the value in one of the columns using a foreach loop:

     

    private void populateSelector( SqlDataAdapter adapter, DataSet profile )  {

    DataTable savedProfiles = new DataTable();

    adapter.FillSchema( profile, SchemaType.Source, "TABLE" );

    adapter.Fill( profile, "TABLE" );

    savedProfiles = profile.Tables["TABLE"];

    foreach( DataRow selectProfile in dtProfile.Rows ) {

    cbSavedProfiles.Items.Add( selectProfile["Column"].ToString() );

    }

    }

     

    What I end up with, is two of the values in Column "Column" in my selector drop down.  Currently there's only one populated row in the table, but since one of the rows is the NULL-filled rows, it's reading both.  My stored procedure is set to return "where column is not null" but it doesn't seem to help.

     

    A final note:  I do not want to bind the control to the data.  While that may superficially seem like the easier solution, it actually has caused some problems with some other areas of the data retrieval.

     

    Thanks.

     

     

    Tuesday, January 29, 2008 3:47 AM

Answers

  • This is the code you have posted.. just look the bold text i have mede in it..

    The DataTable dtProfile you are using here is not declare in this procedure. Actually this confuses me.. I think it should be savedProfiles insted..

    Code Snippet

    private void populateSelector( SqlDataAdapter adapter, DataSet profile )  {

    DataTable savedProfiles = new DataTable();

    adapter.FillSchema( profile, SchemaType.Source, "TABLE" );

    adapter.Fill( profile, "TABLE" );

    savedProfiles = profile.Tables["TABLE"];

    foreach( DataRow selectProfile in dtProfile.Rows ) {

    cbSavedProfiles.Items.Add( selectProfile["Column"].ToString() );

    }

    }


    Tuesday, January 29, 2008 7:19 AM

All replies

  • ok

    Are you positive that the extra row that you are getting back does not have a blank (not null but blank)  as a value ?

    In which case your Stored Procedure would still be returning it and you would get that extra unwanted record.

     

    When you run your stored proc how many records does it return ?

     

    Tuesday, January 29, 2008 6:22 AM
  • Yes Gaetan is right..

    You once check the returing data of the store procedure...

    and do another thing clear the combobox before filling it..
    Tuesday, January 29, 2008 6:30 AM
  • When I run the command in QA, I only get one row returned.  But the data table view shows two rows:

     

    http://farm3.static.flickr.com/2113/2228128450_c59731faa1_o.jpg

     

    However, when I run the application, I get two of the same Names (Default in the pic) in my list box.

    Tuesday, January 29, 2008 6:31 AM
  • Thanks for the input, but clearing the combobox didn't change the result. 

     

    Personally, my guess is that it has to do with the foreach loop, but I am not sure.

     

    Is there a way to add the data row to a dataset using a more conventional loop?  A

     

    for( int i = 0; i < DataTable.Rows; i++) 

     

    sort of loop would probably work, but I haven't had any luck getting that to work either.

     

    Tuesday, January 29, 2008 6:39 AM
  • Your code seems perfect except one this confusing me you are using dtProfile to loop throught . but you are saving the data in SavedProfile DataTable.. Can you check this once..
    Tuesday, January 29, 2008 7:02 AM
  •  

    I'm sorry, I don't think I understand your point.  What am I doing that confuses you?

    The way I understand it is that it's looping through the data table that I've loaded into that variable.  This is my first application with C# that uses a link to a SQL database, so I actually followed the steps I found here:  http://support.microsoft.com/kb/314145/EN-US/  (at least I think I did).

     

     

    Tuesday, January 29, 2008 7:08 AM
  • This is the code you have posted.. just look the bold text i have mede in it..

    The DataTable dtProfile you are using here is not declare in this procedure. Actually this confuses me.. I think it should be savedProfiles insted..

    Code Snippet

    private void populateSelector( SqlDataAdapter adapter, DataSet profile )  {

    DataTable savedProfiles = new DataTable();

    adapter.FillSchema( profile, SchemaType.Source, "TABLE" );

    adapter.Fill( profile, "TABLE" );

    savedProfiles = profile.Tables["TABLE"];

    foreach( DataRow selectProfile in dtProfile.Rows ) {

    cbSavedProfiles.Items.Add( selectProfile["Column"].ToString() );

    }

    }


    Tuesday, January 29, 2008 7:19 AM
  • Ah, I get you.  dtProfiles is a global I declared for several methods of the form to use.

     

    I can give it a shot as you suggested.

    Tuesday, January 29, 2008 7:21 AM
  •  

    Just a FYI, I was having the same issue, but I fixed it by setting AppendDataBoundItems=True

     

     

    <asp:DropDownList ID="ddlFloor" runat="server" DataSourceID="SqlDataSource1"

    DataTextField="Floor" DataValueField="Floor" AppendDataBoundItems=True Width="200px" >

    </asp:DropDownList>

    Wednesday, February 6, 2008 6:49 PM
  • Hi,

    Please go through the below link for help with populating combo box with and without Stored Procedure !

    http://social.msdn.microsoft.com/Forums/en-US/csharpide/thread/24a391d2-8838-4d44-993b-a43046a42b05

     

    Thanks

    Shyam

    Wednesday, April 28, 2010 11:25 AM
  • Hi,

    Please go through the below link for help with populating combo box with and without Stored Procedure !

    http://social.msdn.microsoft.com/Forums/en-US/csharpide/thread/24a391d2-8838-4d44-993b-a43046a42b05

     

    Thanks

    Shyam

    Wednesday, April 28, 2010 11:27 AM