SQL Query Data Join Multiple Tables

Dear Experts-Exchange,

Please see the SQL tables below and SQL/VB.NET query syntax:

tblDepartments                              

DeptID  |   Department_Name
1                     DeptA
2                     DeptB
3                     DeptC

tblEmployee

EmplID | LName |  FName  | DeptID
5              Fish         Paul            2
9              Mark       Jill               3
12            Lois         Phil             1

tblUsers

UserID     EmplID    Password    Admin
pfish            5              lalala            0
jmark           9              vvvvvv         2
plois            12             tttttt             1

Now, the SQL query syntax in vb.net code:

strSQL = "SELECT empl.EmplID as [EmployeeID], " & _
                "empl.LName as [LastName], " & _
                "empl.FName as [FirstName], " & _
                "empl.Department as [Department], " & _
                "user.UserID as [UserID], " & _
                "user.Password as [Password], " & _
                "user.Administrator as [Administrator] " & _
                "FROM employee as empl left outer join users as user on " & _
                "empl.emplid=user.emploid where empl.EmplID = '" & e.CommandArgument & "'"


It’s working good, but I am trying to add the tbldepartments to be joined with other tables in SQL syntax

strSQL = "SELECT empl.EmplID as [EmployeeID], " & _
                "empl.LName as [LastName], " & _
                "empl.FName as [FirstName], " & _
                "empl.Department as [Department], " & _
                "user.UserID as [UserID], " & _
                "user.Password as [Password], " & _
                "user.Administrator as [Administrator], " & _
                “dept.DeptID as [DeptID]                
                “dept.Department_Name as [DeptName] “ & _
                "FROM employee as empl left outer join users as user INNER JOIN departments as dept on " & _
                "empl.emplid=user.emplid where empl.EmplID = '" & e.CommandArgument & "'"

strCnn = System.Configuration.ConfigurationManager.AppSettings("ABC")
            cnn = New SqlConnection(strCnn)

            cmd = New SqlCommand(strSQL, cnn)
            cnn.Open()

            dr = cmd.ExecuteReader(CommandBehavior.SingleRow Or CommandBehavior.CloseConnection)

            Do While dr.Read
                If dr("EmplID") Is DBNull.Value Then
                    Me.LblEmplID.Text = String.Empty
                Else
                    Me.LblEmplID.Text = dr("EmplID")
                End If
                If dr("LName") Is DBNull.Value Then
                    Me.TxtLName.Text = String.Empty
                Else
                    Me.TxtLName.Text = dr("LName")
                End If
                If dr("FName") Is DBNull.Value Then
                    Me.TxtFname.Text = String.Empty
                Else
                    Me.TxtFname.Text = dr("FName")
                End If
                If dr("UserID") Is DBNull.Value Then
                    Me.TxtUserID.Text = String.Empty
                Else
                    Me.TxtUserID.Text = dr("UserID")
                End If
                If dr("Password") Is DBNull.Value Then
                    Me.TxtUpdPassword.Text = String.Empty
                Else
                    Me.TxtUpdPassword.Text = dr("Password")
                End If

                If dr("Department_Name") Is DBNull.Value Then
                    Dim li As New ListItem(dr("Department_Name"))
                    li.Value = dr("Deptid")
                    Me.ddlDep.Items.Add(li)
                End If
            Loop
dr.Close()
        End If

This SQL Syntax is not working.  What am I doing wrong?  Your attention and help is much appreciative. Thanks.


slb2008Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

srikanthreddyn143Commented:
Join was not proper with department

SELECT empl.EmplID as [EmployeeID], " & _
                "empl.LName as [LastName], " & _
                "empl.FName as [FirstName], " & _
                "empl.Department as [Department], " & _
                "user.UserID as [UserID], " & _
                "user.Password as [Password], " & _
                "user.Administrator as [Administrator], " & _
                “dept.DeptID as [DeptID]                
                “dept.Department_Name as [DeptName] “ & _
                "FROM employee as empl left outer join users as useron " & _
                "empl.emplid=user.emplid  INNER JOIN departments as dept  on dept.DeptID= empl.DeptID where empl.EmplID = '" & e.CommandArgument & "'"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slb2008Author Commented:
srikanthereddy, Thanks for your quick response.
I am going to test the sql syntax you sent.  I'll let you know soon.  thanks.
0
slb2008Author Commented:
I am back to tell you the SQL Syntax works very well, but doesn't display the data on department field using VB.NET :
strCnn = System.Configuration.ConfigurationManager.AppSettings("ABC")
            cnn = New SqlConnection(strCnn)

            cmd = New SqlCommand(strSQL, cnn)
            cnn.Open()

            dr = cmd.ExecuteReader(CommandBehavior.SingleRow Or CommandBehavior.CloseConnection)

            Do While dr.Read
                If dr("EmplID") Is DBNull.Value Then
                    Me.LblEmplID.Text = String.Empty
                Else
                    Me.LblEmplID.Text = dr("EmplID")
                End If
                If dr("LName") Is DBNull.Value Then
                    Me.TxtLName.Text = String.Empty
                Else
                    Me.TxtLName.Text = dr("LName")
                End If
                If dr("FName") Is DBNull.Value Then
                    Me.TxtFname.Text = String.Empty
                Else
                    Me.TxtFname.Text = dr("FName")
                End If
                If dr("UserID") Is DBNull.Value Then
                    Me.TxtUserID.Text = String.Empty
                Else
                    Me.TxtUserID.Text = dr("UserID")
                End If
                If dr("Password") Is DBNull.Value Then
                    Me.TxtUpdPassword.Text = String.Empty
                Else
                    Me.TxtUpdPassword.Text = dr("Password")
                End If

   HERE IS NOT WORKING--->             If dr("Department_Name ") Is DBNull.Value Then
                    Dim li As New ListItem(dr("Department_Name"))
                    li.Value = dr("Deptid")
                    Me.ddlDep.Items.Add(li)   <----
Display an error :  Object reference not set to an instance of an object....
                End If
            Loop
dr.Close()
        End If
 
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

srikanthreddyn143Commented:
You should use DeptName rather than Department_Name
0
slb2008Author Commented:
That's right.  
Well you helped me to solved my little problem.
The SQL Syntax was great.  You will take 500 points.  Thanks so much for your help.
slb2008
0
slb2008Author Commented:
helpful.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.