Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problem with syntax with Wizard in a Select/Where statement

Problem with syntax with Wizard in a Select/Where statement

Scheduled Pinned Locked Moved Visual Basic
helpworkspacecsharpdatabase
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peter Leipzig
    wrote on last edited by
    #1

    I am using vb.net 2003 for a stand alone windows program. I access an Access 2002 database file via Jet 4.0 and OleDbDataAdapter. The program is like a "check book" for commercial fishermen to track their landings in relation to the amount of "quota" that they have been assigned. The program is structured so a fisherman can enter multiple boat names and track each one seperately. To work on a particular boats account, the fisherman would select a boat name from a drop down list. The boat name is then captured and assigned the string variable name of "xid" and in a Module have this code:

    Public xid as String

    I used the DataAdapter Configuration Wizard to setup the data connection and assign the DataAdapter and DataSet. I went into Wizard generated code and manually entered a WHERE clause to filter the data for the selected boat. Doing this the program works fine. The code that works is:

    ... WHERE [Boat Name] = '" & xid & "'

    . However, if I return to the Designer view I get an error message: The variable 'xid' is either undeclared or was never assigned. And the entire SELECT statement is erased. If I try letting the Wizard generate the WHERE clause when I enter:

    ... WHERE [Boat Name] = '" & xid & "'

    , the code that is generated is has double double quotas as:

    ... WHERE [Boat Name] = '"" & xid & ""'

    , and this does not show data on my form. I have gone through every possible combination of single quote and double quotes and can not get the wizard to produce the syntax that works. And if I manually edit the wizard generated code the program works, but I can not return to the Designer view. Does any one have a suggestion or solution?

    J 1 Reply Last reply
    0
    • P Peter Leipzig

      I am using vb.net 2003 for a stand alone windows program. I access an Access 2002 database file via Jet 4.0 and OleDbDataAdapter. The program is like a "check book" for commercial fishermen to track their landings in relation to the amount of "quota" that they have been assigned. The program is structured so a fisherman can enter multiple boat names and track each one seperately. To work on a particular boats account, the fisherman would select a boat name from a drop down list. The boat name is then captured and assigned the string variable name of "xid" and in a Module have this code:

      Public xid as String

      I used the DataAdapter Configuration Wizard to setup the data connection and assign the DataAdapter and DataSet. I went into Wizard generated code and manually entered a WHERE clause to filter the data for the selected boat. Doing this the program works fine. The code that works is:

      ... WHERE [Boat Name] = '" & xid & "'

      . However, if I return to the Designer view I get an error message: The variable 'xid' is either undeclared or was never assigned. And the entire SELECT statement is erased. If I try letting the Wizard generate the WHERE clause when I enter:

      ... WHERE [Boat Name] = '" & xid & "'

      , the code that is generated is has double double quotas as:

      ... WHERE [Boat Name] = '"" & xid & ""'

      , and this does not show data on my form. I have gone through every possible combination of single quote and double quotes and can not get the wizard to produce the syntax that works. And if I manually edit the wizard generated code the program works, but I can not return to the Designer view. Does any one have a suggestion or solution?

      J Offline
      J Offline
      Johan Hakkesteegt
      wrote on last edited by
      #2

      The problem is that you are trying to apply Access techniques on vb.net. The wizard does not, and will not ever understand that WHERE [Boat Name] = '" & xid & "' means that you want to use a variable. Try writing the sql sentence in the wizard, and apply your variable like so: WHERE [Boat Name] = @xid. Then, in your vb.net code, before you call the DataAdapter.Fill method pass your variable's value to the adapter's parameter collection. Something like: DataAdapter.SelectCommand.Parameters.Add("@xid", NVarChar).Value = xid (you probably have to check for the correct syntax, but something along these lines anyway) As an added bonus you will be using the safe way to use parameterized queries (much less risk of an sql injection attack).

      My advice is free, and you may get what you paid for.

      P 1 Reply Last reply
      0
      • J Johan Hakkesteegt

        The problem is that you are trying to apply Access techniques on vb.net. The wizard does not, and will not ever understand that WHERE [Boat Name] = '" & xid & "' means that you want to use a variable. Try writing the sql sentence in the wizard, and apply your variable like so: WHERE [Boat Name] = @xid. Then, in your vb.net code, before you call the DataAdapter.Fill method pass your variable's value to the adapter's parameter collection. Something like: DataAdapter.SelectCommand.Parameters.Add("@xid", NVarChar).Value = xid (you probably have to check for the correct syntax, but something along these lines anyway) As an added bonus you will be using the safe way to use parameterized queries (much less risk of an sql injection attack).

        My advice is free, and you may get what you paid for.

        P Offline
        P Offline
        Peter Leipzig
        wrote on last edited by
        #3

        Thanks for the reply. I would like to get this to work properly. This is my first attempt at programing with vb.net and using a database. In the past all of my projects used text files for input an writing. This is very different. I made the change to the WHERE clause in the Wizard to

        ... WHERE [Boat Name] = @xid

        Now in my form I have this code:

        OleDbDataAdapter1.SelectCommand.Parameters.Add("@xid", NVarChar).Value = xid
        OleDbDataAdapter1.Fill(QuotaPounds1.Balance)

        NVarChar is underlined with the explanation that it is not declared. If I edit this to VarChar it remains underlined with the explanation that it is not declared. If I make it Char, it is still underline but now the explanation is that it is "a Type and so is not a valid expression". I thought that the value in that spot was supposed to be a "Type". Any guidance you can provide on the syntax here would be most appreciated.

        J 1 Reply Last reply
        0
        • P Peter Leipzig

          Thanks for the reply. I would like to get this to work properly. This is my first attempt at programing with vb.net and using a database. In the past all of my projects used text files for input an writing. This is very different. I made the change to the WHERE clause in the Wizard to

          ... WHERE [Boat Name] = @xid

          Now in my form I have this code:

          OleDbDataAdapter1.SelectCommand.Parameters.Add("@xid", NVarChar).Value = xid
          OleDbDataAdapter1.Fill(QuotaPounds1.Balance)

          NVarChar is underlined with the explanation that it is not declared. If I edit this to VarChar it remains underlined with the explanation that it is not declared. If I make it Char, it is still underline but now the explanation is that it is "a Type and so is not a valid expression". I thought that the value in that spot was supposed to be a "Type". Any guidance you can provide on the syntax here would be most appreciated.

          J Offline
          J Offline
          Johan Hakkesteegt
          wrote on last edited by
          #4

          You can check from the intellisense what sort of object or value the code is expecting. In this case SqlDbType.NVarChar. To see how it works, don't paste my code in there but type in SqlDbType. and see what the editor suggests. If the editor does not suggest anything, or the code still gets underlined, go all the way to the top of your code, above the Public Class BlaBlaBla bit, and add: Imports System.Data.SqlClient (so this should be the very first line of code), and then try again.

          My advice is free, and you may get what you paid for.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups