Solved

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

Posted on 2014-09-12
4
410 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

760 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

18 Experts available now in Live!

Get 1:1 Help Now