try catch in a component

Experts-

I am writing a function to insert info into a table.  I am using a <cftry> block for the insert and <cfcatch type ="database"> If it does catch I have the simple message "That Attribute already exists in the database."

On my processing  page, I want to write a cfif statement that says,
<cfif>The cftry was successful
<cflocation url="mynewpage.cfm">
<cfelse>
Diplay the cacth message.
</cfif>

Can someone explain how I would do this with the component and the processing page?

Thanks
nick

<cffunction name="addAttributes" returntype="boolean" hint="Add Attributes for tables">
		<cfargument name="attribute"
					type="string"
					required="yes"
					hint="Table Attribute">
		<!--- Now that we set the field names as arguments, lets
				write the insert statement --->
		<cftry>
		
			<cfquery datasource="dbm">
				INSERT INTO htmlTableAttributes(
							attributeName)
				VALUES('#TRIM(ARGUMENTS.attribute)#')
			</cfquery>
		<cfcatch type="database">
		That Attribute already exists in the database.
		</cfcatch>
		</cftry>
		<cfreturn true>
	</cffunction>

Open in new window

LVL 2
nmaranoAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
_agx_Connect With a Mentor Commented:
I prefer using createObject() myself ;-) But if you're using cfinvoke then you'd use the "returnVariable" attribute to capture the response (true/false).  

<cfinvoke component="dbm_tutorials.components.snippets"
                  returnValue="successful"
                  method="addAttributes">
      <cfinvokeargument name="attribute" value="#TRIM(FORM.attribute)#">
</cfinvoke>

<!--- check to see if we made an insert --->
<cfif successful>
      <cflocation url="attributes.cfm">
<cfelse>
      <p class ="instructions">My error message goes here</p>
</cfif>
....
0
 
_agx_Commented:
The function should just return true or false

<cffunction name="addAttributes" returntype="boolean" hint="Add Attributes for tables">
            <cftry> ....
            <cfcatch type="database">
                <cfreturn false>
            </cfcatch>
            </cftry>
           <cfreturn true>
</cffunction>

The displaying page calls the function. Then uses the result to relocate or display an error


<cfset successful = yourComponent.addAttributes("some value")>
<cfif successful>
       <cflocation url="mynewpage.cfm">
<cfelse>
      That Attribute already exists in the database.
</cfif>
0
 
nmaranoAuthor Commented:
agx-

Thanks!  Let me try it out
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
nmaranoAuthor Commented:
agx-

So would the "some value" you described above be true?

AND if so, would my page look something like this with what you described?
<cfinclude template="header.cfm">
<cfinvoke component="dbm_tutorials.components.snippets"
                  method="addAttributes">
      <cfinvokeargument name="attribute"
                               value="#TRIM(FORM.attribute)#">
</cfinvoke>
<!--- check to see if we made an insert --->
<cfset successful = dbm_tutorials.components.snippets("true")>
<cfif successful>
      <cflocation url="attributes.cfm">
<cfelse>
      <p class ="instructions">My error message goes here</p>
</cfif>
<cfinclude template="footer.cfm">
0
 
nmaranoAuthor Commented:
Thanks I'll try it.  And, you should have known I was going to ask you why you prefer createObjects()  I'm using more and more components and always appreciate different perspectives.
0
 
_agx_Commented:
Well personally I find the createObject syntax more compact :) Plus, I like using cfscript a lot, where you can't use tags anyway.  But I know some people used to use cfinvoke because their shared host that doesn't allow createObject().



0
 
gdemariaCommented:
(no points)


Another advantage to createObject is the ability to invoke it once and then just use it thoughout your application.   Just assign it into an application scoped variable...

<cfif NOT isDefined("application.myCFC")>
   <cfset application.myCFC = createObject(...)>
</cfif>

Then use it...

<cfset result = application.myCFC.snippets('variable')>



... of course you don't want to do this with cfc's that may retain session specific values
0
 
nmaranoAuthor Commented:
agx-

I keep getting the error message that "Variable SUCCESSFUL is undefined", but isn't it defined in the retuenValue of the <cfinvoke>?

<cfinvoke component="dbm_tutorials.components.snippets"
                  returnValue="successful"      
                  method="addAttributes">
      <cfinvokeargument name="attribute"
                               value="#TRIM(FORM.attribute)#">
</cfinvoke>
<!--- check to see if we made an insert --->

<cfif successful>
      <cflocation url="attributes.cfm">
<cfelse>
      <p class ="instructions">My error message goes here</p>
</cfif>
0
 
_agx_Connect With a Mentor Commented:
Gah ... that was a typo. My bad. It's returnVariable (did I mention I never use cfinvoke ;-)

<cfinvoke component="test.MyComponent"
                  returnVariable ="successful"      
                  method="addAttributes">
0
 
nmaranoAuthor Commented:
agx-

Thanks for the help!
0
All Courses

From novice to tech pro — start learning today.