Link to home
Start Free TrialLog in
Avatar of Tom Powers
Tom Powers

asked on

left innerjoin sql statement in vb.net

Hey I need to create I guess it's called a Left innerjoin allow me to match a PlayerID on one table called NflPlayers and PlayerID on a second main table called NFLPlayersPass then take firstname  + Lastname in hopefully one datagridview cell but if it can't be done then firstname one cell and then lastname next cell here is the main table code
 Public Sub GetpassingNfl()
        Dim sql As String
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        sql = "Select PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating FROM NFLPlayersPass WHERE PlayerID=" & PlayerID2 & "Order By Season desc"

        dvgPass.DataSource = Nothing
        dvgPass.ClearSelection()



        connection = New SqlConnection(connetionString)
        Dim ds As New DataSet

        Try
            connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds)
            connection.Close()
            dvgPass.DataSource = ds.Tables(0)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        ' ds.Tables(Currentrow) = "Lastname" = txtLname.Text
        ' ds.Tables(Currentrow).Column("College") = txtCollege.Text

        ' txtLname.Text = ds.Tables(Currentrow).TableName("Lastname")
        ' txtCollege.Text = ds.Tables(Currentrow).Item("College")

        Me.dvgPass.RowsDefaultCellStyle.BackColor = Color.Bisque
        Me.dvgPass.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
        dvgPass.ColumnHeadersHeight = 55
        dvgPass.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
        ' Add the image column to the grid.
        ' DataGridView1.Columns.Add(imageCol)

        '  DataGridView1.Columns("Lastname").Frozen = False
        ' dvgPass.Columns("SeasonType").Frozen = True
    End Sub

Open in new window

Thanks EE
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Note that in the current form your code is subject to sql injection.

Imagine if playerID2 were to read "1; drop table nflplayers; --"

The way around this is to parameterize the value.  Your join is included.  Note you may need to alias each column if the column names exist in both.  You can bind to "fullName" in your datagrid.
"Select np.PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating,
FirstName + ' ' + LastName as FullName
FROM 
NflPlayers np
join NFLPlayersPass npp on np.PlayerId = npp.PlayerID
WHERE np.PlayerID= @PlayerID Order By Season desc"


SqlDataAdapter.SelectCommand.AddParameterWithValue("@playerID", PlayerID2 )

Open in new window

Use your SQL Server. Create the JOIN as view or stored procedure.
Avatar of Tom Powers
Tom Powers

ASKER

STE5AN I would love to but I don't know how I'm trying Klye Abrams code I got a sql error near the where clause see screen shot. and thanks for heads up on sql injection but when I setup mySQLDataAdapter As New SqlDataAdapter and SelectCommand As New SqlCommand I get a blue line under
mySQLDataAdapter.SelectCommand.AddParameterWithValue("@playerID", PlayerID2) Any Suggestions

 Public Sub GetpassingNfl()
        Dim sql As String
        Dim mySQLDataAdapter As New SqlDataAdapter
        Dim SelectCommand As New SqlCommand
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        sql = "Select np.PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating,FirstName + ' ' + LastName as FullName FROM() NflPlayers np"
        sql = "join NFLPlayersPass npp on np.PlayerId = npp.PlayerID"
        sql = "WHERE np.PlayerID= @PlayerID Order By Season desc"


        ' mySQLDataAdapter.SelectCommand.AddParameterWithValue("@playerID", PlayerID2)

        dvgPass.DataSource = Nothing
        dvgPass.ClearSelection()



        connection = New SqlConnection(connetionString)
        Dim ds As New DataSet

        Try
            connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds)
            connection.Close()
            dvgPass.DataSource = ds.Tables(0)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        ' ds.Tables(Currentrow) = "Lastname" = txtLname.Text
        ' ds.Tables(Currentrow).Column("College") = txtCollege.Text

        ' txtLname.Text = ds.Tables(Currentrow).TableName("Lastname")
        ' txtCollege.Text = ds.Tables(Currentrow).Item("College")

        Me.dvgPass.RowsDefaultCellStyle.BackColor = Color.Bisque
        Me.dvgPass.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
        dvgPass.ColumnHeadersHeight = 55
        dvgPass.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
        ' Add the image column to the grid.
        ' DataGridView1.Columns.Add(imageCol)

        '  DataGridView1.Columns("Lastname").Frozen = False
        ' dvgPass.Columns("SeasonType").Frozen = True
    End Sub

