Solved

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

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

825 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