Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Code Snippets for Coldfusion Components

Posted on 2006-11-14
3
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Accepted Solution

by:
trailblazzyr55 earned 750 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
Suggested Courses

670 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