Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Fill comboBox with DB content

C# Fill comboBox with DB content

Scheduled Pinned Locked Moved C#
csharpandroiddatabasetutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tizziCerutti
    wrote on last edited by
    #1

    I want to Bind two Columns and populate a ComboBox with it in C#..My code is printing just one. the FirstName. Have tried concatenating the LastName with it but its not working. Can anyone show me how to go about it. Here is my code. Thanks

    OleDbCommand cmd = new OleDbCommand("SELECT * FROM ADb", myCon);
    OleDbDataAdapter d = new OleDbDataAdapter(cmd);
    d.SelectCommand.CommandText = cmd.CommandText.ToString();

            DataTable dt = new DataTable();
            d.Fill(dt);
            cmbNames.DataSource = dt;
            cmbNames.DisplayMember = "FirstName";
    
    B A B A 4 Replies Last reply
    0
    • T tizziCerutti

      I want to Bind two Columns and populate a ComboBox with it in C#..My code is printing just one. the FirstName. Have tried concatenating the LastName with it but its not working. Can anyone show me how to go about it. Here is my code. Thanks

      OleDbCommand cmd = new OleDbCommand("SELECT * FROM ADb", myCon);
      OleDbDataAdapter d = new OleDbDataAdapter(cmd);
      d.SelectCommand.CommandText = cmd.CommandText.ToString();

              DataTable dt = new DataTable();
              d.Fill(dt);
              cmbNames.DataSource = dt;
              cmbNames.DisplayMember = "FirstName";
      
      B Offline
      B Offline
      Boipelo
      wrote on last edited by
      #2

      You meant two rows? Whats the other value you want to bind or data in your table? If it the ID add it as Value Member

      cmbNames.ValueMember = "id";

      Bombo box will bind row by row from your table.

      I remain joe!

      1 Reply Last reply
      0
      • T tizziCerutti

        I want to Bind two Columns and populate a ComboBox with it in C#..My code is printing just one. the FirstName. Have tried concatenating the LastName with it but its not working. Can anyone show me how to go about it. Here is my code. Thanks

        OleDbCommand cmd = new OleDbCommand("SELECT * FROM ADb", myCon);
        OleDbDataAdapter d = new OleDbDataAdapter(cmd);
        d.SelectCommand.CommandText = cmd.CommandText.ToString();

                DataTable dt = new DataTable();
                d.Fill(dt);
                cmbNames.DataSource = dt;
                cmbNames.DisplayMember = "FirstName";
        
        A Offline
        A Offline
        Anna King
        wrote on last edited by
        #3

        You have just provided the code and query for the Firstname, please provide the same code for Lastname too.

        1 Reply Last reply
        0
        • T tizziCerutti

          I want to Bind two Columns and populate a ComboBox with it in C#..My code is printing just one. the FirstName. Have tried concatenating the LastName with it but its not working. Can anyone show me how to go about it. Here is my code. Thanks

          OleDbCommand cmd = new OleDbCommand("SELECT * FROM ADb", myCon);
          OleDbDataAdapter d = new OleDbDataAdapter(cmd);
          d.SelectCommand.CommandText = cmd.CommandText.ToString();

                  DataTable dt = new DataTable();
                  d.Fill(dt);
                  cmbNames.DataSource = dt;
                  cmbNames.DisplayMember = "FirstName";
          
          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          One approach is to retrieve the data in the format you want:

          OleDbCommand cmd = new OleDbCommand("SELECT id, FirstName + ' ' + LastName as Name FROM ADb", myCon);
          OleDbDataAdapter d = new OleDbDataAdapter(cmd);
          d.SelectCommand.CommandText = cmd.CommandText.ToString();

          DataTable dt = new DataTable();
          d.Fill(dt);
          cmbNames.DataSource = dt;
          cmbNames.DisplayMember = "Name";

          I took a guess at the name of your ID column. You still need to select that if you use the combo for selecting things elsewhere.

          T 1 Reply Last reply
          0
          • T tizziCerutti

            I want to Bind two Columns and populate a ComboBox with it in C#..My code is printing just one. the FirstName. Have tried concatenating the LastName with it but its not working. Can anyone show me how to go about it. Here is my code. Thanks

            OleDbCommand cmd = new OleDbCommand("SELECT * FROM ADb", myCon);
            OleDbDataAdapter d = new OleDbDataAdapter(cmd);
            d.SelectCommand.CommandText = cmd.CommandText.ToString();

                    DataTable dt = new DataTable();
                    d.Fill(dt);
                    cmbNames.DataSource = dt;
                    cmbNames.DisplayMember = "FirstName";
            
            A Offline
            A Offline
            Amol_B
            wrote on last edited by
            #5

            You can append two columns in SQL query and use that column as display Member

            SELECT Firstname + ' ' + Lastname,* FROM ADb

            1 Reply Last reply
            0
            • B BobJanova

              One approach is to retrieve the data in the format you want:

              OleDbCommand cmd = new OleDbCommand("SELECT id, FirstName + ' ' + LastName as Name FROM ADb", myCon);
              OleDbDataAdapter d = new OleDbDataAdapter(cmd);
              d.SelectCommand.CommandText = cmd.CommandText.ToString();

              DataTable dt = new DataTable();
              d.Fill(dt);
              cmbNames.DataSource = dt;
              cmbNames.DisplayMember = "Name";

              I took a guess at the name of your ID column. You still need to select that if you use the combo for selecting things elsewhere.

              T Offline
              T Offline
              tizziCerutti
              wrote on last edited by
              #6

              Thanks BobJanova. Thanks Everyone...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups