Populating comboBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buterfly0707
    New Member
    • Nov 2009
    • 30

    Populating comboBox

    hi..

    i want to fill my combobox from the values which im having in my table in the database. and my combobox name is combobox1 and my table is tblDetails, and this table having two columns code and name. i want code values to the cobobox.

    here is my code which i tried to do
    Code:
      this.comboBox1.DataSource = ds.Tables["tbldetail"];
      this.comboBox1.DisplayMember = "Code";//column display name
      this.comboBox1.ValueMember = "code";//actual column name
    ds the dataset.

    this is not working. can some one tell me what was the wrong that im doing here.
    Last edited by Frinavale; Nov 23 '09, 03:04 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    What's the error you're having? I just set up an example using the *exact same code you just posted and it's actually working for me, so I'm wondering if you've got a mistake somewhere in your code, or if I set my test table up differently than you did.

    In addition to letting us know what your error actually is (or how it's not working), maybe check to make sure your table actually contains data at that point, as well as the column names. It's all I can think of with what you've given so far.

    As a side note, bound controls can sometimes not work how you would like. Sometimes it's a good idea to copy the data from your datatable into the combobox manually. I'm not saying you have to do this, but it is an option :)

    *by exact, I mean those three lines using a data table I manually set up and inserted rows into

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Originally posted by buterfly0707
      hi..

      i want to fill my combobox from the values which im having in my table in the database. and my combobox name is combobox1 and my table is tblDetails, and this table having two columns code and name. i want code values to the cobobox.

      here is my code which i tried to do
      Code:
        this.comboBox1.DataSource = ds.Tables["tbldetail"];
        this.comboBox1.DisplayMember = "Code";//column display name
        this.comboBox1.ValueMember = "code";//actual column name
      ds the dataset.

      this is not working. can some one tell me what was the wrong that im doing here.
      So, you say your two column names are "code" and "name"....b ut look at your code: you used "Code" and "code".

      Also, the column names are case sensitive in the .NET data set.

      For future reference:
      "It's not working." is usually not enough for us to help you on. If you have errors, please explain exactly what is not working, and provide the exception name and text.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Originally posted by insertAlias
        So, you say your two column names are "code" and "name"....b ut look at your code: you used "Code" and "code".

        Also, the column names are case sensitive in the .NET data set.
        I was going to point out the same, but I tested it and it actually works. So either column names aren't case sensitive, or DisplayMember ignores case. To be honest though, I am by no means an expert on this kind of thing, so there may be something else going on (with regards to why it accepts "Code" and "code").

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Hmmm, apparently I was assuming the case sensitive part. You know, when you assume something long enough, you start to think that it's fact.

          Regardless, the names he used aren't the same as the names he said his columns were called.

          Comment

          • buterfly0707
            New Member
            • Nov 2009
            • 30

            #6
            thanks for your replies.

            Actually i am not getting any errors or warnings. But its not bounding the values to the combobox from database table.

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Is your combobox coming up empty? Are there blanks where your selections should be, or is it a completely empty combo box?

              It couldn't hurt to check your names as insertAlias suggested, but maybe you should confirm that your database (in those columns) actually contains data. Like I said, I did a pretty quick test with a dataset that I populated manually and it actually worked, so that leads me to believe something might be up with your data. It's a guess, but it should be pretty easy to confirm. Set a breakpoint where you're binding data to your combobox and just use the debugger to see what's in the dataset.

              Comment

              Working...