Showing posts with label cfscript. Show all posts
Showing posts with label cfscript. Show all posts

Wednesday, November 28, 2012

Setting Cookie by cfscript in ColdFusion 10

Now a days in ColdFusion coding most of us generally using script version of CF more than tag version. Sometimes we feel upset when we don't get the script version for a particular tag. In  such cases we write our own function to use that in script code. CFCOOKIE is one tag which used to set cookie and which doesn't have any script version before ColdFusion 10.

   In ColdFusion 10, script version for CFCOOKIE  introduced. We can set cookie by cfscript in ColdFusion 10 as follows.


In the above code two ways are quite similar, here we are creating a structure where the key of the structure is exactly same as the attributes of the cfcookie tag. If we are using cfscript  to set cookie then we don't need to set name of the cookie in the structure as we usually do for cfcookie.

Also in ColdFusion 10, two new attributes added for setting cookie. These are follows:

preserveCase : Specify if you want to make cookie name case sensitive. This is an optional parameter and default value is "false."

encodeValue : Specify if cookie value should be encoded. This is an optional parameter and default value is "false".

Hope it will help you.

Thursday, October 06, 2011

Bug in script version of cfquery

We generally uses ColdFusion tags in our coding. But sometimes we prefer to use script version of that instead the tag. Also in ColdFusion 9.0.1 have a script version code for almost every tags.

In ColdFusion 9.0.1 the script version of cfquery has a bug as follows.

Let our table name is "USERS(USER_ID, FIRST_NAME, LAST_NAME, isDELETED)", which contains the users information.

If I want to get the data of a single user then I will write.

<cfscript>

variables.q = new query();
variables.q.setdatasource("TrackBug_New");
variables.q.setSql('SELECT
USERID,
FIRST_NAME,
LAST_NAME
FROM
USERS
WHERE
USER_ID = :userID
AND
isDELETED = 0');

//Set value in addParam equivalent to cfqueryparam
variables.q.addParam(name="userID", value="1", CFSQLTYPE="CF_SQL_INT");

//Executes the query and Extracts the result from the query object
variables.qry_getUser = variables.q.execute().getresult();

//To show the result
WriteDump(variables.qry_getUser);

</cfscript>

But unfortunately we will get the error message :

=============================================================
Error Executing Database Query
Parameter 'userID AND' not found in the list of parameters specified
=============================================================


Cause Of Problem:
While setting the variable ":userID" to the query string I have placed a line break after that variable and which is the culprit. If we will place a space character after the variable then it will not create any problem. Again for any other character other than space character will cause the problem, like if we replace tab instead of space then we will also face the same problem.

Moral:
When you are using any variable in the query string then always give a space character after the variable.


For this problem I have just wasted 2-3 hr. So I thought it will help you :).

Followers