Private Sub cbSaleCard_No_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbSaleCard_No.SelectedIndexChanged
Dim sqlEdit As String
Dim cmd As New SqlCommand
For i = 0 To dgvEdit.Rows.Count - 1
sqlEdit = "SELECT Customer_ID,Agent_ID,Embassy_ID,DateofIssue,Note,VisaType,WorkingDays,Entries,Duration,Cost,ServiceFee,Sale,Pax,Totalcost,Passenger_ID From VisaSaleCard"
sqlEdit &= "WHERE SaleCard_No = '" & cbSaleCard_No.Text & "'"
cmd = New SqlCommand(sqlEdit, Conn)
dr = cmd.ExecuteReader '********* error บรรทัดนี้ค่ะว่า Incorrect syntax near '='.
dr.Read()
cbCustomer1.Text = dr.Item("Customer_ID").ToString
dr.Close()
Next
End Sub
Dim sqlCompany1 As String
Dim bs1 As New BindingSource
sqlCompany1 = "SELECT * FROM VisaSaleCard"
sqlCompany1 &= " WHERE SaleCard_No = '" & cbSaleCard_No.Text & "'"
'da = New SqlDataAdapter(sqlCompany1, Conn)
' da.Fill(ds, "Customer1")
With cbCustomer1
.DisplayMember = "Customer_ID"
.ValueMember = "Customer_Name"
.DataSource = ds.Tables("Customer1")
End With
'cbCustomer1.DataBindings.Clear()
cbCustomer1.DataBindings.Add("text", ds, "VisaSaleCard.Customer_ID")
end if
Public Class Form1
Dim _DataTable As DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim _Table As DataTable
Dim _Row As DataRow
Dim _BindingSource As New BindingSource
_Table = New DataTable("สัตว์")
_Table.Columns.Add("ชื่อ")
_Table.Columns.Add("ชนิด")
_Table.Columns.Add("ประเภท")
_Row = _Table.NewRow
_Row("ชื่อ") = "จระเข้"
_Row("ชนิด") = "สัตว์ปีก"
_Row("ประเภท") = "สัตว์กินพืช"
_Table.Rows.Add(_Row)
_Row = _Table.NewRow
_Row("ชื่อ") = "นกแก้ว"
_Row("ชนิด") = "สัตว์เลื่อยคลาน"
_Row("ประเภท") = "สัตว์กินเนื้อ"
_Table.Rows.Add(_Row)
_Row = _Table.NewRow
_Row("ชื่อ") = "ปลาสวาย"
_Row("ชนิด") = "สัตว์บก"
_Row("ประเภท") = "สัตว์กินขนมปัง"
_Table.Rows.Add(_Row)
_BindingSource.DataSource = _Table
'_BindingSource.DataMember = "สัตว์"
ComboBox1.DataSource = _BindingSource
ComboBox1.DisplayMember = "ชื่อ"
TextBox1.DataBindings.Add("text", _BindingSource, "ชนิด")
TextBox2.DataBindings.Add("text", _BindingSource, "ประเภท")
End Sub
End Class
***ดู บรรทัดที่ 34 ลงมาครับจะเป็นขั้นตอนการผูก Data กับ Control อันบนเป็นการสร้าง Data เฉยๆ
Dim sql As String
Dim bs As New BindingSource
sql = "SELECT * FROM Table1"
sql &= " WHERE Field_No = '" & combobox1.Text & "'"
da = New SqlDataAdapter(sql, Conn)
Com = New SqlCommand()
With Com
.CommandText = sql
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dt = New DataTable()
dt.Load(dr)
bs.DataSource = dt
combobox2.DataBindings.Clear()
combobox2.DataBindings.Add("Text", bs, "Field_ID")
End If
dr.Close()
End With