0
Community Beginner
,
/t5/coldfusion-discussions/cftry-cfcatch-not-displaying-error-message/td-p/282676
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>
<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>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
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...
<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...
LEGEND
,
/t5/coldfusion-discussions/cftry-cfcatch-not-displaying-error-message/m-p/282677#M25260
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.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
wendelyn4
AUTHOR
Community Beginner
,
/t5/coldfusion-discussions/cftry-cfcatch-not-displaying-error-message/m-p/282678#M25261
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>
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>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Advocate
,
/t5/coldfusion-discussions/cftry-cfcatch-not-displaying-error-message/m-p/282679#M25262
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>
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>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/cftry-cfcatch-not-displaying-error-message/m-p/282680#M25263
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Beginner
,
LATEST
/t5/coldfusion-discussions/cftry-cfcatch-not-displaying-error-message/m-p/282681#M25264
Jul 30, 2007
Jul 30, 2007
Copy link to clipboard
Copied
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>
<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>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

