-
Oct 25th, 2020, 03:47 PM
#1
Thread Starter
Junior Member
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.
-
Oct 25th, 2020, 07:25 PM
#2
Re: Combo Box Number from Query
Originally Posted by xmixmaster
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:
Dim builder As New OleDbConnectionStringBuilder With {.Provider = "SQLOLEDB", .DataSource = strServer}
With builder
.Item("Initial Catalog") = strDataBase
.Item("User ID") = strUserName
.Item("Password") = strPassword
End With
Using connection As New OleDbConnection(builder.ConnectionString),
adapter As New OleDbDataAdapter("SELECT DISTINCT Codeid [Κωδικός], descr [Περιγραφή] FROM EMBONILO_B.DBO.manufacturer", connection)
Dim table As New DataTable
adapter.Fill(table)
BindingSource1.DataSource = table
With ComboBox1
.DisplayMember = "Περιγραφή"
.ValueMember = "Κωδικός"
.DataSource = BindingSource1
End With
Label1.DataBindings.Add("Text", BindingSource1, "Κωδικός")
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.
-
Oct 25th, 2020, 07:25 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|