Solved

sql works in sql express but same query doesn't return data in vb datagridview control

Posted on 2014-09-12
4
414 Views
Last Modified: 2014-10-17
what the app likes likeHello,

I keep posting and then I figure out a piece of the puzzle getting close I think to codeing what I have been posting .First off In wish you could see this but I'd do my best to describe I write a sql query Select* FROM NFLPLAYERS Status ='A' AND Position<> ''" and that loads Datagridview control Now I don't use any binding`These sql queries I write to a textbox to see sql and see if And is positioned Right The query doesn't work however ai TAKE THE QUERY TO SQLSERVER EXPRESS2008 RETURNS DATA FOR AL MY QUERYS.
The one thing I have do is put '' around the value for it to work in sql express if I put '' in vb.net it gives me green line
 Public Sub PlayerID()
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter
        Dim PlayerID As String = tstxt.Text
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        sql = sql & "AND" & " " & "PlayerID= " & PlayerID
        TextBox1.Text = sql
        Try
            connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds)
            connection.Close()
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

Open in new window

IF i COULD PUT '' AROUND PlayerID LIKE   'PlayerID' IT WOULD WORK.
Here is the part I am dealing with am I passing same dataset or data table in each call i feel that's it.
But I can't find any code to help me so please EE HELP this is day four for mr on this problem it's not good we have a deadline.
 Public Sub TeamID()
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter
        Dim Team As String = tstxtid.Text
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        sql = sql & " TeamID=" & " " & Team
        connection = New SqlConnection(connetionString)
        TextBox1.Text = sql
        Try
            connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds1)
            connection.Close()
            DataGridView1.DataSource = ds1.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Public Sub PlayerID()
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter
        Dim PlayerID As String = tstxt.Text
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        sql = sql & "AND" & " " & "PlayerID= " & PlayerID
        TextBox1.Text = sql
        Try
            connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds)
            connection.Close()
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Public Sub Lastname()
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        sql = sql & " AND " & " " & "Lastname like '%" & _
                tstxt.Text & "%'"
        connection = New SqlConnection(connetionString)
        TextBox1.Text = ""
        TextBox1.Text = sql
        Try
            connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds)
            connection.Close()
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Public Sub Position()
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        sql = sql & " AND" & " " & "Position=" & _
                     tscmb.SelectedText
        connection = New SqlConnection(connetionString)
        TextBox1.Text = sql
        Try

            connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds)
            connection.Close()
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Public Sub College()
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter

        Dim colltxt As String = tstxt.Text
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        sql = sql & " AND " & "  " & "College=" & colltxt

        connection = New SqlConnection(connetionString)
        TextBox1.Text = sql
        Try
            connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds)
            connection.Close()
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        ds = Nothing
    End Sub
    Public Sub SetupDataControl()
       
        If (tscmb.SelectedText = "SELECT" Or tscmb.SelectedText <> "" Or tstxt.SelectedText <> "") Then
            Exit Sub
        End If
        If tstxtid.Text <> "" Then
            TeamID()
        End If
        If (tscmb.Text = "College") Then
            College()
        End If
        If (tscmb.Text = "ID") Then
            PlayerID()
            
        End If
        If (tscmb.Text = "ALL") Then

            dataload()
        End If
        If (tscmb.SelectedText = "Position") Then
            Position()
        End If


        If (tscmb.Text = "Lastname") Then
            Lastname()

        End If
       
        TextBox1.Text = sql

        

    End Sub

Open in new window

0
Comment
Question by:powerztom
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
pjg418 earned 500 total points
ID: 40320173
you need to add parameters to your SQL commands to declare the variable before you pass it to SQL, otherwise the command will not be able to match your parameters.

  
Public Function DLookupStrValue(TableName As String, rFieldName As String, wFieldName As String, wFieldValue As String) As Object

        Dim con As New SqlClient.SqlConnection
        Dim cmd As New SqlClient.SqlCommand
        Dim retVal As Object = Nothing

        Try
            con.ConnectionString = "Data Source=FARRARI;Initial Catalog=DealerMate;Integrated Security=True"
            con.Open()
            cmd.Connection = con
            cmd.Parameters.Add("@FieldValue", SqlDbType.VarChar).Value = wFieldValue
            cmd.CommandText = "SELECT " & rFieldName & " FROM " & TableName & " WHERE " & wFieldName & " = @FieldValue"
            retVal = cmd.ExecuteScalar()

        Catch ex As Exception
            Console.WriteLine("ERROR: " & vbCrLf & ex.ToString)
            MsgBox("ERROR: " & vbCrLf & ex.ToString)
        Finally
            cmd.Dispose()
            con.Close()
        End Try

        Return retVal

    End Function

Open in new window


So for your first code snip

Public Sub PlayerID()
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter
        Dim PlayerID As String = tstxt.Text
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        sql = sql & "AND" & " " & "PlayerID= " & PlayerID
        TextBox1.Text = sql
        Try
            connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds)
            connection.Close()
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

Open in new window


I am assuming playerID is some kind of string and that string in your DB is a nvarchar(max) size so your code above you would need to only add this one line:

 adapter.SelectCommand.Parameters.Add("@Player", SqlDbType.NVarChar).Value = PlayerID

and edit this one other:

  Sql = Sql & "AND" & " " & "PlayerID = @Player"

i edited your first snip to the one bellow, this should work for you

 Public Sub PlayerID()
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter
        Dim PlayerID As String = tstxt.Text
        connetionString = "Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus"
        Sql = Sql & "AND" & " " & "PlayerID = @Player"
        TextBox1.Text = sql
        Try
            connection.Open()
            adapter = New SqlDataAdapter(Sql, connection)
            adapter.SelectCommand.Parameters.Add("@Player", SqlDbType.NVarChar).Value = PlayerID
            adapter.Fill(ds)
            connection.Close()
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

Open in new window


lmk
0
 

Author Comment

by:powerztom
ID: 40323985
pjg418,

Today I had to help put out a fire you know what I mean Your code looks promising. I hope tomorrow to use it. How a great night.










pjg418
0
 

Author Closing Comment

by:powerztom
ID: 40338182
Sorry it took so long but I was out the country good work
0
 
LVL 1

Expert Comment

by:pjg418
ID: 40387358
glad to help :) no worries :)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now