Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlDataReader
Public Class Form1
Public conn As New SqlConnection("server=NOR-PC;Integrated Security=True;User Id=sa ;Password=1234;database=test_top;Trusted_Connection=False; ")
Dim ch_load As Boolean = False
Public Sub load_order()
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
Dim str As String = "select * from Orders"
Dim dr As SqlDataReader
Dim dt As DataTable
Dim com As SqlCommand
com = New SqlCommand()
With com
.CommandType = CommandType.Text
.CommandText = str
.Connection = conn
dr = .ExecuteReader()
If dr.HasRows Then
dt = New DataTable
dt.Load(dr)
With cmb_order
.ValueMember = "ID_order"
.DisplayMember = "ID_order"
.DataSource = dt
End With
Else
cmb_order.DataSource = Nothing
End If
End With
conn.Close()
End Sub
Public Sub load_deptment()
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
Dim str As String = "select * from Deptment"
Dim dr As SqlDataReader
Dim dt As DataTable
Dim com As SqlCommand
com = New SqlCommand()
With com
.CommandType = CommandType.Text
.CommandText = str
.Connection = conn
dr = .ExecuteReader()
If dr.HasRows Then
dt = New DataTable
dt.Load(dr)
With cmb_dept
.ValueMember = "ID_dept"
.DisplayMember = "Name"
.DataSource = dt
End With
Else
cmb_order.DataSource = Nothing
End If
End With
conn.Close()
End Sub
Public Sub load_employee()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
load_order()
load_deptment()
ch_load = True
End Sub
Private Sub cmb_order_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_order.SelectedIndexChanged
If Not ch_load Then
Exit Sub
End If
Dim str As String = "select Deptment.Name,employee.Name from Orders,employee,Deptment where ID_order=" & cmb_order.SelectedValue.ToString
str &= " and Orders.ID_emp=employee.ID_emp and Deptment.ID_dept=employee.ID_dept"
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
Dim dr As SqlDataReader
Dim com As SqlCommand
com = New SqlCommand()
With com
.CommandType = CommandType.Text
.CommandText = str
.Connection = conn
dr = .ExecuteReader()
dr.Read()
If dr.HasRows Then
cmb_dept.Text = dr(0)
cmb_emp.SelectedValue = dr(1)
End If
End With
conn.Close()
End Sub
Private Sub cmb_dept_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_dept.SelectedIndexChanged
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
Dim str As String = "select * from employee where ID_dept=" & cmb_dept.SelectedValue.ToString
Dim dr1 As SqlDataReader
Dim dt As DataTable
Dim com As SqlCommand
com = New SqlCommand()
With com
.CommandType = CommandType.Text
.CommandText = str
.Connection = conn
dr1 = .ExecuteReader()
If dr1.HasRows Then
dt = New DataTable
dt.Load(dr1)
With cmb_emp
.ValueMember = "ID_emp"
.DisplayMember = "Name"
.DataSource = dt
End With
Else
cmb_order.DataSource = Nothing
End If
End With
dr1.Close()
conn.Close()
End Sub
End Class