ติดปัญหาตรง
1.เวลาเลือก ComboCondition แล้วมัน Error
2.เวลาเลือก ComboCondition แล้ว กรณีที่ข้อมูลใน Data base SQL SEVER มันไม่มี อยากให้ ComboDetai โชว์ค่าว่าง ตอนนี้มันโชว์ ข้อมูลจากตัวแรกที่เราเลือก
หน้าจอ
ส่วน Error
Code (VB.NET)
Private Sub ShowDataCombobox()
If ComboCondition.Text = "Terminal" Then
sb = New StringBuilder()
sb.Append("SELECT * FROM tTerminal")
Dim sqlIni As String
sqlIni = sb.ToString()
RemoveHandler ComboDetail.SelectedValueChanged, AddressOf ComboDetail_SelectedIndexChanged
com = New SqlCommand()
With com
.CommandText = sqlIni
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
End With
If dr.HasRows Then
Dim dttTerminal As DataTable
dttTerminal = New DataTable()
dttTerminal.Load(dr)
With ComboDetail
.BeginUpdate()
.DisplayMember = "C_Name"
.ValueMember = "L_ID"
.DataSource = dttTerminal
.EndUpdate()
End With
dr.Close()
AddHandler ComboDetail.SelectedValueChanged, AddressOf ComboDetail_SelectedIndexChanged
ComboDetail.ContextMenu = New ContextMenu()
End If
ElseIf ComboCondition.Text = "Department" Then
sb = New StringBuilder()
'sb.Append("SELECT {FN Concat({FN Concat(c_code,' : ')},substring(c_name,1,30))} As test FROM cPost")
sb.Append("SELECT * FROM cPost")
Dim sqlIni As String
sqlIni = sb.ToString()
RemoveHandler ComboDetail.SelectedValueChanged, AddressOf ComboDetail_SelectedIndexChanged
com = New SqlCommand()
With com
.CommandText = sqlIni
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dtcPost = New DataTable
dtcPost.Load(dr)
With ComboDetail
.BeginUpdate()
.DisplayMember = "C_Name"
.ValueMember = "C_Code"
.DataSource = dtcPost
.EndUpdate()
End With
End If
dr.Close()
AddHandler ComboDetail.SelectedValueChanged, AddressOf ComboDetail_SelectedIndexChanged
ComboDetail.ContextMenu = New ContextMenu()
End With
End If
End Sub
Tag : .NET, Ms SQL Server 2005, Win (Windows App), VB.NET
If ComboCondition.Text = "Terminal" Then
ComboDetail.Items.Clear()
'--อ่านชื่อหมวด Terminal ลงใน ComboDetail เพื่อใช้ในการเลือกรายชื่อ
sql = ("SELECT * FROM tTerminal Order by L_ID")
command = New SqlCommand(sql, connection)
reader = command.ExecuteReader()
While reader.Read()
ComboDetail.Items.Add(reader("L_ID"))
End While
reader.Close()
If ComboDetail.Items.Count > 0 Then
ComboDetail.SelectedIndex = 0
End If
ElseIf ComboCondition.Text = "Department" Then
ComboDetail.Items.Clear()
sql = ("SELECT {FN Concat({FN Concat(c_code,' : ')},substring(c_name,1,30))} As test FROM cPost")
command = New SqlCommand(sql, connection)
reader = command.ExecuteReader()
While reader.Read()
ComboDetail.Items.Add(reader(0))
End While
reader.Close()
If ComboDetail.Items.Count > 0 Then
ComboDetail.SelectedIndex = 0
End If
End If
Private Sub Combo2_KeyDown(KeyCode As Integer, Shift As Integer)
Set RS = New ADODB.Recordset
RS.Open "select * from Table2 where SN = '" & Combo2.Text & "'", cn, adOpenKeyset, adLockOptimistic
If Not RS.EOF Then
Me.Combo1.Text = RS!Device
End If
RS.Close
Set RS = Nothing
End Sub