Sunday, March 25, 2012

Test Your ColdFusion skill ( Part - II)(ColdFusion - 8 and 9 )




  1. Which of following tags with errors will NOT be caught with a <cfcatch > block?
    < CFEST X = 5 / 0 >
    < CFSET X = "Y" + 1 >
    < cfinclude template="myFile.cfm" > where myFile.cfm is NOT found.
    None of these.

  2. If you have the following variable definition in the Application.cfm file, what is the scope of the variable after it is created?
    < cfset x="foo" >

    Variables (local)
    Application
    Session
    Request

  3. Which of the following scope variables can not store a ColdFusion query object?
    Request Scope Variables.
    Thread Scope Variables.
    Client Scope variables.
    Local scope Variables.

  4. What will be the outputof the following code?
    ABC < cfcontent reset="Yes" type="text/html" > DEF.

    ABC
    DEF
    ABCDEF
    None Of these

  5. Choose the correct syntax for looping over a structure.
    < cfloop collection="struct" item="i" >
    < cfloop collection="#struct#" item="s" >
    < cfloop from="#struct#" index="i" to="#StructLen(struct)#" >
    < cfloop collection="#struct#" index="i" >

  6. Which of the following is not true about < cfheader >?
    It creates attribute/value pairs in the request header.
    It creates attribute/value pairs in the response header.
    It can be used to prevent a server from caching a page.
    None of these.

  7. What is the correct syntax to include a template ?
    < cfinclude template="./myinclude.cfm" >
    < cfinclude template="C:\inetpub\wwwroot\doccomments\myinclude.cfm" >
    < cfinclude template="http://localhost:8500/doccomments/myinclude.cfm" >
    None of these.

  8. What is the output of below code?
    < cfset variables.str="Hello" >
    < cfoutput > #variables.str# </cfoutput >
    < cfsilent>
    < cfset variables.str="World" >
    < cfoutput> #variables.str # </cfoutput >
    < /cfsilent >

    Hello World
    Hello
    World
    None of these.

  9. Which of the following not true about structCopy() and duplicate() ?
    structCopy(): Copies nested structures by reference.
    structCopy(): If the original structure get changed then it doesn't affects the copied structure.
    duplicate(): There is no reference to the original variable
    duplicate(): This is also known as deep copy.

  10. Which of the following will not “obey” a timeout, whether it is set in the ColdFusion Administrator or in code on the CFM page?
    cfquery
    cfthread
    cfstoredproc
    None of these.



Tuesday, March 06, 2012

Escape plus(+) sign in url

Escape plus(+) sign in URL

When we pass some special characters to server side(in URL) for processing we prefer to use the JavaScript function escape(). The work of escape() is to convert the special characters into corresponding ASCII characters.  The server side code automatically does the conversion from ASCII to the original characters.

       But when we pass a plus(+) sign in the escape() function it doesn't converts the plus(+) sign to the corresponding ASCII character. Rather it replaces the plus (+) sign with a space character. Which cause many issue during the AJAX call to the server side.

Also, if we are thinking to avoid escape() function and pass the string directly to then also it will not work.

Solution:
  When we are escaping a string before escaping that we will convert all the plus(+) sign with the corresponding ASCII character . Then during escape function call it will work like the normal way.

e.g- 
var  strQueryString = "SELECT 1+1 FROM EMPLOYEES";
  1. escape(strQueryString).replace(new RegExp( "\\+", "g" ),"%2B") (will convert plus sign to ASCII)
  2. escape(strQueryString) (will not convert plus sign to ASCII)

You can simply see the difference by the above  code.

Export cfgrid Data or Table Data in Excel, Pdf and CSV Format(ColdFusion - 9)

Export cfgrid Data or Table Data in Excel, Pdf and CSV Format(ColdFusion - 9)

Below I have posted the sample code for exporting a table data in Excel/Pdf/CSV format. The code contains the comment line for each of the action. I think it will help you to understand.

<!--- The format to Which you want to Export the Data --->
<cfparam name="url.format" default="csv" />

