Professional Web Applications Themes

Help a newbie! How do I SELECT using a string as a WHERE clause? - ASP Database

Hi, I'm having a problem. I've found that running a recordset with the query: SELECT * FROM user; works fine, but as soon as I add a WHERE clause, I get a Jet error. (too few parameters). I suspect the syntax may be faulty, but I don't know. This is what I've got: 1 .strSQL = "SELECT * FROM user WHERE username=" & Request.QueryString("username") & ";" 2. objRecordSet.Open strSQL,, DB_CONNECTION_STRING, adOpenKeyset, adLockPessimistic, adCmdText so the string in 1. is being used as the parameter for the command in 2. Only problem is this returns a Too Few Paramters Jet error. ...

  1. #1

    Default Help a newbie! How do I SELECT using a string as a WHERE clause?

    Hi,

    I'm having a problem.

    I've found that running a recordset with the query:

    SELECT * FROM user;

    works fine, but as soon as I add a WHERE clause, I get a Jet error.
    (too few parameters).

    I suspect the syntax may be faulty, but I don't know. This is what
    I've got:

    1 .strSQL = "SELECT * FROM user WHERE username=" &
    Request.QueryString("username") & ";"

    2. objRecordSet.Open strSQL,, DB_CONNECTION_STRING, adOpenKeyset,
    adLockPessimistic, adCmdText

    so the string in 1. is being used as the parameter for the command in
    2.

    Only problem is this returns a Too Few Paramters Jet error. As soon
    as I change this to a simple query, as I said before, this works.

    Do I need speech marks in the query?

    Help!

    Pablo.
    Pablo Contreras Guest

  2. #2

    Default Re: Help a newbie! How do I SELECT using a string as a WHERE clause?

    Unless your usernames are numeric, you'll need to put single quotes
    around the variable. Like so:

    strUserName = request.querystring("username")
    ' insert code to check that this actually is a username and not an empty
    string or whatever
    strSQL = "Select field1, field2 from user where username = '" &
    strUserName & "'"

    A couple of other things:
    1) You probably don't need those terminating semicolons. Have you tried
    without?
    2) Select * wastes resources on both ends -- the database needs to
    translate that into columns before retrieving them, and the server
    (probably) stores columns you don't need. So you may want to just list
    the columns you need.

    "Pablo Contreras" <pablopabs2003.plus.com> wrote in message
    news:d48ohvsbt24cm073rhcj354c3m2imfdput4ax.com...
    > Hi,
    >
    > I'm having a problem.
    >
    > I've found that running a recordset with the query:
    >
    > SELECT * FROM user;
    >
    > works fine, but as soon as I add a WHERE clause, I get a Jet error.
    > (too few parameters).
    >
    > I suspect the syntax may be faulty, but I don't know. This is what
    > I've got:
    >
    > 1 .strSQL = "SELECT * FROM user WHERE username=" &
    > Request.QueryString("username") & ";"
    >
    > 2. objRecordSet.Open strSQL,, DB_CONNECTION_STRING, adOpenKeyset,
    > adLockPessimistic, adCmdText
    >
    > so the string in 1. is being used as the parameter for the command in
    > 2.
    >
    > Only problem is this returns a Too Few Paramters Jet error. As soon
    > as I change this to a simple query, as I said before, this works.
    >
    > Do I need speech marks in the query?
    >
    > Help!
    >
    > Pablo.

    Kris Eiben Guest

Similar Threads

  1. where clause as variable string
    By RichardG in forum Coldfusion Database Access
    Replies: 2
    Last Post: April 28th, 09:54 PM
  2. Using 'IN' clause with mutli-select listbox
    By TheresaK in forum Dreamweaver AppDev
    Replies: 7
    Last Post: March 8th, 07:44 PM
  3. Select from table where field = string
    By Michel in forum PHP Development
    Replies: 6
    Last Post: August 24th, 06:35 PM
  4. string constants in select
    By ramesh.vasudevan@verizon.com in forum Informix
    Replies: 3
    Last Post: August 8th, 02:35 PM
  5. DataRow[] and Select(filter with a like clause) question
    By Ron Vecchi in forum ASP.NET General
    Replies: 2
    Last Post: July 30th, 01:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139