Results 1 to 3 of 3

Thread: Combo Box Number from Query

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    18

    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 .

    Code:
    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)
                Connection.Open()
                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
                    ComboBox.Items.Add(dr(1).ToString)
                End While
                dr.Close()
                Connection.Close()
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.
    vb.net Code:
    1. Dim builder As New OleDbConnectionStringBuilder With {.Provider = "SQLOLEDB", .DataSource = strServer}
    2.  
    3. With builder
    4.     .Item("Initial Catalog") = strDataBase
    5.     .Item("User ID") = strUserName
    6.     .Item("Password") = strPassword
    7. End With
    8.  
    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
    12.  
    13.     adapter.Fill(table)
    14.  
    15.     BindingSource1.DataSource = table
    16.  
    17.     With ComboBox1
    18.         .DisplayMember = "Περιγραφή"
    19.         .ValueMember = "Κωδικός"
    20.         .DataSource = BindingSource1
    21.     End With
    22.  
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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