<cftry>
    <cfset request.qryGetData = queryNew("") /><!--- The Query Object. To Which we will Export --->
    <cfset request.queryResult = structNew() /><!--- Result set of the Query Object --->
   
    <!--- Query to get data from the specified DSN. This DSN created when the ColdFusion is installed in your machine--->
    <cfquery name="request.qryGetData" datasource="cfdocexamples" result="request.queryResult">
        SELECT * FROM EMPLOYEES
    </cfquery>
   
    <!--- Check the format Requested to download and Prepare the Document Accordingly --->
    <cfif url.format EQ "csv">
       
        <!--- Preparing the CSV Document --->
        <cfset request.strCSVString = "" />
       
        <!--- Storing the comma separated column name in a string Object and Appending chr(13) and chr(10) for line break and carriage return--->
        <cfset request.strCSVString = request.strCSVString & "#request.qryGetData.columnList#" & chr(13) & chr(10) />
        <cfset request.rowArray = arrayNew(1) />
        <cfloop query="request.qryGetData">
            <cfset request.rowArray = arrayNew(1) />
            <cfset request.columnCntr = 1 />
           
            <!--- Stroing the row of a query object into an array--->
            <cfloop list="#request.qryGetData.columnList#" index="colname">
                <cfset request.rowArray[request.columnCntr] = request.qryGetData[colname][request.qryGetData.currentRow]>
                <cfset request.columnCntr += 1 />
            </cfloop>
           
            <!--- Converting the array into a list. So that the entire row will be converted into a comma separated  list--->
            <cfset request.strCSVString = request.strCSVString & arrayToList(request.rowArray) & chr(13) & chr(10) />
        </cfloop>
       
        <!--- Convert the comma separated string content into a CSV file(download.csv) and this file will be downloaded.--->
        <cfcontent type="application/csv">
        <cfheader name="content-disposition" value="inline; filename=download.csv">
        <cfoutput>#request.strCSVString#</cfoutput>
    <cfelseif url.format EQ "excel">
   
        <!--- Preparing the Query Object into tabular format --->
        <cfsavecontent variable="request.exportContent">
            <table>
                <tr>
                <cfoutput>
                   
                    <!--- Writting all Column Names as table header--->
                    <cfloop index="columnName" list="#request.queryResult.COLUMNLIST#">
                        <th>#columnName#</th>
                    </cfloop>
                </cfoutput>
                </tr>
               
                <!---Loop over the query object and get the column name dynamically from the result set and fetch the cell
                    value of table and write it in the string object--->
                <cfoutput query="request.qryGetData">
                    <tr>
                        <cfloop index="columnName" list="#request.queryResult.COLUMNLIST#">
                            <td>#evaluate("request.qryGetData.#columnName#")#</td>
                        </cfloop>
                    </tr>
                </cfoutput>
            </table>
        </cfsavecontent>
       
        <!--- Convert the tabular string data into an excel document(download.xls)--->
        <cfcontent type="application/msexcel">
        <cfheader name="content-disposition" value="inline; filename=download.xls">
        <cfoutput>#request.exportcontent#</cfoutput>
    <cfelseif url.format EQ "pdf">
   
        <!--- Preparing the Query Object into tabular format --->
        <cfsavecontent variable="request.exportContent">
            <table>
                <tr>
                <cfoutput>
                   
                    <!--- Writting all Column Names as table header--->
                    <cfloop index="columnName" list="#request.queryResult.COLUMNLIST#">
                        <th>#columnName#</th>
                    </cfloop>
                </cfoutput>
                </tr>
               
                <!---Loop over the query object and get the column name dynamically from the result set and fetch the cell
                    value of table and write it in the string object--->
                <cfoutput query="request.qryGetData">
                    <tr>
                        <cfloop index="columnName" list="#request.queryResult.COLUMNLIST#">
                            <td>#evaluate("request.qryGetData.#columnName#")#</td>
                        </cfloop>
                    </tr>
                </cfoutput>
            </table>
        </cfsavecontent>
       
        <!--- Convert the tabular string data into a pdf document(download.pdf)--->
        <cfheader name="content-disposition" value="inline; filename=download.pdf">
        <cfdocument format="pdf">
            <cfoutput>#request.exportcontent#</cfoutput>
        </cfdocument>
    <cfelse>
        Error in Preparing the Document.
    </cfif>
    <cfcatch>
        Error In Preparing the Document.
    </cfcatch>
</cftry>

By using the above code we can export a Query Object Data into different format.

Then how we will export cfgrid data with  a particular page no??
Ans:
 Just you have to pass two another parameter in the URL. i.e

<cfparam name="url.pageNo" default="1" />
<cfparam name="url.pageCount" default="100" />

Now, when we are looping over the query object for creating CSV/Tabular string we have to set the start and end value. Like the below:

<cfset request.start = ((url.pageNo - 1) * url.pageCount) + 1 />
<cfset request.end = (request.start - 1) + url.pageCount />

<cfloop 
    query = "query name"  //request.qryGetData
    startRow = "row number" // request.start
    endRow = "row number" //request.end
</cfloop>


Hope you will enjoy with the code. :)

Other Grid related topics:
1. Starting with CFGRID( part - 1 )
2. Starting with CFGRID( part - 2 )(Auto Refreshing CFGRID)
3. Conditionally Change the Color Of a Cell Text In cfgrid
4. Search Functionality in CFGRID
5. Export Query Object in Spreadsheet using spreadsheet object.


Followers