Results 1 to 3 of 3

Thread: Combo Box Number from Query

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2018

    Combo Box Number from Query

    So i have a database with 2 rows code,name lets say its like
    code / name
    1 / john
    2 / george

    i use this query to bring them in my combo box .

    strConnection = String.Format("Provider=SQLOLEDB;Data Source={0};Initial Catalog={1};User ID={2};Password={3};",
                                              strServer, strDataBase, strUserName, strPassword)
                Dim Connection As New OleDbConnection(strConnection)
                Dim cm As New OleDbCommand("SELECT Codeid [Κωδικός],descr [Περιγραφή] FROM EMBONILO_B.DBO.manufacturer GROUP BY Codeid,descr", Connection)
                Dim dr As OleDbDataReader = cm.ExecuteReader
                While dr.Read
                End While
    and it show the name john and george. What i want is when you click the combo box and you select a name i want the code to appear on the combo box lets say if its george selected i want number 2 in combo box etc.

    Thanks for advance.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Combo Box Number from Query

    Quote Originally Posted by xmixmaster View Post
    So i have a database with 2 rows code,name
    It sounds like you mean two columns rather than two rows. Also, according to your SQL, they are Codeid and descr, not code and name.

    As for the question, that's not really how ComboBoxes work. What you would normally do is use a Label or perhaps a TextBox to display the other value, e.g. Code:
    1. Dim builder As New OleDbConnectionStringBuilder With {.Provider = "SQLOLEDB", .DataSource = strServer}
    3. With builder
    4.     .Item("Initial Catalog") = strDataBase
    5.     .Item("User ID") = strUserName
    6.     .Item("Password") = strPassword
    7. End With
    9. Using connection As New OleDbConnection(builder.ConnectionString),
    10.       adapter As New OleDbDataAdapter("SELECT DISTINCT Codeid [Κωδικός], descr [Περιγραφή] FROM EMBONILO_B.DBO.manufacturer", connection)
    11.     Dim table As New DataTable
    13.     adapter.Fill(table)
    15.     BindingSource1.DataSource = table
    17.     With ComboBox1
    18.         .DisplayMember = "Περιγραφή"
    19.         .ValueMember = "Κωδικός"
    20.         .DataSource = BindingSource1
    21.     End With
    23.     Label1.DataBindings.Add("Text", BindingSource1, "Κωδικός")
    24. End Using
    Now you can select a description in the ComboBox and the corresponding code will be displayed in the Label. You could possibly set it up such that the Label was covering the text portion of the ComboBox, so the user could see the descriptions in the drop-down list but the code "in" the text area.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Combo Box Number from Query

    On a separate note, is there a good reason that you're using OleDb to connection SQL Server instead of the dedicated SqlClient? If not, switch to using SqlClient instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width