Open in new window

Kyle what does np and npp mean in your select statement?
Remove the parentheses () here:
FROM() NflPlayers np

What I'm doing is aliasing the tables.

so basically np means NFLplayers
and npp means NFLPlayersPass
i'M STILL GETTING SAME ERROR MESSAGE ABOUT THE INNCORRECT SYNTAX NEAR THE WHERE CLAUSE DO I HAVE A ODD  CHARACTER ALSO i REMMED OUT
 mySQLDataAdapter.SelectCommand.AddParameterWithValue("@playerID", PlayerID2) COULD IT BE THAT CAUSE THIS LINE WAS GIVING ME AND ERROR LINE UNDER  IT.
Public Sub GetpassingNfl()
        Dim sql As String
        Dim mySQLDataAdapter As New SqlDataAdapter
        Dim SelectCommand As New SqlCommand
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        sql = "Select np.PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating,FirstName + ' ' + LastName as FullName FROM NflPlayers np"
        sql = "join NFLPlayersPass npp on np.PlayerId = npp.PlayerID"
        sql = "WHERE np.PlayerID= @PlayerID Order By Season desc"


        ' mySQLDataAdapter.SelectCommand.AddParameterWithValue("@playerID", PlayerID2)

        dvgPass.DataSource = Nothing
        dvgPass.ClearSelection()



        connection = New SqlConnection(connetionString)
        Dim ds As New DataSet

        Try
            connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds)
            connection.Close()
            dvgPass.DataSource = ds.Tables(0)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        ' ds.Tables(Currentrow) = "Lastname" = txtLname.Text
        ' ds.Tables(Currentrow).Column("College") = txtCollege.Text

        ' txtLname.Text = ds.Tables(Currentrow).TableName("Lastname")
        ' txtCollege.Text = ds.Tables(Currentrow).Item("College")

        Me.dvgPass.RowsDefaultCellStyle.BackColor = Color.Bisque
        Me.dvgPass.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
        dvgPass.ColumnHeadersHeight = 55
        dvgPass.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
        ' Add the image column to the grid.
        ' DataGridView1.Columns.Add(imageCol)

        '  DataGridView1.Columns("Lastname").Frozen = False
        ' dvgPass.Columns("SeasonType").Frozen = True
    End Sub

Open in new window

   sql = "Select np.PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating,FirstName + ' ' + LastName as FullName FROM NflPlayers np"
        sql += " join NFLPlayersPass npp on np.PlayerId = npp.PlayerID"
        sql += " WHERE np.PlayerID= @PlayerID Order By Season desc"

Open in new window


you need to append the statement, and you need to allow for spaces.
Kyle  a few things npp represents the stats PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating
and np has FirstName + ' ' + LastName as FullName just grabbing name.
is that screwing up query cause there backwards.
And can I have it appear Fullname first in Datagridview?
here is the code

