Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-09-12
4
Medium Priority
?
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
pjg418 earned 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

670 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