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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting a 'const' variable using an IF test? 1

Status
Not open for further replies.

piker42

Programmer
Nov 27, 2001
7
0
0
US
Currently I set the ADO connect string for my site into a 'constant' that is used by all the asp pages on my site. This allows me to change the database that I connect to depending on whether I am in test mode or production mode.

I now want to test for which server the code is running on and then in code set the 'constant' variable to the right connection string. I can easily determine which server is running the code, but if I use either an IF or SELECT to set the 'costant', I get an error message that I can't change a 'constant'. It's like it is ignoring the IF or SELECT and processing every 'const....' statement.

Here's a sample of the code that is causing the trouble:
Code:
<%
	txtServerType = fnServerType
	If txtServerType = "Dev" Then
		' Use for development server
		Const dbDataSource = "Provider=SQLOLEDB.1;Initial1 ..."

	ElseIF txtServerType = "Stage" Then 
		' Use for Staging server
		Const dbDataSource = "Provider=SQLOLEDB.1;Initial2 ..."
	
	ElseIF txtServerType = "Prod" Then 
		' Use for production server
	Const dbDataSource = "Provider=SQLOLEDB.1;Initial3 ..."
	End IF
%>

The code is run only once for each session. The code is by itself in an 'include' file. The 'dbDataSource' variable is used on virturally every page of the web site. I don't want to recode all the pages as this site and other similar sites may have to be redone in 6-9 months anyway, but this small change would save time and eliminate risk to production data in the meantime. Thanks,
 
You can't change a constant, only a variable (hence the names). Constants are great for reducing memory and processing requirements because the ASP processor basicaly just substitutes the value of the constant everywhere on the page before processing it, unlike a variable which, since it can change, only subsitutes on a line by line parsed basis.

There's no reason you can't use a variable (Dim) instead of a constant, in your example, however, like
Code:
<%
    Dim dbDataSource
    txtServerType = fnServerType
    If txtServerType = "Dev" Then
        ' Use for development server
        dbDataSource = "Provider=SQLOLEDB.1;Initial1 ..."

    ElseIF txtServerType = "Stage" Then 
        ' Use for Staging server
        dbDataSource = "Provider=SQLOLEDB.1;Initial2 ..."
    
    ElseIF txtServerType = "Prod" Then 
        ' Use for production server
        dbDataSource = "Provider=SQLOLEDB.1;Initial3 ..."
    End IF
%>
I like to use an application variable for my connection string, one that I set in the global.asa, using a similar "which server am I on" logic structure. In my global.asa, using your code, I'd put:
Code:
<%
    txtServerType = fnServerType
    If txtServerType = "Dev" Then
        ' Use for development server
        Application("dbDataSource") = "Provider=SQLOLEDB.1;Initial1 ..."

    ElseIF txtServerType = "Stage" Then 
        ' Use for Staging server
        Application("dbDataSource") = "Provider=SQLOLEDB.1;Initial2 ..."
    
    ElseIF txtServerType = "Prod" Then 
        ' Use for production server
        Application("dbDataSource") = "Provider=SQLOLEDB.1;Initial3 ..."
    End IF
%>
and then in my code just use Application("dbDataSource") for my connection string. This makes it available to every page.
 
Thanks, Genimuse, I wasn't trying to change the constant, just determine what the value should be for this session by using the IF or Select logic. OH Well. I'm going to try both of your suggestions to see how they work. I know one of them will.
 
I know you weren't intentionally trying to change the value of the constant, but that's the way the compiler would see it. :) Best of luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top