Imports System.Data
Imports System.Data.SqlClient
Public Class frmPASS
    Dim PlayerID2 As Integer
    Dim Position2 As String
    Public connetionString As String
    Dim connection As SqlConnection
    Dim adapter As SqlDataAdapter
    'Public da As SqlDataAdapter

    Public stgPath As String = "\\tsnfps01\webdev\branding\tompowers\Nflplayers\players\"
    Public stgPathTeam As String = "http://images.sportsnetwork.com/nfl/atthegridiron/Resources/"

    Public Sub GetpassingNfl()
        Dim sql As String
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        connection = New SqlConnection(connetionString)
        sql = "Select np.PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating,FirstName + ' ' + LastName as FullName FROM NflPlayers np"
        sql += "join NFLPlayersPass npp on np.PlayerID = npp.PlayerID"
        sql += "WHERE np.PlayerID= @PlayerID Order By Season desc"
        Dim SelectCommand As New SqlCommand(sql, connection)
        da.SelectCommand.Parameters.AddWithValue("@PlayerID", PlayerID2)
        
            Try
                connection.Open()
                adapter = New SqlDataAdapter(sql, connection)
                adapter.Fill(ds)
                connection.Close()
                dvgPass.DataSource = ds.Tables(0)

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

            ' ds.Tables(Currentrow) = "Lastname" = txtLname.Text
            ' ds.Tables(Currentrow).Column("College") = txtCollege.Text

            ' txtLname.Text = ds.Tables(Currentrow).TableName("Lastname")
            ' txtCollege.Text = ds.Tables(Currentrow).Item("College")

            Me.dvgPass.RowsDefaultCellStyle.BackColor = Color.Bisque
            Me.dvgPass.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
            dvgPass.ColumnHeadersHeight = 55
            dvgPass.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
            ' Add the image column to the grid.
            ' DataGridView1.Columns.Add(imageCol)

            '  DataGridView1.Columns("Lastname").Frozen = False
            ' DataGridView1.Columns("Lastname").Frozen = True



        ' dvgPass.DataSource = Nothing
        ' dvgPass.ClearSelection()



           
            ' ds.Tables(Currentrow) = "Lastname" = txtLname.Text
            ' ds.Tables(Currentrow).Column("College") = txtCollege.Text

            ' txtLname.Text = ds.Tables(Currentrow).TableName("Lastname")
            ' txtCollege.Text = ds.Tables(Currentrow).Item("College")

            Me.dvgPass.RowsDefaultCellStyle.BackColor = Color.Bisque
            Me.dvgPass.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
            dvgPass.ColumnHeadersHeight = 55
            dvgPass.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
            ' Add the image column to the grid.
            ' DataGridView1.Columns.Add(imageCol)

            '  DataGridView1.Columns("Lastname").Frozen = False
            dvgPass.Columns("SeasonType").Frozen = True
    End Sub









   
    Private Sub frmPASS_Load(sender As Object, e As EventArgs) Handles Me.Load
        lblName.Text = Form1.txtFname.Text & Form1.txtLname.Text & Form1.txtPosition.Text
        PlayerID2 = CInt(Form1.Playerid1)
        Position2 = Form1.txtPosition.Text

        GetpassingNfl()
        
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        picplayer.ImageLocation = Form1.stgPath & Form1.txtPlayerID.Text & ".jpg"
        picTeam.ImageLocation = Form1.stgPathTeam & Form1.txtTeamID.Text & ".png"
    End Sub
End Class[embed=file 875247]

Open in new window

Kyle thanks for your help so far.
Sqlexception.png
Actually it just hangs if aI rem out
 da.SelectCommand.Parameters.AddWithValue("@PlayerID", PlayerID2) set a breakpoint on this line and PlayerID2 HAD THE CORRECT PlayerID. ANY SUGGESTIONS i COULD SURE USE IT.
sql = "Select np.PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating,FirstName + ' ' + LastName as FullName FROM NflPlayers np"
        sql += "[SPACE HERE] join NFLPlayersPass npp on np.PlayerID = npp.PlayerID"
        sql += "[SPACE HERE] WHERE np.PlayerID= @PlayerID Order By Season desc"



 sql = "Select np.PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating,FirstName + ' ' + LastName as FullName FROM NflPlayers np"
        sql += " join NFLPlayersPass npp on np.PlayerID = npp.PlayerID"
        sql += " WHERE np.PlayerID= @PlayerID Order By Season desc"

Open in new window

