Filling and Getting the ValueMember of a ComboBox in Vb.Net

In this tutorial I will show you how to fill and get the ValueMember of a ComboBox in VB.Net. Knowing the value member of the items that will be displayed in a combo box is very important.Why? For instance, in the “employee’s table”, you want to display the name of the employee in the combo box, but you want to get the id of it. So, that’s the time to use the value member to get the id of the employee in the combo box. I used MS Access for my Database. Let’s Begin: 1.Open Visual Studio. 2.Create a new project. 3.Make the Form just like this. getfirstform Now, double click the Form. Then, declare all the classes and variables that are needed above the Form1_Load.
  1. 'DECLARE A STRING VARIABLE TO STORE THE STRING CONNECTION.
  2.     Dim strcon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
  3.                                          Application.StartupPath & "\userdb.accdb"
  4.  
  5.    'INITIALIZE THE STRING CONNECTION TO BE OPEN
  6.     Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection(strcon)
  7.     'INITIALIZE THE COMMANDS THAT ARE USED TO FILL THE DATASET AND UPDATE THE DATA SOURCE
  8.     Dim da As New OleDb.OleDbDataAdapter
  9.     'INITIALIZE YOUR DATASET
  10.     Dim ds As New DataSet
  11.     'DECLARE A STRING VARIABLE TO STORE THE QUERY IN IT
  12.     Dim sql As String
After that, create a sub procedure for filling a ComboBox with data that came from the database.
  1. Public Sub cbofill(ByVal sql As String, ByVal cbo As ComboBox)
  2.         Try
  3.             'OPENING THE CONNECTION
  4.             con.Open()
  5.             'SET YOUR COMMANDS AND A DATABASE CONNECTION
  6.             ' THAT ARE USED TO FILL THE DATASET AND UPDATE
  7.             ' THE DATA SOURCE.
  8.             da = New OleDb.OleDbDataAdapter(sql, con)
  9.             'SET A NEW DATASET
  10.             ds = New DataSet
  11.             'ADDING AND REFRESHING THE ROWS OF YOUR DATATABLE
  12.             '(FILLING THE DATATABLE)
  13.             da.Fill(ds, "userdb")
  14.  
  15.             With cbo
  16.                 'SET YOUR DATASOURCE TO GET THE
  17.                 ' COLLECTION OF TABLES CONTAINED
  18.                 ' IN THE DATASET
  19.                 .DataSource = ds.Tables(0)
  20.                 'SET THE FIELDS OF THE DATATABLE TO
  21.                 ' BE SHOWN ON THE COMBOBOX
  22.                 .DisplayMember = "username"
  23.                 'SET THE ACTUAL VALUE OF THE
  24.                 ' ITEMS IN THE COMBOBOX
  25.                 .ValueMember = "user_id"
  26.             End With
  27.         Catch ex As Exception
  28.             'CATCH ANY ERRORS
  29.             MsgBox(ex.Message)
  30.         End Try
  31.         'CLOSING THE CONNECTION
  32.         con.Close()
  33.     End Sub
In the Form1_Load, set your query and call the sub procedure that you have created.
  1.   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         'STORE YOUR QUERY IN THE STRING VARIABLE
  3.         sql = "SELECT * FROM tbluser"
  4.         'CALL YOUR SUB PROCEDURE AND PUT YOUR QUERY IN THE FIRST PARAMETER.
  5.         'AND FOR THE SECOND PARAMETER IS THE NAME OF THE COMBOBOX.
  6.         cbofill (sql, ComboBox1)
  7.     End Sub
In the click event handler of a Button, set the selected value of a ComboBox to put it on the RichTextBox.
  1.   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         'PUT THE VALUEMEMBER OF THE COMBOBOX IN THE RICHTEXTBOX
  3.         RichTextBox1.Text = "User Id :" & ComboBox1.SelectedValue.ToString
  4.     End Sub

Comments

Hi. good day!... i need your help guys. i do have a project which is scheduling system.. my problem is i want to display data where in the feild "superior" is equal to the selected item in combobox...my database is SQL..thank you in advance.\

In this project, only 1 output. So can you plz create a another project that will show 5 output in 5 nos of TextBox ?

Yes, i can do that. just keep updated.

Add new comment