Stored Procedure as method of Connection and NULLs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Anderson

    Stored Procedure as method of Connection and NULLs

    [This question is directed at Bob Barrows, but perhaps someone else knows an
    answer. In any case it is good information for the group.]

    I really like Bob's "stored procedure as method of Connection Object"
    technique. It is convenient, compact, and concise, and simplifies protection
    from SQL injection.

    HOWEVER, I cannot figure out a way to pass a null value to an INT parameter
    when I use this. Null string parameters are fine. The error reads:

    Microsoft OLE DB Provider for SQL Server error '80040e07'
    Operand type clash: text is incompatible with int

    As you can imagine, "text is incompatible with int" gets my attention. I am
    explicitly passing a null (not the string "null"). I am using JScript on the
    web server and connecting to SQL Server 2000. An example:

    var CN = Server.CreateOb ject("ADODB.Con nection"),
    PCTagNumber = +Request.Form(" PCTagNumber").I tem || null,
    DeskLocation = Request.Form("D eskLocation").I tem || null

    CN.Open(CNStrin g)
    CN.Inventory_Up date(HR.UserID, PCTagNumber,Des kLocation)

    Assume the stored procedure looks something like:

    CREATE PROCEDURE dbo.Inventory_U pdate(
    @UserID VARCHAR(50),
    @TagNumber INT,
    @DeskLocation VARCHAR(255)
    ) AS ...

    In the above case, a null tag number produces the error, while a null desk
    location results in a SQL NULL passed to the parameter @DeskLocation.

    Is there a way for me to pass a null integer here, or is this a limitation
    of Bob's technique?



    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms. Please do not contact
    me directly or ask me to contact you directly for assistance. If your
    question is worth asking, it's worth posting.


  • Dave Anderson

    #2
    Re: Stored Procedure as method of Connection and NULLs

    I wrote:[color=blue]
    > I cannot figure out a way to pass a null value to an INT
    > parameter when I use this.[/color]

    Not looking good for me:

    "To execute a stored procedure, issue a statement where
    the stored procedure name is used as if it were a method
    on the Connection object, followed by any parameters.
    ADO will make a "best guess" of parameter types."

    Gain technical skills through documentation and training, earn certifications and connect with the community




    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms. Please do not contact
    me directly or ask me to contact you directly for assistance. If your
    question is worth asking, it's worth posting.


    Comment

    • Bob Barrows [MVP]

      #3
      Re: Stored Procedure as method of Connection and NULLs

      Dave Anderson wrote:[color=blue]
      > I wrote:[color=green]
      >> I cannot figure out a way to pass a null value to an INT
      >> parameter when I use this.[/color]
      >
      > Not looking good for me:
      >
      > "To execute a stored procedure, issue a statement where
      > the stored procedure name is used as if it were a method
      > on the Connection object, followed by any parameters.
      > ADO will make a "best guess" of parameter types."
      >
      > http://msdn.microsoft.com/library/en...connection.asp[/color]

      Yes, I am having the same issue. I can only come up with two workarounds:
      1.
      Declare the parameter with a default so it is optional, making it the last
      parameter to be declared, then
      if (PCTagNumber==n ull)
      CN.Inventory_Up date(HR.UserID, DeskLocation)
      else
      CN.Inventory_Up date(HR.UserID, DeskLocation,PC TagNumber)

      Drawbacks
      - I only succeeded in omitting the last argument
      - It doesn't help with more than one nullable parameter

      2.
      Use a "magic" number instead of null, testing for that value in the
      procedure
      Drawbacks
      - do I need to enumerate them?

      I even tried using the parameter marker technique, which suffered the same
      fate (I somewhat expected that, suspecting that the same stuff was happening
      behind the scenes to make both techniques work).

      Oh well, I guess it's back to the explicit Parameters collection for these
      situations (where int parameters are nullable)

      Bob
      --
      Microsoft MVP - ASP/ASP.NET
      Please reply to the newsgroup. This email account is my spam trap so I
      don't check it very often. If you must reply off-line, then remove the
      "NO SPAM"


      Comment

      Working...