kYLE MY FRIEND This has been a battle I was getting @PLayer SQLexception error then I added the parameterized query now It just Freezes for like five seconds then Terminates no SQL Error Message.
here is code which I'm sure something I did not your code
Imports System.Data
Imports System.Data.SqlClient
Public Class frmPASS
    Dim PlayerID2 As Integer
    Dim Position2 As String
    Public connetionString As String
    Dim connection As SqlConnection
    Dim adapter As SqlDataAdapter
    'Public da As SqlDataAdapter

    Public stgPath As String = "\\tsnfps01\webdev\branding\tompowers\Nflplayers\players\"
    Public stgPathTeam As String = "http://images.sportsnetwork.com/nfl/atthegridiron/Resources/"

    Public Sub GetpassingNfl()
        Dim sql As String
        Dim ds As New DataSet
        Dim mySQLDATAAdapter As New SqlDataAdapter
        Dim SelectCommand As New SqlCommand
        ' Dim da As New SqlDataAdapter
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        connection = New SqlConnection(connetionString)
        mySQLDATAAdapter.SelectCommand.Parameters.AddWithValue("@PlayerID", PlayerID2)
        sql = "Select np.PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating,FirstName + ' ' + LastName as FullName FROM NflPlayers np"
        sql += " join NFLPlayersPass npp on np.PlayerID = npp.PlayerID"
        sql += " WHERE np.PlayerID= @PlayerID Order By Season desc"

        '  sql = "Select PlayerID,Team,Season,Seasontype,Conference,Att,Comp,Yards,Long,TD,Inter,Sacks,SKYds,Rating FROM NflPlayersPass"

        ' sql += " WHERE PlayerID=" & PlayerID2 & "Order By Season desc"
        
        
            Try
                connection.Open()
                adapter = New SqlDataAdapter(sql, connection)
                adapter.Fill(ds)
                connection.Close()
                dvgPass.DataSource = ds.Tables(0)

        Catch ex As SqlException
            MsgBox(ex.ToString)
            End Try

            ' ds.Tables(Currentrow) = "Lastname" = txtLname.Text
            ' ds.Tables(Currentrow).Column("College") = txtCollege.Text

            ' txtLname.Text = ds.Tables(Currentrow).TableName("Lastname")
            ' txtCollege.Text = ds.Tables(Currentrow).Item("College")

            Me.dvgPass.RowsDefaultCellStyle.BackColor = Color.Bisque
            Me.dvgPass.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
            dvgPass.ColumnHeadersHeight = 55
            dvgPass.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
            ' Add the image column to the grid.
            ' DataGridView1.Columns.Add(imageCol)

            '  DataGridView1.Columns("Lastname").Frozen = False
            ' DataGridView1.Columns("Lastname").Frozen = True



        ' dvgPass.DataSource = Nothing
        ' dvgPass.ClearSelection()



           
            ' ds.Tables(Currentrow) = "Lastname" = txtLname.Text
            ' ds.Tables(Currentrow).Column("College") = txtCollege.Text

            ' txtLname.Text = ds.Tables(Currentrow).TableName("Lastname")
            ' txtCollege.Text = ds.Tables(Currentrow).Item("College")

            Me.dvgPass.RowsDefaultCellStyle.BackColor = Color.Bisque
            Me.dvgPass.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
            dvgPass.ColumnHeadersHeight = 55
            dvgPass.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
            ' Add the image column to the grid.
            ' DataGridView1.Columns.Add(imageCol)

            '  DataGridView1.Columns("Lastname").Frozen = False
            dvgPass.Columns("SeasonType").Frozen = True
    End Sub









   
    Private Sub frmPASS_Load(sender As Object, e As EventArgs) Handles Me.Load
        lblName.Text = Form1.txtFname.Text & Form1.txtLname.Text & Form1.txtPosition.Text
        PlayerID2 = CInt(Form1.Playerid1)
        Position2 = Form1.txtPosition.Text

        GetpassingNfl()
        
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        picplayer.ImageLocation = Form1.stgPath & Form1.txtPlayerID.Text & ".jpg"
        picTeam.ImageLocation = Form1.stgPathTeam & Form1.txtTeamID.Text & ".png"
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()


    End Sub
End Class

Open in new window

Kyle I truely appreciates your efforts cause I need to do this join in more forms
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kyle you are awesome. I know I've make novice mistakes and you are patience enough to continue to help. I saw your work history. You are a God when it comes to this. Thank You my Friend.
Kyle everything is great but I found one thing I screwed up I should have noticed. So I'm gonna put in a new ticket. Joining one more table with a field I need.