Convention for writing parameters in CommandText

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Convention for writing parameters in CommandText

    Hi,

    This function always return and empty RecordSet (R.eof=True) the problem
    is probably the way I pass the parameter in the CommandText. To avoid
    looking at all code first look at this I think my mistake is
    '[@prmClientNo]' but it does not give any error message. Any help will
    be appreciate!

    // code from the function below
    Call R.Open("SELECT c.COMP_NAME, c.CLIENT_NO, cr.NAME FROM CLIENT AS c
    " & _
    "LEFT JOIN CLIENT_REF AS cr ON c.CLIENT_NO = cr.CLIENT_NO "
    & _
    "WHERE c.CLIENT_NO = '[@prmClientNo]' " & _
    "ORDER BY NAME ASC", conn, adOpenForwardOnly,
    adLockReadOnly, _
    adCmdText)


    //************************************************** ********************************

    function ClientNoIsValid(sClientNo)
    dim conn
    dim R
    dim F
    dim prm
    dim cmd
    set conn = Server.CreateObject("ADODB.Connection")
    set R = server.CreateObject("ADODB.RecordSet")
    set cmd = Server.CreateObject("ADODB.Command")

    conn.ConnectionString="Provider=Microsoft.Jet.OLED B.4.0;Data Source=" &
    _
    Server.MapPath("Data\WEB_PROD.mdb") & ";Persist
    Security Info=True"
    conn.CursorLocation=adUseClient
    conn.Mode= adModeRead
    conn.Open
    Set cmd.ActiveConnection = Conn
    set prm = cmd.CreateParameter("@prmClientNo", adChar, adParamInput, 13)

    cmd.Parameters.Append prm
    cmd.Parameters("@prmClientNo").Value = sClientNo
    cmd.Parameters.Refresh

    R.CursorLocation=adUseClient
    Call R.Open("SELECT c.COMP_NAME, c.CLIENT_NO, cr.NAME FROM CLIENT AS c
    " & _
    "LEFT JOIN CLIENT_REF AS cr ON c.CLIENT_NO = cr.CLIENT_NO "
    & _
    "WHERE c.CLIENT_NO = '[@prmClientNo]' " & _
    "ORDER BY NAME ASC", conn, adOpenForwardOnly,
    adLockReadOnly, _
    adCmdText)
    ClientNoIsValid = not R.eof
    Response.Write R.eof & "<br>"
    'Response.Write R.Fields("COMP_NAME").Value & " " &
    R.Fields("NAME").Value & "<BR>"
    Set conn = Nothing
    Set R = Nothing

    end function


    //*******************************

    Thank's in advance
    serge


    Serge Myrand Guest

  2. Similar Questions and Discussions

    1. Writing a DIV tag?
      Starting to mess with writing to the HtmlTextWriter, and have a question about divs. Why is it that HtmlTextWriterTag.Div writes a *table*, when...
    2. build CommandText string->error!!!
      your sqlcmd is out of scope, once you closed the } all the information about the sqlcmd is dumped and no reference to it exists
    3. Writing to jpg fix?
      I just switched from Quark. Now when I'm placing any kind of graphic (pdf or tiff), when In Design places the graphic, a dialog box comes up that...
    4. Website File Naming Convention-Pub2003
      Have no problem loading website to ISP. Publisher names each page of the website index_page561xxx.htm, etc. How do I get it to name the page by...
    5. create parameters without creating parameters
      cant you create ado command parameteres without creating a parameter object? i have a function that takes the name of a stored proc, and two...
  3. #2

    Default Re: Convention for writing parameters in CommandText

    Thank you very much,

    I will use 'saved parameter query' when I will be able to do everything by hand.





    "Bob Barrows [MVP]" wrote:
    > Serge Myrand wrote:
    > > Hi,
    > >
    > > This function always return and empty RecordSet (R.eof=True) the
    > > problem
    > > is probably the way I pass the parameter in the CommandText. To avoid
    > > looking at all code first look at this I think my mistake is
    > > '[@prmClientNo]'
    >
    > True.
    > When using a text sql statement, you have to use the ODBC ? parameter
    > placeholder, like this:.
    >
    > sSQL = "SELECT ... WHERE c.CLIENT_NO = ? ..."
    >
    > > but it does not give any error message. Any help will
    > > be appreciate!
    > >
    > > // code from the function below
    > > Call R.Open("SELECT c.COMP_NAME, c.CLIENT_NO, cr.NAME FROM CLIENT AS
    > > c " & _
    > > "LEFT JOIN CLIENT_REF AS cr ON c.CLIENT_NO =
    > > cr.CLIENT_NO " & _
    > > "WHERE c.CLIENT_NO = '[@prmClientNo]' " & _
    > > "ORDER BY NAME ASC", conn, adOpenForwardOnly,
    > > adLockReadOnly, _
    > > adCmdText)
    > >
    > You just undid all of your hard work in building the Command object. See
    > below
    >
    > >
    > >
    > //************************************************** ************************
    > ********
    > >
    > > function ClientNoIsValid(sClientNo)
    > > dim conn
    > > dim R
    > > dim F
    > > dim prm
    > > dim cmd
    > > set conn = Server.CreateObject("ADODB.Connection")
    > > set R = server.CreateObject("ADODB.RecordSet")
    > > set cmd = Server.CreateObject("ADODB.Command")
    > >
    > > conn.ConnectionString="Provider=Microsoft.Jet.OLED B.4.0;Data
    > > Source=" & _
    > > Server.MapPath("Data\WEB_PROD.mdb") & ";Persist
    > > Security Info=True"
    > > conn.CursorLocation=adUseClient
    > > conn.Mode= adModeRead
    > > conn.Open
    > > Set cmd.ActiveConnection = Conn
    > > set prm = cmd.CreateParameter("@prmClientNo", adChar, adParamInput,
    > > 13)
    > >
    > > cmd.Parameters.Append prm
    > > cmd.Parameters("@prmClientNo").Value = sClientNo
    >
    > NO! Get rid of the following line:
    > ************************************
    > > cmd.Parameters.Refresh
    > **************************************
    > >
    > > R.CursorLocation=adUseClient
    >
    > I look at the following and all I can say is: "Incredible!" Why did you
    > build the Command object if you weren't going to use it??
    >
    > > Call R.Open("SELECT c.COMP_NAME, c.CLIENT_NO, cr.NAME FROM CLIENT AS
    > > c " & _
    > > "LEFT JOIN CLIENT_REF AS cr ON c.CLIENT_NO =
    > > cr.CLIENT_NO " & _
    > > "WHERE c.CLIENT_NO = '[@prmClientNo]' " & _
    > > "ORDER BY NAME ASC", conn, adOpenForwardOnly,
    > > adLockReadOnly, _
    > > adCmdText)
    >
    > 1) Since you are using a client-side cursor, the only cursor-type available
    > is a static cursor. If you really want a forward-only cursor (and it appears
    > that you do), you need to get rid of the lines where you set the
    > cursorlocation to adUseClient.
    >
    > Let's modify your code as follows:
    >
    > dim conn
    > dim R
    > dim F 'What is this?
    > dim sSQL
    > dim prm
    > dim cmd
    > sSQL = "SELECT ... WHERE c.CLIENT_NO = ? ..."
    > set conn = Server.CreateObject("ADODB.Connection")
    > conn.ConnectionString="Provider=Microsoft.Jet.OLED B.4.0;" & _
    > "Data Source=" & Server.MapPath("Data\WEB_PROD.mdb")
    > conn.Open
    > set cmd = Server.CreateObject("ADODB.Command")
    > Set cmd.ActiveConnection = Conn
    > cmd.CommandText = sSQL
    > cmd.CommandType=adCmdText
    > set prm = cmd.CreateParameter("@prmClientNo", adVarChar, _
    > adParamInput, 13)
    > cmd.Parameters.Append prm
    > cmd.Parameters("@prmClientNo").Value = sClientNo
    >
    > Set R = cmd.Execute
    >
    > > ClientNoIsValid = not R.eof
    > > Response.Write R.eof & "<br>"
    > > 'Response.Write R.Fields("COMP_NAME").Value & " " &
    > > R.Fields("NAME").Value & "<BR>"
    > > Set conn = Nothing
    > > Set R = Nothing
    > >
    > > end function
    > >
    >
    > By not using a saved parameter query, you are making life difficult for
    > youself. Do yourself a favor and Google for posts by me containing the words
    > "saved parameter query".
    >
    > Bob Barrows
    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    Serge Myrand Guest

  4. #3

    Default Re: Convention for writing parameters in CommandText

    Everythings works find now.

    If I had many parameters would I "Append" them in the order the query will use
    them and still use the '?' as placeholder in the CommandText?

    thank's
    serge

    "Bob Barrows [MVP]" wrote:
    > Serge Myrand wrote:
    > > Hi,
    > >
    > > This function always return and empty RecordSet (R.eof=True) the
    > > problem
    > > is probably the way I pass the parameter in the CommandText. To avoid
    > > looking at all code first look at this I think my mistake is
    > > '[@prmClientNo]'
    >
    > True.
    > When using a text sql statement, you have to use the ODBC ? parameter
    > placeholder, like this:.
    >
    > sSQL = "SELECT ... WHERE c.CLIENT_NO = ? ..."
    >
    > > but it does not give any error message. Any help will
    > > be appreciate!
    > >
    > > // code from the function below
    > > Call R.Open("SELECT c.COMP_NAME, c.CLIENT_NO, cr.NAME FROM CLIENT AS
    > > c " & _
    > > "LEFT JOIN CLIENT_REF AS cr ON c.CLIENT_NO =
    > > cr.CLIENT_NO " & _
    > > "WHERE c.CLIENT_NO = '[@prmClientNo]' " & _
    > > "ORDER BY NAME ASC", conn, adOpenForwardOnly,
    > > adLockReadOnly, _
    > > adCmdText)
    > >
    > You just undid all of your hard work in building the Command object. See
    > below
    >
    > >
    > >
    > //************************************************** ************************
    > ********
    > >
    > > function ClientNoIsValid(sClientNo)
    > > dim conn
    > > dim R
    > > dim F
    > > dim prm
    > > dim cmd
    > > set conn = Server.CreateObject("ADODB.Connection")
    > > set R = server.CreateObject("ADODB.RecordSet")
    > > set cmd = Server.CreateObject("ADODB.Command")
    > >
    > > conn.ConnectionString="Provider=Microsoft.Jet.OLED B.4.0;Data
    > > Source=" & _
    > > Server.MapPath("Data\WEB_PROD.mdb") & ";Persist
    > > Security Info=True"
    > > conn.CursorLocation=adUseClient
    > > conn.Mode= adModeRead
    > > conn.Open
    > > Set cmd.ActiveConnection = Conn
    > > set prm = cmd.CreateParameter("@prmClientNo", adChar, adParamInput,
    > > 13)
    > >
    > > cmd.Parameters.Append prm
    > > cmd.Parameters("@prmClientNo").Value = sClientNo
    >
    > NO! Get rid of the following line:
    > ************************************
    > > cmd.Parameters.Refresh
    > **************************************
    > >
    > > R.CursorLocation=adUseClient
    >
    > I look at the following and all I can say is: "Incredible!" Why did you
    > build the Command object if you weren't going to use it??
    >
    > > Call R.Open("SELECT c.COMP_NAME, c.CLIENT_NO, cr.NAME FROM CLIENT AS
    > > c " & _
    > > "LEFT JOIN CLIENT_REF AS cr ON c.CLIENT_NO =
    > > cr.CLIENT_NO " & _
    > > "WHERE c.CLIENT_NO = '[@prmClientNo]' " & _
    > > "ORDER BY NAME ASC", conn, adOpenForwardOnly,
    > > adLockReadOnly, _
    > > adCmdText)
    >
    > 1) Since you are using a client-side cursor, the only cursor-type available
    > is a static cursor. If you really want a forward-only cursor (and it appears
    > that you do), you need to get rid of the lines where you set the
    > cursorlocation to adUseClient.
    >
    > Let's modify your code as follows:
    >
    > dim conn
    > dim R
    > dim F 'What is this?
    > dim sSQL
    > dim prm
    > dim cmd
    > sSQL = "SELECT ... WHERE c.CLIENT_NO = ? ..."
    > set conn = Server.CreateObject("ADODB.Connection")
    > conn.ConnectionString="Provider=Microsoft.Jet.OLED B.4.0;" & _
    > "Data Source=" & Server.MapPath("Data\WEB_PROD.mdb")
    > conn.Open
    > set cmd = Server.CreateObject("ADODB.Command")
    > Set cmd.ActiveConnection = Conn
    > cmd.CommandText = sSQL
    > cmd.CommandType=adCmdText
    > set prm = cmd.CreateParameter("@prmClientNo", adVarChar, _
    > adParamInput, 13)
    > cmd.Parameters.Append prm
    > cmd.Parameters("@prmClientNo").Value = sClientNo
    >
    > Set R = cmd.Execute
    >
    > > ClientNoIsValid = not R.eof
    > > Response.Write R.eof & "<br>"
    > > 'Response.Write R.Fields("COMP_NAME").Value & " " &
    > > R.Fields("NAME").Value & "<BR>"
    > > Set conn = Nothing
    > > Set R = Nothing
    > >
    > > end function
    > >
    >
    > By not using a saved parameter query, you are making life difficult for
    > youself. Do yourself a favor and Google for posts by me containing the words
    > "saved parameter query".
    >
    > Bob Barrows
    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    Serge Myrand Guest

  5. #4

    Default Re: Convention for writing parameters in CommandText

    Serge Myrand wrote:
    > Hi,
    >
    > This function always return and empty RecordSet (R.eof=True) the
    > problem
    > is probably the way I pass the parameter in the CommandText. To avoid
    > looking at all code first look at this I think my mistake is
    > '[@prmClientNo]'
    True.
    When using a text sql statement, you have to use the ODBC ? parameter
    placeholder, like this:.

    sSQL = "SELECT ... WHERE c.CLIENT_NO = ? ..."

    > but it does not give any error message. Any help will
    > be appreciate!
    >
    > // code from the function below
    > Call R.Open("SELECT c.COMP_NAME, c.CLIENT_NO, cr.NAME FROM CLIENT AS
    > c " & _
    > "LEFT JOIN CLIENT_REF AS cr ON c.CLIENT_NO =
    > cr.CLIENT_NO " & _
    > "WHERE c.CLIENT_NO = '[@prmClientNo]' " & _
    > "ORDER BY NAME ASC", conn, adOpenForwardOnly,
    > adLockReadOnly, _
    > adCmdText)
    >
    You just undid all of your hard work in building the Command object. See
    below

    >
    >
    //************************************************** ************************
    ********
    >
    > function ClientNoIsValid(sClientNo)
    > dim conn
    > dim R
    > dim F
    > dim prm
    > dim cmd
    > set conn = Server.CreateObject("ADODB.Connection")
    > set R = server.CreateObject("ADODB.RecordSet")
    > set cmd = Server.CreateObject("ADODB.Command")
    >
    > conn.ConnectionString="Provider=Microsoft.Jet.OLED B.4.0;Data
    > Source=" & _
    > Server.MapPath("Data\WEB_PROD.mdb") & ";Persist
    > Security Info=True"
    > conn.CursorLocation=adUseClient
    > conn.Mode= adModeRead
    > conn.Open
    > Set cmd.ActiveConnection = Conn
    > set prm = cmd.CreateParameter("@prmClientNo", adChar, adParamInput,
    > 13)
    >
    > cmd.Parameters.Append prm
    > cmd.Parameters("@prmClientNo").Value = sClientNo

    NO! Get rid of the following line:
    ************************************
    > cmd.Parameters.Refresh
    **************************************
    >
    > R.CursorLocation=adUseClient
    I look at the following and all I can say is: "Incredible!" Why did you
    build the Command object if you weren't going to use it??
    > Call R.Open("SELECT c.COMP_NAME, c.CLIENT_NO, cr.NAME FROM CLIENT AS
    > c " & _
    > "LEFT JOIN CLIENT_REF AS cr ON c.CLIENT_NO =
    > cr.CLIENT_NO " & _
    > "WHERE c.CLIENT_NO = '[@prmClientNo]' " & _
    > "ORDER BY NAME ASC", conn, adOpenForwardOnly,
    > adLockReadOnly, _
    > adCmdText)
    1) Since you are using a client-side cursor, the only cursor-type available
    is a static cursor. If you really want a forward-only cursor (and it appears
    that you do), you need to get rid of the lines where you set the
    cursorlocation to adUseClient.

    Let's modify your code as follows:


    dim conn
    dim R
    dim F 'What is this?
    dim sSQL
    dim prm
    dim cmd
    sSQL = "SELECT ... WHERE c.CLIENT_NO = ? ..."
    set conn = Server.CreateObject("ADODB.Connection")
    conn.ConnectionString="Provider=Microsoft.Jet.OLED B.4.0;" & _
    "Data Source=" & Server.MapPath("Data\WEB_PROD.mdb")
    conn.Open
    set cmd = Server.CreateObject("ADODB.Command")
    Set cmd.ActiveConnection = Conn
    cmd.CommandText = sSQL
    cmd.CommandType=adCmdText
    set prm = cmd.CreateParameter("@prmClientNo", adVarChar, _
    adParamInput, 13)
    cmd.Parameters.Append prm
    cmd.Parameters("@prmClientNo").Value = sClientNo

    Set R = cmd.Execute
    > ClientNoIsValid = not R.eof
    > Response.Write R.eof & "<br>"
    > 'Response.Write R.Fields("COMP_NAME").Value & " " &
    > R.Fields("NAME").Value & "<BR>"
    > Set conn = Nothing
    > Set R = Nothing
    >
    > end function
    >
    By not using a saved parameter query, you are making life difficult for
    youself. Do yourself a favor and Google for posts by me containing the words
    "saved parameter query".

    Bob Barrows


    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  6. #5

    Default Re: Convention for writing parameters in CommandText

    > If I had many parameters would I "Append" them in the order the query
    will use
    > them and still use the '?' as placeholder in the CommandText?
    Wouldn't it be a bit more sane and manageable to use named parameters?
    Doesn't Bob have a script that will automate the creation of this?

    --
    [url]http://www.aspfaq.com/[/url]
    (Reverse address to reply.)


    Aaron [SQL Server MVP] Guest

  7. #6

    Default Re: Convention for writing parameters in CommandText

    Serge Myrand wrote:
    > Everythings works find now.
    >
    > If I had many parameters would I "Append" them in the order the
    > query will use them and still use the '?' as placeholder in the
    > CommandText?
    >
    Yes. But it would be MUCH simpler to use saved parameter queries.

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  8. #7

    Default Re: Convention for writing parameters in CommandText

    Aaron [SQL Server MVP] wrote:
    >> If I had many parameters would I "Append" them in the order the
    >> query will use them and still use the '?' as placeholder in the
    >> CommandText?
    >
    > Wouldn't it be a bit more sane and manageable to use named parameters?
    > Doesn't Bob have a script that will automate the creation of this?
    Unfortunately, he's using Access, so my code generator won't work for that.

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  9. #8

    Default Re: Convention for writing parameters in CommandText

    Ok, Thank you for your time.
    serge

    "Bob Barrows [MVP]" wrote:
    > Serge Myrand wrote:
    > > Everythings works find now.
    > >
    > > If I had many parameters would I "Append" them in the order the
    > > query will use them and still use the '?' as placeholder in the
    > > CommandText?
    > >
    >
    > Yes. But it would be MUCH simpler to use saved parameter queries.
    >
    > Bob Barrows
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    Serge Myrand Guest

Posting Permissions

  • You may not post new threads
  • You may 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