Skip to main content
Participant
August 10, 2011
Answered

strange behavior of cfset

  • August 10, 2011
  • 3 replies
  • 457 views

This problem has befuddled me for a couple of days now and I cannot figure it out. First I'll say that my set up is ColdFusion 9, SQL Server 2008, and IIS 7.0. Let me also say that I have no control over my development environment which is a major hindrance but that is the case. Whenever I get an error with my ColdFusion code I simply get a generic IIS "500 Internal Server Error". The admin probably has suppressed more meaningful error messages that ColdFusion would give me.  So for the time being this is the environment I have to work with and can't do anything about it.

So with this in mind here is the issue. Look at the pseudo code below which resembles the issue I am having:

<cfset x = query1.field1>

<cfset y = query2.field1>

<cfset z = x - y>

<td><CFOUTPUT>#DollarFormat(x)#</CFOUTPUT></td>

<td><CFOUTPUT>#DollarFormat(y)#</CFOUTPUT></td>

This is pretty straight-forward code that should easily work. Take into account that the two queries being referred to are both defined further up in the code (which I haven't included). However for some unknown reason the third line (setting variable "z") breaks the code. If I comment this line out the page will at least run without an error. The values of variable "x" and "y" display without a problem.Also take into account I have NOT tried to DISPLAY variable "z" yet, I am simply trying to set the value for it which ColdFusion seems to object to for some reason. Also variables "x" and "y" are integers and are not null values. Also this entire statement is within a "cfloop" so it should run these lines 3 times (which it does and it outputs the correct values).

Does anyone have any idea what might cause the 3rd line to give an error? I'm somewhat new to ColdFusion so maybe its something obvious (though I've used similiar code before without a problem). If you need me to elaborate more I can though I'm not sure how helpful that would be. Any help is much appreciated

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    First, get in touch with your line manager and tell them you cannot be expected to work like this.  Get them to talk to the network admin manager and get robust exception handling switched on on the CF server.  There is absolutely no valid reason for them to not have that switched on in a dev environment.

    I realise you said it can't be changed, but it's amazing how much of the unchangeable can be changed if one is direct and firm about things.

    But anyway...

    You can put a try/catch around the erroring line of code and in the catch block dump out the cfcatch struct, which'll give you the actual error.

    It's impossible to say much beyond that, because we've got no idea what data is in those queries.  It'll be straight fwd to solve once we have the error message.

    I am guessing there is a chance that the values in those fields are not numeric (like nulls or something).

    As a general rule when encountering "mysterious" errors, dump out the data contributing to the code that is erroring and make sure it's what you think it is.

    --

    Adam

    3 replies

    JeffBevAuthor
    Participant
    August 11, 2011

    I appreciate the help guys. I figured it out. I used <cftry> and <cfcatch) and the error returned was:

         The value '' cannot be converted to a number.

         Caught an exception, type = Expression

    I had previously tested for "null" values and this hadn't worked. This made me realize that this was actually a zero length string (not a null). I had also tried testing for a zero length strength, however I was using "=" instead of "EQ" to do it. Once I did this I was able to convert the value to 0 if it returned a zero length string.

    I think what was throwing me the most was the fact that it was able to successfuly output the statements:

    <cfoutput>DollarFormat(x)</cfoutput>

    <cfoutput>DollarFormat(y)</cfoutput>

    And none of the ouput would come out as a zero length string, it would come out as "0". Were this not the case I probably would have tested for a zero length string sooner. Anyway, my guess is that the "DollarFormat" function is converting the empty string to a "0" and that's why it is displaying as such.

    Thanks for the advice on using the ColdFusion error trapping functions. That put me on to the right track. And I will try to plead my case to the web/db administrator. I had avoided doing this in the past because I only have to suport a single ColdFusion app that runs in this environment. However I may end up having to support others in the future and this is definitely a major hindrance!

    Inspiring
    August 11, 2011

    20/20 hindsight shows your problem to be preventable.  Given that:

    1.  ColdFusion does not have null values and converts database nulls to empty strings, and

    2.  You had an empty string when you were expecting an integer, and

    3.  You were in a loop,

    I will boldly assume that the numbers came from a database.  Depending on your business requirements, you can:

    1.  Not allow null values and use default values instead, or

    2.  Use a database function in your select query to convert null values to some default, or

    3.  Exclude records with null values from your query results

    Inspiring
    August 10, 2011

    I simply get a generic IIS

    "500 Internal Server Error".

    Also make sure the 500 error really is on the server side. Internet Explorer has a setting that can hide error messages too. ie friendly http error messages

    Adam Cameron.Correct answer
    Inspiring
    August 10, 2011

    First, get in touch with your line manager and tell them you cannot be expected to work like this.  Get them to talk to the network admin manager and get robust exception handling switched on on the CF server.  There is absolutely no valid reason for them to not have that switched on in a dev environment.

    I realise you said it can't be changed, but it's amazing how much of the unchangeable can be changed if one is direct and firm about things.

    But anyway...

    You can put a try/catch around the erroring line of code and in the catch block dump out the cfcatch struct, which'll give you the actual error.

    It's impossible to say much beyond that, because we've got no idea what data is in those queries.  It'll be straight fwd to solve once we have the error message.

    I am guessing there is a chance that the values in those fields are not numeric (like nulls or something).

    As a general rule when encountering "mysterious" errors, dump out the data contributing to the code that is erroring and make sure it's what you think it is.

    --

    Adam