dim selectstr as string ="select * from tb_name"
dim cmd as new sqlcommand(selectstr,conn) '<---- conn คือ connectionstring
dim dr as sqldatareader
dr = cmd.excutereader
while dr.read
combobox1.items.add = dr("field_name")
end while
Public Class Form4
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'BudgetDataSet1.Bank' table. You can move, or remove it, as needed.
Dim sb As String = "SELECT BankID, BankName"
sb &= vbCrLf & "FROM Bank"
sb &= vbCrLf & "ORDER BY BankName"
Dim stConn As String = "Data Source=.\SQLExpress;Initial Catalog=Test;User ID=sa;Password=xxxxxxx"
Dim sqlCn As New SqlClient.SqlConnection(stConn)
sqlCn.Open()
Dim sqlCmd As New SqlClient.SqlCommand(sb, sqlCn)
Dim da As New SqlClient.SqlDataAdapter(sqlCmd)
da.Fill(Me.ds, "Bank")
Me.bsBank.DataSource = Me.ds
Me.bsBank.DataMember = "Bank"
Me.z_BankName.DataSource = Me.bsBank
Me.z_BankName.DisplayMember = "BankName"
End Sub
End Class
'combobox Customer
Dim sqlCompany As String
Dim bs As New BindingSource
sqlCompany = "SELECT * FROM Clients"
da = New OleDbDataAdapter(sqlCompany, Conn)
da.Fill(ds, "Clients")
bs.DataSource = ds
bs.DataMember = "Clients"
With cbCustomer
.DataSource = bs
.DisplayMember = "CompanyName"
.BeginUpdate()
.ValueMember = "CompanyID"
.EndUpdate()
'.DataBindings.Add("Text", bs, "CompanyName")
End With
'combobox Agent
Dim sqlAgent As String
Dim bs1 As BindingSource
sqlAgent = "SELECT * FROM Agent"
da = New OleDbDataAdapter(sqlAgent, Conn)
'bs1.Fill(ds, "Agent")
bs1.DataSource = ds
bs1.DataMember = "Agent"
cbAgent.DisplayMember = "Agent_name"
'cbAgent.DataBindings.Add("Text", bs, "Agent_name")
'cbCustomer.DataBindings.Add("Text", bs1, "CompanyName")
With cbAgent
.DataSource = bs1
.BeginUpdate()
.ValueMember = "Agent_ID"
.EndUpdate()
End With