Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i set up a connection to a database on a cfscript tag?

Status
Not open for further replies.

devondago

Programmer
Jan 22, 2003
38
US
I need help with this application.cfm file...I am having problems in 2 places....Application variables....If I am reading correctly I set up my database connection there....I have change it to reflect...Application.mhs74159 ="Request.App"; Application.DBType = "SQLServer"; but later on the page it states to use the as Datasource = #Request.App.DS#". When i run the pages with the the variables set to Request.App.ds I get the following error on the login_process.cfm page.
DATASOURCE

The value cannot be converted to a string because it is not a simple value. Simple values are booleans, numbers, strings, and date-time values.

The error occurred while processing an element with a general identifier of (CFQUERY), occupying document position (13:4) to (13:54).


Date/Time: 03/04/03 13:14:02

Can someone help me set up the right variables?
<!---


Called From:
Automatically called by all ColdFusion pages within
the same directory or children directories

--->

<!--- Define the application. --->
<!--- applicationtimeout is not set and will use the default of the server. --->
<cfapplication name=&quot;Apps&quot;
sessiontimeout=&quot;#CreateTimeSpan(0,0,20,0)#&quot;
sessionmanagement=&quot;yes&quot; />

<!---
Set a default value for Application.Initialized. If the application scope
has not yet been created, the value will be false. Set the value to a
request variable so the application initialization can be tested without
needing to perform a lock.
--->
<cflock scope=&quot;Application&quot; timeout=&quot;10&quot; type=&quot;exclusive&quot;>
<cfparam name=&quot;Application.Initialized&quot; default=&quot;false&quot; />
<cfset Request.AppInitialized = Application.Initialized />
</cflock>

<!--- If the Application has not been initialized, set the values. --->
<cfif NOT Request.AppInitialized>
<cflock scope=&quot;Application&quot; timeout=&quot;10&quot; type=&quot;exclusive&quot;>
<cfscript>
Application.Initialized = true;

// Application variables
// ------------------------
Application.DS = &quot;WebTricks_Apps&quot;; // Change the DS name to the appropriate database DS
Application.DBType = &quot;Access&quot;; // Can be SQLServer, Access or Oracle

// Calendar variables
// ------------------------
// Can anyone add an event? If set to false, only logged in Administrators
// can add events to the calendar.
Application.CalendarAllSubmit = true;
// Should the &quot;Add to Outlook&quot; link display on event details?
Application.CalendarShowOutlookLink = true;
// Set the start and end times to be displayed on the daily calendar
// Minute values must be either 00 or 30 for start or 29 or 59 for end
Application.CalendarStartTime = &quot;08:00&quot;; // Default to &quot;00:00&quot;
Application.CalendarEndTime = &quot;23:59&quot;; // Default to &quot;23:59&quot;
</cfscript>
</cflock>
</cfif>

<!---
Set a default value for Session.Initialized. If the session scope
has not yet been created, the value will be false. Set the value to a
request variable so the application session can be tested without
needing to perform a lock.
--->
<cflock scope=&quot;Session&quot; timeout=&quot;10&quot; type=&quot;exclusive&quot;>
<cfparam name=&quot;Session.Initialized&quot; default=&quot;false&quot; />
<cfset Request.SesInitialized = Session.Initialized />
</cflock>

<!--- If the Session has not been initialized, set the values. --->
<cfif NOT Request.SesInitialized>
<cflock scope=&quot;Application&quot; timeout=&quot;10&quot; type=&quot;exclusive&quot;>
<!---
Set the default session variables.
These may change if/when the user logs in.
--->
<cfscript>
Session.Initialized = true;
Session.LoggedInFlag = false;
Session.UserID = &quot;&quot;;
Session.AdminFlag = false;
Session.EmailAddress = &quot;&quot;;
Session.Name = &quot;&quot;;
Session.LastLogin = &quot;&quot;;
</cfscript>
</cflock>
</cfif>

<!---
Because Session and Application variables need to be locked
when both read from and written to, the Application and Session
scopes are copied (duplicated) into Request variables that can
be accessed throughout the application without needing to perform a
lock to do a read.

Locks still need to be performed when writing to the Session and
Application Scopes.

To output Session.VarName, just use Request.Ses.VarName
to get the value of Session.VarName without having to use a lock.

Datasource = #Request.App.DS#&quot;

This duplication will be performed each time a page is run
and takes no server overhead.
--->
<cflock scope=&quot;Application&quot; timeout=&quot;10&quot; type=&quot;readonly&quot;>
<cfset Request.App = Duplicate(Application) />
</cflock>

