문제

I have three tables: users, departments and user_department.

I have an ASP.NET page, 'Departments' as shown below:

enter image description here

When I click an item on the DropDownList I want to be able to display all the department members would be shown in the ListBox (through user_department).

Database:

enter image description here

What SQL command do I have to perform?

This is what I tried:

SELECT 
    u.username
FROM users u
INNER JOIN user_department ud ON u.user_id = ud.user_id
WHERE ud.department_id = @parameter

VB.NET

 Protected Sub drpDepartments_SelectedIndexChanged(sender As Object, e As EventArgs) Handles drpDepartments.SelectedIndexChanged

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim deptComm As String = "SELECT department_id FROM departments WHERE department_name = @DepartmentName"

        Dim deptSQL As New SqlCommand
        Dim dr As SqlDataReader = deptSQL.ExecuteReader()

        deptSQL = New SqlCommand(deptComm, conn)
        deptSQL.Parameters.AddWithValue("@DepartmentName", drpDepartments.SelectedItem.Text)

        dr.Read()

        If dr.HasRows Then


            Dim department_id As Integer = Convert.ToInt32(dr("department_id"))
            Session("DepartmentID") = department_id

        End If

        dr.Close()
        conn.Close()

However, they are not working.

Do you have any idea how I can solve this?

도움이 되었습니까?

해결책

try this....

SELECT * FROM users u
INNER JOIN 
(
Select x.user_id as userid,x.department_id,y.department_name,y.deparment_code
 from user_Department x 
inner join departments y 
on x.department_id=y.department_id WHERE x.department_id=@yourparameter
) 
    f on u.user_id= f.userid
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top