Filling ComboxBox with Data in VB.Net and SQL Server 2019

Submitted by: 
Visitors have accessed this post 147 times.

In this tutorial, I will teach you how to Fill a ComboBox with Data in VB.Net and SQL Server 2019. This method has the ability to retrieve the data in the sql server database and display it into the combobox. This is simple yet powerful method that you can do it in no time. Follow the step by step guide to know how it works.

I used Microsoft Visual Studio 2015 and SQL Server 2019 to develop this program.

Let’s Begin:

Creating Database

1. Install the MSSMS 2019 on your machine.
2. Open the MSSMS 2019 . After that, right click the database, then select “New Database” and name it “dbperson
fig 1
3. Do the following query to create a table in the database that you have created

  1.  
  2. USE [dbperson]
  3. GO
  4.  
  5. /****** Object: Table [dbo].[tblperson] Script Date: 11/10/2019 11:15:11 AM ******/
  6. SET ANSI_NULLS ON
  7. GO
  8.  
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11.  
  12. CREATE TABLE [dbo].[tblperson](
  13. [PersonID] [INT] IDENTITY(1,1) NOT NULL,
  14. [Fname] [nvarchar](50) NULL,
  15. [Lname] [nvarchar](50) NULL
  16. ) ON [PRIMARY]
  17. GO

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for visual basic.
ps1

Step 2

Add a ComboBox inside the Form. Then do the Form just like shown below.
fig 2

Step 3

Press F7 to open the code editor. In the code editor, add a namespace to access SQL Server libraries.

  1.  
  2. Imports System.Data.SqlClient

Step 4

Create a connection between Visual Basic 2015 and SQL Server database. After that, declare and initialize all the classes and variables that are needed.

  1.  
  2. Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;Database=dbperson;trusted_connection=true;")
  3. Dim cmd As SqlCommand
  4. Dim da As SqlDataAdapter
  5. Dim dt As DataTable
  6. Dim sql As String

Step 5

Create a method for filling data in the combobox in the database.

  1.  
  2. Private Sub fillCombo(sql As String, cbo As ComboBox)
  3. Try
  4. con.Open()
  5.  
  6. cmd = New SqlCommand
  7. da = New SqlDataAdapter
  8. dt = New DataTable
  9.  
  10. With cmd
  11. .Connection = con
  12. .CommandText = sql
  13. End With
  14.  
  15. With da
  16. .SelectCommand = cmd
  17. .Fill(dt)
  18. End With
  19.  
  20. cbo.DataSource = dt
  21. cbo.DisplayMember = dt.Columns(1).ColumnName
  22. cbo.ValueMember = dt.Columns(0).ColumnName
  23.  
  24. Catch ex As Exception
  25. MsgBox(ex.Message)
  26. Finally
  27. con.Close()
  28. da.Dispose()
  29. End Try
  30. End Sub

Step 6

Write the following codes to fill data in the combobox in the first load of the form.

  1.  
  2. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  3. sql = "SELECT * FROM tblperson"
  4. fillCombo(sql, ComboBox1)
  5. End Sub

The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment bel


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.