Jan 22, 2012

Outputting Coldfusion Parameters in cfstoredproc stored procedures

Last week I was running into an issue with a stored procedure. Passing parameters to the procedure using cfstoredproc and cfprocparam.

Here is how I solved it to trap the problem which was a HY000 error returned by Oracle.

using a combination of cfoutput and pre and then clipping the < part of the container tags and then closing the cfoutput and pre tags I was able to determine a few things.


< cfoutput >
< pre >
CFstoredproc procedure="schema.package.procedure" datasource="dsn"> cfprocparam type="inout" cfsqltype="CF_SQL_Number" dbvarname="mID" value="#trim(variables.mID)#" null="#evaluate("variables.mID is """"")#"> cfprocparam type="in" cfsqltype="CF_SQL_Varchar" dbvarname="pName" value="#form.pname#"> cfprocparam type="in" cfsqltype="CF_SQL_Number" dbvarname="pMedium" value="#trim(form.pMedium)#"> CFStoredproc>
<  /pre >
< /cfoutput >
< cfabort >

(where you see the actual container tags spaced out is because of blogger doing some clean-up...I forgot how frustrating this can be :) )

I re-saved the code snippet and visited the page in my browser.  I was able to determine that I actually had more than one problem.  I was passing part of a form tag to my procedure parameter which was a coding error on my part, and also one of my variable data types were mismatching.  For example a string where a number was expected by the procedure.  I got my data types fixed removed the pre tag and the cfoutput surrounding it and it worked like a charm.  i hope this helps others who have got stuck on something like this.

Crafty website and eBook marketing tips on content and keywords

This book is very meat and potatoes on content generation and keyword relevancy.  It is not what you say it is how you say it and on websites and eBooks that goes double.  There is more than meets the eye when it comes to search and we will get to the bottom of the problems developers and designers face in this installment.

Visit:

http://www.amazon.com/Content-Keywords-Crafty-Tips-ebook/dp/B0070GC12C/




You can locate my other blog about books I write and my author page here:

http://www.amazon.com/Frank-Tudor/e/B005VR3DMY/

http://franktudor.wordpress.com/

Back link strategies for your website or eBook

Solving the problems of web is never easy, you have to think like the bots, spiders and crawlers.  You have to at the same time maintain useful purposeful meaningful information. Each sentence you right is being scrutinized in the meat grinder of search engines and how you get to the top spot is the golden ring of web developers and designer teams throughout the world.  They are better staffed then you, they have more experience creating compelling websites.  So I have created this book to give your the step by step content requirements and where to find trusted technology and resources to get you the rest of the way.

http://www.amazon.com/Strategies-Website-eBook-Crafty-ebook/dp/B0070DA00U/

You can locate my other blog about books I write and my author page here:

http://www.amazon.com/Frank-Tudor/e/B005VR3DMY/

http://franktudor.wordpress.com/

Crafty eBook and Web Marketing Tips Series

I have created a series of SEO related books for helping solve some problems in eBook and web marketing.  The books are parsed into logical segments.  The material will focus on numerous ways to improve your position in search results.

You can locate my two latest installments on Amazon:

http://www.amazon.com/Content-Keywords-Crafty-Tips-ebook/dp/B0070GC12C/

http://www.amazon.com/Strategies-Website-eBook-Crafty-ebook/dp/B0070DA00U/

As a SEO grunt on the ground and helping companies in the game of inches against very creative competition, it never hurts to bolster your metal arsenal with tools and tricks to help you get the PR, relevancy and click through rate your deserve.

Mar 25, 2008

Coldfusion Powerball lottery pick tool

Ever wanted to pick lottery numbers with your own tool? This tool is setup for the powerball. and has set by step explanations.

<!---in coldfusion pick random values is a pain, because if you need five numbers changes are you will get duplicates.--->

<!---We have added some code to deal with that.--->

<!---First we are setting a parameter to create an array called randArray.--->

<cfparam

name="randArray"

type="any"

default="#ArrayNew(1)#" />

<!---I am setting a loop incremental to zero because I need to work with it in my loop. I need five numbers, but first I set it to zero...stay with me.--->

<cfset loopIncremental = 0>

<!---Now we are creating a conditional loop to check to see if the loop is less than five. It is so we are are going to loop and add 1 to the loop incremental until it hits five then stop (see we need five white balls). --->

<cfloop condition="loopIncremental lt 5">

<!---The arrRandRange is the range of values to pick numbers from. In powerball we need 1 through 54.--->

<cfset arrRandRange = randrange(1, 54)>

<!---We are now taking our array we set above int he parameter and change it to a list--->

<cfset randArrayToList = arraytolist(randArray)>

<!---Now we are checking to see if the list has a duplicate value. and setting that to an if statement which goes like this. If the array/list has the number we just randomly picked, we do nothing. If we have a fresh number we append the array and add that value to it.--->

<cfif listfind(randArrayToList, #arrRandRange#)>

<cfelse>

<!---here is where we add the value if the 'condition' is non-duplicate.--->

<cfset ArrayAppend(randArray,"#arrRandRange#")>

<!---we then increment the loop incremental by 1 (see if if it doesn't get added to the array we are still one number short and so we do not set the incriment. Clear as mud?--->

<cfset loopIncremental = #loopIncremental# + 1>

<!---then we exit the if statement--->

</cfif>

<!---next we exit the loop--->

</cfloop>

<cfoutput>

<!---I'm probably doing a usless step here but my basic effort is to sort the array's numbers from lowest to highest and then output the numbers to the screen.--->

<cfset xArrayToList = ArrayToList(randArray)>

<cfset yListSort = listsort(xArrayToList, "numeric")>

#yListSort#

</cfoutput>

<!---And for my last trick I am grabbing one red ball from a range 1 through 50--->

<cfset arrRandRange2 = randrange(1, 50)>

<!---then outputting that value--->

<cfoutput>

#arrRandRange2#

</cfoutput>

and abracadabra I have my lottery number picks...I hope they are winners.

Mar 24, 2008

Looping backwards over a list or array

Many months back I was on a job interview and someone asked me to loop backwards over a name, essentially spelling it backwards. What an odd request. I fumbled around and was totally stumped. I didn't get the job, but you can bet your ass I figured out how to loop backwards over a list/array or whatever. Below is the proof of concept example code.

Enjoy.

<!---Loop backwards example.--->

<!---First we are going to create a list.--->

<cfset myList = "F,r,a,n,k,T,u,d,o,r">

<!---Next we are going to create an array out of it.--->

<cfset myArray = listToArray(myList)>

<!---Now weare going to set up our loop with parameters.--->

<br>

<!---Note that I am setting the from and to max to min and

setting step to -1 so it can step backwards throught he loop.

--->

<cfoutput>

<cfloop from="#arrayLen(myArray)#" to="1" step="-1" index="i" >

#myArray[i]#<br>

</cfloop>

</cfoutput>

Coldfusion try/catch example.

Like other languages, Coldfusion has the ability to attempt code and then provide a custom error if it fails. This is good if you cannot test your code and example would be if you are waiting for other groups to get database development done, or server environment constraints.

So here is a simple example.

Say I have a query that adds favorites to my table first I am going to set some simple variables

<cfset me = "Frank">

<cfset myFav = "Franklin">

Next, I am going to set up my try/catch code Note I have a primary key made up of fvrt_src, fvrt_sel, meaning I cannot add it twice and if I refresh the screen the error will be produced.

<cftry>

<cfquery

datasource="#request.dsn#"

name="addingToFav">

insert into favorites (fvrt_src, fvrt_sel)

values ('#me#', '#myfav#')

</cfquery>

If the error is produced the catch will sense the database error and produce a nice controlled session error instead of a screen with gibberish (client confusion screens).

<cfcatch type="database">

<cfset session.favAddError = "User has already been added to your favorties<br>

please check your favorites list above.">

</cfcatch>

</cftry>

Next if the session has been created I display the error message below.

<cfif isdefined(“session.favAddError”)>

<cfoutput>

#session.favAddError#

</cfoutput>

Mar 6, 2008

Easy CFC

This is a simple coldfusion pages and the accompanying cfc.  
There are two functions that do different things. It might be
clear as mud so don't hesitate to drop me a line if you get lost,
but here is the idea, I am craeting two functions and invoking
them in differnt ways one example is through createObject and
the other is the cfinvoke tag.

(first our index.cfm page)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>
Untitled Document
</title>
</head>
<body>

<!---
Using cfset to call an object and output a value
In a cfc I have a component that has a function called timer.
Below I an 'instanciating' the object and
assigning it to a variable named 'time'
instead of using cfinvoke I am using the function
createObject. createObject has several parameter settings.
For a CFC and if that CFC is in the same directory as this file.
Then I simply set the createObject to call a component named time.
--->

<cfset time = createObject('component', 'myCfc')>

<!---
Below I am outputting the variable I set above.
Notice I am using the dot notation.
time.timer()
Notice the paranthesis. If my object
took an argument I would place it there.
Like this time.timer(today,tommorow,yesterday,etc)
But I have nothing to pass since
this is only a simple example.
--->

<cfoutput>
#time.x("This is a argument/parameter being passed to my object")#
</cfoutput>
<br<br>

<!---Here is a quick example of a cfc that does some arithmetic.--->

<!--- We are using the invoke method (tag) to pull this object into play --->

<cfinvoke component="myCfc" method="z" returnvariable="goober">

<cfoutput>
#goober#
</cfoutput>

</body>
</html>

(my next file is called myCfc.cfc)

<cfcomponent hint="amigo formats the date...if argument gets passed then i append it to the amigo string">
<cffunction
name="x"
description="the function name is x">
<cfargument
name="today"
type="string"
required="no">

<cfset amigo = timeFormat(now(), "h:MM")>

<cfif isdefined("arguments.today")>
<cfset amigo = arguments.today & " " & amigo>
</cfif>
<cfreturn amigo>
</cffunction>

<cffunction name="z">
<cfset x = 100>
<cfset y = 5>
<cfreturn x/y>
</cffunction>
</cfcomponent>

Mar 3, 2008

CFC Simplicity

Ok so here is my quick and dirty primer on CFCs. CFCs are the defacto method for keeping your development nice and clean. For this example i am using a simple query and then invoking it in a web page. There are two files you need the CFC and the CFM calling the CFC.

So here are the files assuming you have a database registered in your Coldfusion administrator.

index.cfm
qryPerson.cfc

For the index page here are the contents with explanations and the cfc with explanations.

<!---First we are invoking the cfc component, it's method

which is the same name as the component, and giveing

the return variable the same name. You can get

creative but you will atleast have to have these

three items. --->

<cfinvoke component="qryPerson" method="qryPerson" returnvariable="qryPerson">

<!---Here ais the only argument I am passing the first name is frank.--->

<cfinvokeargument name="first" value="Frank">

<!---Closing the invoke.--->

</cfinvoke>

<!---Now I am dumping out the return query.--->

<cfdump var="#qryPerson#">

<!---If you want to actually present the return...do this...--->
<cfoutput query="qryPerson">

#first_name# #last_name# #userID#

</cfoutput>

Here is the qryPerson.cfc (component).

<!---This cfcomponent can be modified to alter combinations

of first and last or even first, last and userID.

But your if statement would be more complex.--->

<!---Keeping it very simple here first cones the component name.--->

<cfcomponent>

<!---The function comes next make sure it is sent to return type query--->

<cffunction name="qryPerson" access="public" returntype="query">

<!---Here is my arguement I am calling it is not requiried--->

<cfargument name="first" type="string" required="yno">

<!---Then my query it is very simple but notice the embedded

if statement. Notice that the variable says with

arguments.first ARGUMENTS is what you use inside the CFC unless

you set it to a different variable but for simplicity I pulled

it directly, which is perfectly acceptable to do.--->

<cfquery datasource="emp" name="qryPerson">

Select * from emp

<cfif isdefined("arguments.first")>

where first_name = '#arguments.first#'

</cfif>

</cfquery>

<!---Now we return the query to this cfreturn tag..

just use the name of the query.--->

<cfreturn qryPerson>

</cffunction>

</cfcomponent>

Any questions? drop me a line at frank_tudor@yahoo.com

Dec 21, 2007

Variable Primer

Vocab:

Variable: a container that holds something

Value: assigned to a variable

example = variable = value

In coldfusion the simple variable can be handled in a few ways.

For example say we have the word 'apple' and we want to take this word and pass it throughout our application.

In a form this is how you would do it:

<form action="variables2.cfm" method="post">
<input name="fruit" value="apple" type="text">
<input value="submit" name="submit" type="submit">
</form>

and on the variable2.cfm page you would need code that looked like this to display it.

<cfoutput>
</cfoutput><p>passed variables</p>
<cfif>
#form.name#
</cfif>

This is a URL what we can a query string variable

<a href="http://www.blogger.com/variables2.cfm?name=frank">URL variable example</a>

For a session you would set your variable like this: Session are great for storing variables that are needed throughout your website. so instead of move hidden input fields through forms, or long crazy querystring you simple set the session.whatever and assign the value.

<cfset firstname=" 'Doug'">
<cfoutput>
#session.firstname#
</cfoutput>

Here is another type of variable called a 'request' variable. This type of variable only lasts for the life of the page...Move to another page and it is gone. request.variables are useful for database references where you would assigned the data source to something like request.dsn (datasource name).

<cfset frank=" 35">
request.frank is <cfoutput>#request.frank#</cfoutput>

The last variable is just the variable's variable where you would set your variable in somethign like this &lt;cfset fruit = 'apple'&gt; it only lasts for the duration of the page, move away from the page and it is gone.

Frank