<cflock scope=&quot;Session&quot; timeout=&quot;10&quot; type=&quot;readonly&quot;>
<cfset Request.Ses = Duplicate(Session) />
</cflock>

<!--- End Application.cfm --->
 
Just a point of clarification: you are not actually setting up a database connection; you are merely defining some strings that you will use later in your CFQUERY tags.

The message is saying that Request.App.DS is not a simple value. It is some complex data type (probably a structure). This could have happened because you made an error in the assignment of Application.DS to Request.App.DS.

(1) The first thing that stands out is this:

Application.mhs74159 =&quot;Request.App&quot;

This is incorrect. The Request structure (which contains values intended to only be read) is initialized by copying the Application and Session structures, like this:

Request.App = Duplicate(Application)
Request.Ses = Duplicate(Session)

You are initializing a variable in the Application scope from a variable in the Request scope. You should do something like the &quot;WebTricks_App&quot; line below.

(2) The Application.cfm code that you included does not look like your actual code.

Is this the actual code that produced the error message?

The pertinent parts of it (that relate to your error) are:

Application.DS = &quot;WebTricks_Apps&quot;
Request.App = Duplicate(Application)

This is what you show, and it appears to be correct; that is why I don't think that you are showing us the actual code that produced the error message.
 
ok....i think is what you may need in order to help me figure out the mystery....here are the 2 login and login_process.cfm files that take variables from the application.cfm file on my first post. If you noticed on the login_process.cfm file there is a call to the database. <cfquery datasource=&quot;#Request.App.DS#&quot; name=&quot;GetUser&quot;> the datasource name request.app.ds is what is giving me the error i mentioned on my first post. I tried setting up a cfparam and calling the request.app.ds as my db connection but I get the same error. Any suggestions? ps. thanks for responding back...
login.cfm file
<!--


Description:
Login Page.
Includes the login form.

-->

<!--- Set the variables to be used throught the application. --->
<cfscript>
Request.RootDir = &quot;&quot;;
Request.PageTitle = &quot;Application Login&quot;;
</cfscript>

<!--- Include the header file. --->
<cfinclude template=&quot;#Request.RootDir#inc_header.cfm&quot; />

<h1>Login Form</h1>

<!--- Set the variables for the included form. --->
<cfset Request.FormAction = &quot;login_process.cfm&quot; />
<cfset Request.RedirectTo = &quot;index.cfm&quot; />

<!--- Include the form. --->
<cfinclude template=&quot;inc_login_form.cfm&quot; />

<!--- Include the footer file. --->
<cfinclude template=&quot;#Request.RootDir#inc_footer.cfm&quot; />


<!-- End index.cfm -->
login_process.cfm
<!---



Description:
Processes the login form

Required variables:
FORM.RedirectTo

--->

<!--- If FORM.RedirectTo doesn't exist, form wasn't submitted - back to login page. --->
<cfif NOT IsDefined(&quot;FORM.RedirectTo&quot;)>
<cflocation url=&quot;login.cfm&quot; addtoken=&quot;no&quot; />
</cfif>

<!--- Include the validation file. --->
<cfinclude template=&quot;inc_login_begin_process.cfm&quot; />

<!--- If Request.ErrorMessage is empty, check to see if login is valid. --->
<cfif NOT Len(Trim(Request.ErrorMessage))>
<!--- Query the user record. --->
<cfquery datasource=&quot;#Request.App.DS#&quot; name=&quot;GetUser&quot;>
SELECT UserID,
UserName,
FullName,
Password,
EmailAddress,
AdminFlag,
LastLoginDate
FROM SiteUser
WHERE UserName = <cfqueryparam cfsqltype=&quot;CF_SQL_VARCHAR&quot; value=&quot;#Request.UserName#&quot; />
</cfquery>

<!--- If there are no records, the user doesn't exist. --->
<cfif GetUser.RecordCount EQ 0>
<cfset Request.ErrorMessage = Request.ErrorMessage & &quot;The user name you entered is not valid. Please try again.<br>&quot; />
<!--- If the user does exist, is the password valid? --->
<cfelseif GetUser.Password NEQ Request.Password>
<cfset Request.ErrorMessage = Request.ErrorMessage & &quot;You have entered an incorrect password for this user name. Please try again.<br>&quot; />
</cfif>
</cfif>

<!--- If the error message has length, display the login page again. --->
<cfif Len(Trim(Request.ErrorMessage))>
<cfinclude template=&quot;login.cfm&quot; />
<cfabort />
</cfif>

<!--- All is well, log the user in. --->

