0 Replies - 652 Views - Last Post: 26 August 2011 - 11:08 AM

#1 Bladewing51   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 79
  • Joined: 26-August 11

SQL wont see variable VB.net

Posted 26 August 2011 - 11:08 AM

This is my first time posting so if you need more info or I've missed something that I should have posted please let me know

I'm building a program that will search for items in a SQL 2008 database, I plan to at some point in the development create a textbox that can be used to search for a user defined value but for simplicitys sake while during development I'm using a static variable declared under a public module. Whats happening is I'm having problems making the SQL server see my Public variable, when I start the program my label will show the variables value but when I click the search tool box that is created after I put in my query it clears the entire form of records. Even when I build the query in the query builder it still shows Null values.

The database is connected as I'm able to create a static value in the SQL query and it will find the records I need but when using a variable declared in my module or from the textbox it comes up blank.

I cannot think what I may be missing, I've searched the web but other examples shown dont seem to work for my program.

MODULE
Module Module1
    Public Upc1 As Integer

End Module



FORM1 CODE
 

ublic Class Form1

    Private Sub CarsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CarsBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.CarsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.CarsDatabaseDataSet)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CarsDatabaseDataSet.Versions' table. You can move, or remove it, as needed.
        Me.VersionsTableAdapter.Fill(Me.CarsDatabaseDataSet.Versions)
        'TODO: This line of code loads data into the 'CarsDatabaseDataSet.models' table. You can move, or remove it, as needed.
        Me.ModelsTableAdapter.Fill(Me.CarsDatabaseDataSet.models)
        'TODO: This line of code loads data into the 'CarsDatabaseDataSet.cars' table. You can move, or remove it, as needed.
        Me.CarsTableAdapter.Fill(Me.CarsDatabaseDataSet.cars)

        Upc1 = 14
        LblUpc1.Text = Upc1
    End Sub

    Private Sub GetAllToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetAllToolStripButton.Click
        Try
            Me.CarsTableAdapter.GetAll(Me.CarsDatabaseDataSet.cars)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub GetClassicsToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetClassicsToolStripButton.Click
        Try
            Me.CarsTableAdapter.GetClassics(Me.CarsDatabaseDataSet.cars)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub GetSUVToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetSUVToolStripButton.Click
        Try
            Me.CarsTableAdapter.GetSUV(Me.CarsDatabaseDataSet.cars)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub SearchToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Me.CarsTableAdapter.search(Me.CarsDatabaseDataSet.cars, "Upc1")
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Search1ToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search1ToolStripButton.Click
        Try
            Me.CarsTableAdapter.search1(Me.CarsDatabaseDataSet.cars, Upc1ToolStripTextBox.Text)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub Upc1ToolStripLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Upc1ToolStripLabel.Click

    End Sub
End Class




The SQL QUERY is as follows
SELECT        *
FROM            cars
WHERE        (Note = @Upc1)



Now I dont know if this makes any difference but when I build my query in the SQL query builder in VS2010 it opens a new window called Query Peramiters and displays two fields NAME (This being already populated as the variable Upc1) and Value (Which has a NULL value) I tried making this VALUE @Upc1 or just Upc1 or Textbox.text but I get the same issue where the forms get cleared.

I can only assume that the variable is not getting seen by the query and instead placing a NULL value into my search and giving back only NULL values aswell.

Thank you

Is This A Good Question/Topic? 0
  • +

Page 1 of 1