views:

810

answers:

3

I'm trying to insert a value from a form field into a SQL Server MONEY field using ColdFusion. If the amount is just dollars, or if the cents are more than 12, the insert goes fine.

But if the cents amount is less than 12, according to the error that's thrown, the system appears to be converting the money amount into a date format before the insert, without my asking for that. And then, of course, it errors out, because I'm trying to insert a date into a MONEY field. (!)

I tried using CFQUERYPARAM with the type CF_SQL_DECIMAL, but that didn't work. Any thoughts would be greatly appreciated.

Here's the form code:

<CFFORM ACTION="_execute_project_payment.cfm?action=Add" METHOD="post" NAME="FormX">
<CFOUTPUT>
<INPUT TYPE="hidden" NAME="projectID" VALUE="#projectID#">
<TR>
<TD CLASS="label" ALIGN="right" VALIGN="top">Payments:</TD>

<TD>

$<CFINPUT NAME="payment" TYPE="TEXT" VALIDATE="FLOAT" SIZE="15" REQUIRED="YES" MESSAGE="You must enter a payment amount.">&nbsp;&nbsp;&nbsp;Date:


<CFINPUT TYPE="text" name="payment_date" id="payment_date" SIZE="12" VALIDATE="DATE" REQUIRED="YES" MESSAGE="You must enter a payment date.">


<img src="cal_icon.gif" id="trigger_payment_date"
style="cursor: pointer; border: 0px;"
title="Date selector"
onmouseover="this.style.background=’red’;"
onmouseout="this.style.background=’’" />
<script type="text/javascript">
Calendar.setup({
inputField : "payment_date",
ifFormat : "%m/%d/%Y",
button : "trigger_payment_date",
align : "Tl",
singleClick : false
});
</script>


<INPUT TYPE="Submit" VALUE="Add">

<BR> 

</TD>
</TR>

</CFOUTPUT>
</CFFORM>

And here's the insert:

<CFSET paydateODBC = CreateODBCDate(FORM.payment_date)>

<CFQUERY NAME="add_project_payment" DATASOURCE="#dsource#" USERNAME="#usern#" PASSWORD="#passw#">
INSERT INTO project_payments (
         projectID,
                        payment,
                        payment_date
        )
      VALUES (
         #projectID#,
                        #FORM.Payment#,
                        #paydateODBC#
        )
</CFQUERY>

And here's the error, trying to enter 666.10 as the payment:

[Macromedia][SQLServer JDBC Driver][SQLServer]Conversion failed when converting datetime from character string.

The error occurred in C:\Websites\qrpqiy\toubltracker\_execute_project_payment.cfm: line 21

19 :         #projectID#,
20 :                         #FORM.payment#,
21 :                         #paydateODBC#
22 :        )
23 : </CFQUERY>

SQLSTATE      22007
SQL        INSERT INTO project_payments ( projectID, payment, payment_date ) VALUES ( 433, {d '0666-10-01'}, {d '2009-05-15'} )
VENDORERRORCODE       241
+2  A: 

Personally, I never use the Money type in SQL. I find I have a better degree of control if I use the Decimal field type.

Steve -Cutter- Blades
I tried doing the same insert into a DECIMAL field, and the same thing happened. I think CF is doing this date format conversion, and I don't know how to stop it.
Adam J. Blust
+2  A: 
<cfqueryparam value="#theamount#" cfsqltype="cf_sql_money">

that's what I use and I've never had a problem. if you can post your query and some stub data, it would help to determine the cause better.

rip747
I tried that, and it actually made things worse. When I tried to insert a value that would error out - like 666.10 - the CFQUERYPARAM turned it into a massive negative number. It seems that CF is doing this conversion of the decimal to a date format before the insert is even attempted.
Adam J. Blust
For what it's worth, now that you've resolved your field-name issue, this is the best possible way to write your code, so I'm voting it up.
Adam Tuttle
+2  A: 

Well, according to Adobe, the problem was my field names. Apparently in CF there is an obscure old function that you can get a field like "payment" to be evaluated as a date when you also include a "payment_date" field. Argh. I changed the field names, and it works now.

Adobe's docs on this: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=validateData_10.html#1154690

Adam J. Blust
That is perverse. -1 to CF
DanSingerman
That is a strange one. Make sure to blog about that.
Steve -Cutter- Blades
you say according to adobe, can you link to the information? Is it a problem on the CF side or on the sql server side?
Ryan Guill
Here's the Adobe info on this "function." The idea, in old versions of CF, was to do server-side validation.http://livedocs.adobe.com/coldfusion/8/htmldocs/validateData_10.html#1154690
Adam J. Blust
@HermanD: The reason this feature still exists today is for backwards compatibility. Allaire, Macromedia, and now Adobe have all gone to great lengths to preserve backward compatibility. For the most part, you could take code written against ColdFusion 1.0 and it would run today against 8.0 (or in the 9.0 beta going on now). I don't think anyone here can claim they've never made a bad design decision early in a product's life and then had to live with it. Let him without sin cast the first stone.
Adam Tuttle
@Adam J. Blust: You should edit the link from your comment into your answer, and be sure to accept it as the correct answer.
Adam Tuttle