Solved

Code Snippets for Coldfusion Components

Posted on 2006-11-14
3
234 Views
Last Modified: 2013-12-24
Can someone provide me with some Code Snippets for a Coldfusion Component that will allow me to add, edit, update, & delete data.  

I would like code for the CFC File as well as the CFM file that I am using,  You don't have to custom name anything just use examples

like Datasource1, Variable2, Form3, etc..

 I just need to use this to store on Dreamweaver  

I will raise the amount of points if someone would like to throw in a custom tag snippet, and provide a significant value within the Component, or CFM file

Also, if the hints on the CFC can be very detailed with its hints I would appreciate it!!!!

0
Comment
Question by:rudodoo
3 Comments
 
LVL 3

Expert Comment

by:bhinshawnc1
ID: 17943511
0
 
LVL 20

Accepted Solution

by:
trailblazzyr55 earned 250 total points
ID: 17947461
here is an example of a CFC, now there's tons of different ways to go about doing something, this just shows some syntax through the update, delete, add, and get.... again, depending on your needs it could be very different... the methods in this cfc check for errors in the queries as well... the links provided above should give you an idea on how to use the cfc to do what you want... also I'd really recommend if you have Ben Forta's / Ray Camden's "Coldfusion Web Application Construction Kit" book, you read through the chapters discussing CFC's.....

http://www.amazon.com/Macromedia-ColdFusion-Web-Application-Construction/dp/0321223675/sr=1-1/qid=1163603218/ref=pd_bbs_sr_1/102-1153260-6204136?ie=UTF8&s=books

<cfcomponent hint="I start a CFC, I must be at the start of every CFC file, I contain methods to add, update, delete, and get records of customers.">

<cffunction name="addCustomer" access="public" returntype="struct" output="false" hint="I add a customer to the database">
      <cfargument name="custFirstName" required="yes" type="string">
      <cfargument name="custLastName" required="yes" type="string">
      <!--- define any variables used locally here in the "var" scope first --->
      <cfset var currDateTime = now()>
      <!--- the query status here is to provide feedback to your app on the success or failure of your query --->
      <cfset var qryStatus = structnew()>
      <cfset qryStatus['success'] = 0 > 
      <cfset qryStatus['errMessage'] = "">
      <cfset qryStatus['errDetail'] = "">
      <cfset qryStatus['errCode'] = "">
      <cfset qryStatus['errType'] = "">
      <cftry>
            <cfquery datasource="YOUR_DSN">
                  INSERT INTO yourTable (firstName,LastName,createdDate)
                  VALUES ('#arguments.custFirstName#','#arguments.custLastName#','#currDateTime#')
            </cfquery>
            <cfcatch type="any">
                  <!--- provide some feedback for any errors --->
                  <cfset qryStatus['errMessage'] = cfcatch.Message>
                  <cfset qryStatus['errDetail'] = cfcatch.Detail>
                  <cfset qryStatus['errCode'] = cfcatch.ErrNumber>
                  <cfset qryStatus['errType'] = cfcatch.Type>
                  <cfset qryStatus['success'] = -1 >
            </cfcatch>
      </cftry>
      <cfreturn qryStatus>
</cffunction>

<cffunction name="updateCustomer" access="public" returntype="struct" output="false" hint="I update a customer in the database">
      <cfargument name="custFirstName" required="yes" type="string">
      <cfargument name="custLastName" required="yes" type="string">
      <cfargument name="custID" required="yes" type="numeric">
      <!--- define any variables used locally here in the "var" scope first --->
      <cfset var currDateTime = now()>
      <!--- the query status here is to provide feedback to your app on the success or failure of your query --->
      <cfset var qryStatus = structnew()>
      <cfset qryStatus['success'] = 0 > 
      <cfset qryStatus['errMessage'] = "">
      <cfset qryStatus['errDetail'] = "">
      <cfset qryStatus['errCode'] = "">
      <cfset qryStatus['errType'] = "">
      <cftry>
            <cfquery datasource="YOUR_DSN">
                  UPDATE yourTable
                  SET firstName = '#arguments.custFirstName#',
                      LastName = '#arguments.custLastName#',
                        modifiedDate = '#currDateTime#'
                  WHERE customerID = #arguments.custID#
            </cfquery>
            <cfcatch type="any">
                  <!--- provide some feedback for any errors --->
                  <cfset qryStatus['errMessage'] = cfcatch.Message>
                  <cfset qryStatus['errDetail'] = cfcatch.Detail>
                  <cfset qryStatus['errCode'] = cfcatch.ErrNumber>
                  <cfset qryStatus['errType'] = cfcatch.Type>
                  <cfset qryStatus['success'] = -1 >
            </cfcatch>
      </cftry>
      <cfreturn qryStatus>
