Solved

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

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

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 ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

636 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