Skip to main content
Known Participant
June 27, 2011
Answered

<CFQUERYPARAM Null attribute did not work!

  • June 27, 2011
  • 4 replies
  • 2678 views

Hi All!

From what I learned, cfqueryparam's null attribute should be very useful for this scenario:

<CFIF IsDefined("Form.Name") AND Form.Name NEQ "">

Do this

<CFELSE>

Do That

</CFIF>

With the null attribute from cfqueryparam, we can write a clean and more elegant code like this:

<CFIF IsDefined("Form.Name") AND Form.Name NEQ "">

   <CFQUERYPARAM cfsqltype="cf_sql_varchar" value="#Trim(Form.Name)#" null="#NOT Len(Trim(Form.Name))#">

</CFIF>

But when I tried it on my INSERT statement, I got error message saying that the amount of columns don't match it's values. I counted more than once

they exactly matched up.

I modified the same insert statement with values changed to use cfiif cfelse method, it worked. When I changed it back to use cfqueryparam and its null attribute, it did not work again because when form.name is blank/null, cfqueryparam did not insert the NULL value the way cfif Isdefined expression does

I also tried using yesNoFormat and still did not work.

Is this a bug?

This topic has been closed for replies.
Correct answer -__cfSearching__-

Is this a bug?

No. Your two statements are not equivalent. Unlike the first statement, the second one does nothing if the field does not exist or is empty:

<CFIF IsDefined("Form.Name") AND Form.Name NEQ "">

      create cfqueryparam

<CFELSE>

       do nothing  ....

</CFIF>

If you used <cfparam> to ensure FORM.Name always exists, then you could simply write:

       <CFQUERYPARAM cfsqltype="cf_sql_varchar" value="#Trim(Form.Name)#" null="#NOT Len(Trim(Form.Name))#">

4 replies

Inspiring
June 27, 2011

Is this a bug?

Yep.  But it'll be a bug in your code, not CF's.

However to say much beyond that is impossible without actually seeing your code...

--

Adam

Inspiring
June 27, 2011

However to say much beyond that is impossible without

actually seeing your code...

Two words for you "Jive software" ..

Inspiring
June 27, 2011

However to say much beyond that is impossible without

actually seeing your code...

Two words for you "Jive software" ..

<cfset someCodeHere = "no problem">

--

Adam

-__cfSearching__-Correct answer
Inspiring
June 27, 2011

Is this a bug?

No. Your two statements are not equivalent. Unlike the first statement, the second one does nothing if the field does not exist or is empty:

<CFIF IsDefined("Form.Name") AND Form.Name NEQ "">

      create cfqueryparam

<CFELSE>

       do nothing  ....

</CFIF>

If you used <cfparam> to ensure FORM.Name always exists, then you could simply write:

       <CFQUERYPARAM cfsqltype="cf_sql_varchar" value="#Trim(Form.Name)#" null="#NOT Len(Trim(Form.Name))#">
Inspiring
June 27, 2011

You still need the cfelse for when the cfif contition is not true

or you will not have a cfqueryparam to match the insert column.

Known Participant
June 27, 2011

Greetings,

I will be out of the office on Monday and Tuesday, June 27 and 28. I will respond to all emails when I return.

Thanks,

Ron Barth

Alternative Systems

www.altsystem.com

603.537.9473