</cffunction>

<cffunction name="deleteCustomer" access="public" returntype="struct" output="false" hint="I delete a customer in the database">
      <cfargument name="custID" required="yes" type="numeric">
      <!--- define any variables used locally here in the "var" scope first --->
      <cfset var qryStatus = structnew()>
      <!--- the query status here is to provide feedback to your app on the success or failure of your query --->
      <cfset qryStatus['success'] = 0 > 
      <cfset qryStatus['errMessage'] = "">
      <cfset qryStatus['errDetail'] = "">
      <cfset qryStatus['errCode'] = "">
      <cfset qryStatus['errType'] = "">
      <cftry>
            <cfquery datasource="YOUR_DSN">
                  DELETE FROM yourTable
                  WHERE customerID = #arguments.custID#
            </cfquery>
            <cfcatch type="any">
                  <!--- provide some feedback for any errors --->
                  <cfset qryStatus['errMessage'] = cfcatch.Message>
                  <cfset qryStatus['errDetail'] = cfcatch.Detail>
                  <cfset qryStatus['errCode'] = cfcatch.ErrNumber>
                  <cfset qryStatus['errType'] = cfcatch.Type>
                  <cfset qryStatus['success'] = -1 >
            </cfcatch>
      </cftry>
      <cfreturn qryStatus>
</cffunction>

<cffunction name="getCustomer" access="public" returntype="struct" output="false" hint="I get a customer in the database">
      <cfargument name="custID" required="yes" type="numeric">
      <!--- define any variables used locally here in the "var" scope first --->
      <cfset var myQry = "">
      <cfset var qryStatus = structnew()>
      <cfset var custInfo = structnew()>
      <cfset var rtnStruct = structnew()>
      <!--- the query status here is to provide feedback to your app on the success or failure of your query --->
      <cfset qryStatus['success'] = 0 > 
      <cfset qryStatus['errMessage'] = "">
      <cfset qryStatus['errDetail'] = "">
      <cfset qryStatus['errCode'] = "">
      <cfset qryStatus['errType'] = "">
      <!--- the cust info here is to provide the first an last name of the customer based on the id passed to the method --->
      <cfset custInfo['firstName'] = "">
      <cfset custInfo['lastName'] = "">
      <cftry>
            <cfquery name="myQry" datasource="YOUR_DSN">
                  SELECT firstName, lastName
                  FROM yourTable
                  WHERE customerID = #arguments.custID#
            </cfquery>
            <cfcatch type="any">
                  <!--- provide some feedback for any errors --->
                  <cfset qryStatus['errMessage'] = cfcatch.Message>
                  <cfset qryStatus['errDetail'] = cfcatch.Detail>
                  <cfset qryStatus['errCode'] = cfcatch.ErrNumber>
                  <cfset qryStatus['errType'] = cfcatch.Type>
                  <cfset qryStatus['success'] = -1 >
            </cfcatch>
      </cftry>
      <cfif myQry.recordcount>
            <cfset custInfo['firstName'] = myQry.firstName>
            <cfset custInfo['lastName'] = myQry.lastName>
      </cfif>
      <!--- put both structs into one struct to return to your app --->
      <cfset rtnStruct['qryStatus'] = qryStatus >
      <cfset rtnStruct['custInfo'] = custInfo >
      <cfreturn rtnStruct>
</cffunction>
<!--- close out your component, this should conclude your .cfc file --->
</cfcomponent>
0
 

Author Comment

by:rudodoo
ID: 17997341
Thanks for the snippet, I appreciate it
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question