how to load data in combobox from database in vb.net 2019?

How to Load Data In Combobox From Database in VB.NET

Load data in combobox From database in vb.net is very easy. To load data in combobox From database your need to creaate the following files in your project.

If you not read my previous article jus now : Vb.Net Program Structure

  1. Connection Module
  2. Form
  1. Connection Module

To add the module to write the code with integrate from the database and access the data.

Imports System.Data.SqlClient

Module Module1
Public rpt, dts As String
Public record As Boolean = False

Public con As New OleDb.OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source =” & Application.StartupPath & “\SGS_DB.mdb”)

Add 2nd module in your  project and write the following code.

mports System.Data.OleDb
Module ModConVar

Public cmd As New OleDbCommand
Public cn As New OleDbConnection
Public dr As OleDbDataReader
Public dt As New DataTable
Public da As New OleDbDataAdapter
Public db As String = "sgs_db.mdb"
Public cnstring As String = "Provider=Microsoft.jet.oledb.4.0;data source=" & db & ""

Add the Function In 2nd module.

 Public Sub ConnDB()
Try
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & constr & "")
conn.Open()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Code: conn001", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

2. Add GUI Form

step 01.  add GUI Form in your project and write a name.

Step02. Place once label and and a combobox from the  toolbox in the left side.

Step03. Add the following code in the Form namespace on the top.

Imports System.Data.OleDb

Stepe 04. Double click on the Form and add the following code on the Form Load Events
If con.State = ConnectionState.Open Then
con.Close()
End If
cmd = New OleDb.OleDbCommand("select category_name from category", con)
con.Open()
Dim dr1 As OleDb.OleDbDataReader = cmd.ExecuteReader
While dr1.Read
cbocategory.Items.Add(dr1(0))

End While


dr1.Close()
con.Close()




 

Leave a Reply