Skip to main content
Participating Frequently
November 7, 2013
Question

How to pass Query Resultset from one Page to another page in Coldfusion

  • November 7, 2013
  • 1 reply
  • 1953 views

Hi,

I have a requirement where I need to export query results to Excel. I have a generic code where i can export details to excel which requires query resultset. Is there any way to get the queryresultset from another page?

To explain in detail, I have queried DB and I have resultset in First Coldfusion CFM page. I need to use that result set in another coldfusion page. Please help.

Thanks,

Ravi

    This topic has been closed for replies.

    1 reply

    Inspiring
    November 7, 2013

    The web is stateless.  For example:

    Visitor 1: Hi Website!  I'd like Page 1.cfm.

    Server: Hey Visitor 1! I've never seen you before.  Sure, here's Page 1.cfm.

    Visitor 1: Ok, my user clicked on Page 2.cfm, can I have that?

    Server: Hey Visitor 1! I've never seen you before!  Sure, here's Page 2.cfm.

    In other words, the server does not know you were the same person who just before visited Page 1.cfm.  You are unknown between your requests.  THAT is statelessness.

    To resolve it, you have to store data into something known as a PERSISTENT scope.  This is a scope that can hold data across requests.  2 of the more popular are the APPLICATION scope (sitewide) and SESSION scope (available to that user)

    So for example you could do this

    <cfquery name="SESSION.data" datasource="my_datasource">

    SELECT * FROM Table WHERE Field = 'value'

    </cfquery>

    Since this data is in the SESSION scope (SESSION.data), it will now exist ACROSS requests (until the session expires).  Note, for this to work, you have to have session management enabled in the application.cfc file AND the user has to be accepting cookies.  (If not, you can place the data variable into the APPLICATION scope)

    Now, on your next page, you can reference SESSION.data (or APPLICATION.data), and the information should still be there.

    Anit_Kumar
    Inspiring
    November 8, 2013

    Alongwith Aegis information, you can have an idea from the below code:-

    <cfquery name = "test" dataSource = "Project">

                                            select Month, Date

                                            where Month='January'

                                            from Calender

    </cfquery>

    <cfspreadsheet

                                            action = "write"

                                            filename="Report.xls"

                                            query="test"

                                            overwrite="true">

    Hope this helps.

    Regards,

    Anit Kumar