<!--- Update the last login date/time in the database. --->
<cfquery datasource=&quot;#Request.App.DS#&quot;>
UPDATE SiteUser
SET LastLoginDate = <cfqueryparam cfsqltype=&quot;CF_SQL_DATE&quot; value=&quot;#Now()#&quot; />
WHERE UserID = #GetUser.UserID#
</cfquery>

<!--- Set the session variables for the user. --->
<cflock scope=&quot;Session&quot; type=&quot;exclusive&quot; timeout=&quot;5&quot;>
<cfscript>
Session.LoggedInFlag = true;
Session.UserID = GetUser.UserID;
if (Val(GetUser.AdminFlag) EQ 0) {
Session.AdminFlag = false;
} else {
Session.AdminFlag = true;
}
Session.EmailAddress = GetUser.EmailAddress;
Session.Name = GetUser.FullName;
Session.LastLogin = GetUser.LastLoginDate;
</cfscript>
</cflock>

<!--- Redirect to the indicated page. --->
<cflocation url=&quot;#Request.RedirectTo#&quot; addtoken=&quot;no&quot; />

<!--- End login_process.cfm --->
 
devondago,

Thanks for posting the login script. However, the problem seems to be in the initialization of Request.App.DS. I believe that you are doing this in Application.cfm. As I said in my earlier reply, the code for Application.cfm that you posted appears to be correct, but since you are getting an error, I suspect that the code that you posted is not the code that you are actually using. Please show the code where you initialize (assign a value) to Request.App.DS.
 
ok here is the appl file that came with the application. I am trying to test......
<cfapplication name=&quot;Apps&quot;
sessiontimeout=&quot;#CreateTimeSpan(0,0,20,0)#&quot;
sessionmanagement=&quot;yes&quot; />

<!---
Set a default value for Application.Initialized. If the application scope
has not yet been created, the value will be false. Set the value to a
request variable so the application initialization can be tested without
needing to perform a lock.
--->
<cflock scope=&quot;Application&quot; timeout=&quot;10&quot; type=&quot;exclusive&quot;>
<cfparam name=&quot;Application.Initialized&quot; default=&quot;false&quot; />
<cfset Request.AppInitialized = Application.Initialized />
</cflock>

<!--- If the Application has not been initialized, set the values. --->
<cfif NOT Request.AppInitialized>
<cflock scope=&quot;Application&quot; timeout=&quot;10&quot; type=&quot;exclusive&quot;>
<cfscript>
Application.Initialized = true;

// Application variables
// ------------------------
Application.DS = &quot;WebTricks_Apps&quot;; // Change the DS name to the appropriate database DS
Application.DBType = &quot;Access&quot;; // Can be SQLServer, Access or Oracle

// Calendar variables
// ------------------------
// Can anyone add an event? If set to false, only logged in Administrators
// can add events to the calendar.
Application.CalendarAllSubmit = true;
// Should the &quot;Add to Outlook&quot; link display on event details?
Application.CalendarShowOutlookLink = true;
// Set the start and end times to be displayed on the daily calendar
// Minute values must be either 00 or 30 for start or 29 or 59 for end
Application.CalendarStartTime = &quot;08:00&quot;; // Default to &quot;00:00&quot;
Application.CalendarEndTime = &quot;23:59&quot;; // Default to &quot;23:59&quot;
</cfscript>
</cflock>
</cfif>

<!---
Set a default value for Session.Initialized. If the session scope
has not yet been created, the value will be false. Set the value to a
request variable so the application session can be tested without
needing to perform a lock.
--->
<cflock scope=&quot;Session&quot; timeout=&quot;10&quot; type=&quot;exclusive&quot;>
<cfparam name=&quot;Session.Initialized&quot; default=&quot;false&quot; />
<cfset Request.SesInitialized = Session.Initialized />
</cflock>

<!--- If the Session has not been initialized, set the values. --->
<cfif NOT Request.SesInitialized>
<cflock scope=&quot;Application&quot; timeout=&quot;10&quot; type=&quot;exclusive&quot;>
<!---
Set the default session variables.
These may change if/when the user logs in.
--->
<cfscript>
Session.Initialized = true;
Session.LoggedInFlag = false;
Session.UserID = &quot;&quot;;
Session.AdminFlag = false;
Session.EmailAddress = &quot;&quot;;
Session.Name = &quot;&quot;;
Session.LastLogin = &quot;&quot;;
</cfscript>
</cflock>
</cfif>

<!---
Because Session and Application variables need to be locked
when both read from and written to, the Application and Session
scopes are copied (duplicated) into Request variables that can
be accessed throughout the application without needing to perform a
lock to do a read.

Locks still need to be performed when writing to the Session and
Application Scopes.

To output Session.VarName, just use Request.Ses.VarName
to get the value of Session.VarName without having to use a lock.

Datasource = #Request.App.DS#&quot;

This duplication will be performed each time a page is run
and takes no server overhead.
--->
<cflock scope=&quot;Application&quot; timeout=&quot;10&quot; type=&quot;readonly&quot;>
<cfset Request.App = Duplicate(Application) />
</cflock>

<cflock scope=&quot;Session&quot; timeout=&quot;10&quot; type=&quot;readonly&quot;>
<cfset Request.Ses = Duplicate(Session) />
</cflock>

<!--- End Application.cfm --->
 
OK, so that IS the code that you are using. Now I'm confused ...

In you initial post, your wrote:

I have change it to reflect...Application.mhs74159=&quot;Request.App&quot;

What did you mean by that? I didn't see any code like that in your Application.cfm.
 
thank you for writing back...yes on my orginal post i had changed it to Application.mhs74159=&quot;Request.App&quot; however i sent you the original file i got with the application...I have been doing some other testing and if I dont use this file and declare my database connection and variables and the other pages it seem to work...But maybe you can help me solve another mystery....there is a file where it is allow to upload images, the code reads..
<cfset Request.UploadDirectory = GetDirectoryFromPath(GetCurrentTemplatePath()) & &quot;images/&quot; />

<!--- here i Get the extension of the uploaded file. --->
<cfset Request.FileExt = ListLast(Request.SystemFileName, &quot;.&quot;) />

<!--- Now I Upload the file. --->
<cffile action=&quot;upload&quot;
destination=&quot;#Request.UploadDirectory#event_#Request.EventID#.#Request.FileExt#&quot;
filefield=&quot;ImageFile&quot;
nameconflict=&quot;overwrite&quot; />

<!--- I Update the event with the name of the file. --->
<cfquery datasource=&quot;#application.datasource#&quot;>
UPDATE Event
SET EventImage = 'event_#Request.EventID#.#Request.FileExt#'
WHERE EventID = #Request.EventID#
</cfquery>
My problem is the following...we use webfileUtils to do our web uploads using asp. there is a certain way in CF to upload images per say.How can I incorporate the asp code into this cf code to allow the image upload?
<asp code>
if action = &quot;upload&quot; then

pth = server.mappath(&quot;..&quot;) & &quot;\images&quot;
Response.Write &quot;server.mappath = &quot; & pth & &quot;<br>&quot;
Response.End

Set ObjWebUpload = Server.CreateObject(&quot;WebFileUtils.WebUpload&quot;)
filename = ObjWebUpload.DoUpload(cstr(pth))
SQL = &quot;insert into volunteer_pics(filename,caption) values ('&quot;&filename&&quot;','&quot;&caption&&quot;')&quot;
set rst = cons.execute(SQL)
set rst = nothing

response.redirect &quot;volunteer_opp_pics_admin.asp?action=main&validateuser=ok&validatepwd=ok&quot;

End If
<end of asp code?
any help I can get on this will really help me....
 
You have lost me now...

You cannot have ASP and Coldfusion.. nor any other server side language be processed at the same time by the same file. the extension of the file is handled by a defined engine.. not all of them.

SO.. you want to upload an image with ASP?
Why... you can simply upload using coldfusion. It is cleaner and simpler to do than ASP. It offers you all that ASP could do. Is it you just need help with the CF code or are you dead set on ASP to do it only?

if so.. have your uploades processed through an ASP page.. seperate. David McIntosh

Have a nice day!
 
I agree that it would be simpler to just do it in ColdFusion, but if you must use the ASP code, here are some thoughts:

The ASP code must be in a separate file with the extension .asp. You will have to invoke the ASP file from within ColdFusion and pass to it the filename. If the ASP file does not take a filename parameter and the other parameters such as filetype, then you will have to modify it to do so. I'm imagining that you will have a <FORM>. The call would be something like this:

<FORM action=fileUpload.asp?action=upload&filename=#Request.UploadDirectory#event_#Request.EventID#.#Request.FileExt#>

<input type=&quot;submit&quot; name=&quot;Upload&quot; value=&quot;Upload File&quot;>
</FORM>

The code above uses the GET method. You could also use the POST method.
 
thansk for all the feedback on this...I got it working in Cf by using
<cffile action=&quot;upload&quot;
destination=&quot;#Request.UploadDirectory#event_#Request.EventID#.#Request.FileExt#&quot;
filefield=&quot;ImageFile&quot;
nameconflict=&quot;overwrite&quot; />
thanks for all the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top