• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

cftry / cfcatch not displaying error message

Community Beginner ,
Jul 30, 2007 Jul 30, 2007

Copy link to clipboard

Copied

I am trying to display an error message when a user inaccurately selects two items. But the message is not displaying. Please let me know what I'm doing wrong. On the form page the user selects their couse and types in their ID. If the ID and course are not already in the same table in the database, then they should be prompted to try again. Here's my code

<cftry>
<cfquery name="all" datasource="hr1">
Select EMPLID, COURSE_TITLE
From TRAINING
WHERE EMPLID='#FORM.EMPLOYID#'
And COURSE_TITLE like '#FORM.CLASS#%'
</cfquery>

<cfcatch type = "any">
<!--- the message to display --->
<cfoutput>We do not show you registered for this course, please select another</cfoutput>

</cfcatch>
</cftry>

Views

948

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Beginner , Jul 30, 2007 Jul 30, 2007
Thanks for all the help. I found another solution in another forum. Since the query fails it's not populating a record count. I had to go back to the CFTry and relable the query. THis code works -

<cftry>
<cfquery name="all" datasource="hr1">
Select EMPLID, COURSE_TITLE
From TRAINING
WHERE EMPLID='#FORM.EMPLOYID#'
And COURSE_TITLE like '#FORM.CLASS#%'
</cfquery>
<cfcatch type="database">
<cfset all = QueryNew(EMPLID)/>
</cfcatch>
</cftry>

Then using the recordcount feature it outputs the c...

Votes

Translate

Translate
LEGEND ,
Jul 30, 2007 Jul 30, 2007

Copy link to clipboard

Copied

get rid of cftry/cfcatch. Instead, use if/else on the recordcount for your query.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 30, 2007 Jul 30, 2007

Copy link to clipboard

Copied

I tried that earlier but I still must be doing something wrong. When I use CFIF I still get no error message for a NULL record count. I've tried changing the
tag cfif #all.Recordcount# to LT 1, and to = "" and to = '' but nothing seems to output the error message when the recordcount = 0 . Here's my code

<cfoutput query="all">
<cfif #all.Recordcount# NEQ 1>
#all.recordcount# We do not have a course like this
<cfelse>
<hr>
#all.recordcount# you are OK an email has been sent to your supervisor
<!--- send email here --->
<hr>
</cfif>
</cfoutput>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 30, 2007 Jul 30, 2007

Copy link to clipboard

Copied

This post was entered just as your second one was. Sorry.

Dan is right. You need to check (via cfif/cfelse) if there are any records for the query.

There actually is no error in your statement above, so the cfcatch never fires. The key thing to keep in mind is that when a query returns no records, it is not an error. It's just empty.

So, do your query and then:
<cfif all.RecordCount>
<!--- qry is good... --->
<cfelse>
<!--- qry is empty (no results returned) --->
We do not show you registered for this course...
</cfif>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 30, 2007 Jul 30, 2007

Copy link to clipboard

Copied

> <cfoutput query="all">

This tells CF to loop over the query (it's a poorly named / implemented
tag). If there are no records: there's nothing to loop over, so anything
inside the loop is "ignored".

> <cfif #all.Recordcount# NEQ 1>

You need to check your record count BEFORE you loop over your query.

And lose the # characters: you don't need them within CFML statements
unless the variable is part of a quoted string.

It might be worth reading this:
http://www.adobe.com/support/coldfusion/getting_started/using_poundsigns_quotat/

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 30, 2007 Jul 30, 2007

Copy link to clipboard

Copied

LATEST
Thanks for all the help. I found another solution in another forum. Since the query fails it's not populating a record count. I had to go back to the CFTry and relable the query. THis code works -

<cftry>
<cfquery name="all" datasource="hr1">
Select EMPLID, COURSE_TITLE
From TRAINING
WHERE EMPLID='#FORM.EMPLOYID#'
And COURSE_TITLE like '#FORM.CLASS#%'
</cfquery>
<cfcatch type="database">
<cfset all = QueryNew(EMPLID)/>
</cfcatch>
</cftry>

Then using the recordcount feature it outputs the correct response

<cfoutput query="all">
<cfif all.recordcount GTE 1>
<cfmail to="email@someplace" from="email@someplace" subject="Please unenroll">#form.employID# wishes to remove from the course #COURSE_TITLE#</cfmail>
You selected to unregister for <em>#COURSE_TITLE#</em>. An email has been sent to your supervisor.
<cfelse>
Our records do not show that you registered for this course. <a href="dumppage.cfm">Please select another</a>.
</cfif>
</cfoutput>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation