How to load database value to combobox?


How to load database value to combobox?

How to load database value to combobox?

Abstract


Just drag and drop one combobox control to your windows form.
Then add the below two name space in you form
Imports System.Data.SqlClient
Imports System.Data
Full Code

Imports System.Data.SqlClient
Imports System.Data
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conectionstring As String
conectionstring = "USER=userid;PASSWORD=password;SERVER=servername;DATABASE=databasename"
Dim SQLQuery As String = "select p_id,lastname,firstname from persons123"
Dim MyConn As New SqlClient.SqlConnection(conectionstring)

Dim dataset As DataSet = New DataSet()
Dim dr As SqlDataReader
MyConn.Open()
Dim Cmd As New SqlClient.SqlCommand(SQLQuery, MyConn)
dr = Cmd.ExecuteReader()
While dr.Read
ComboBox1.Items.Add(dr(0).ToString())
End While
End Sub
End Class


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: