Dinamically Declare variable in ASP

M

Michael

Hi. I am trying to declare variables based on DB column.
I have a table with 2 columns:
"varname" and "varvalue".

values for example:
varName: 'varPhone'
varValue: '450-111-2222'

During the looping I'd like to declare on asp page variables that taken from
varname column and assign values taken from varValue column.
i think the Execute method can help me, but failed to do this.

Some code I am going to use is here:

Do while not rs.eof
first=rs("varName')
second=rs("varValue")
Execute(first & "=" & second) ?????????
rs.movenext
Loop

Thanks a lot for help
Michael
 
E

Evertjan.

Michael wrote on 17 dec 2006 in microsoft.public.inetserver.asp.general:
Hi. I am trying to declare variables based on DB column.
I have a table with 2 columns:
"varname" and "varvalue".

values for example:
varName: 'varPhone'
varValue: '450-111-2222'

During the looping I'd like to declare on asp page variables that
taken from varname column and assign values taken from varValue
column. i think the Execute method can help me, but failed to do this.

Some code I am going to use is here:

Do while not rs.eof
first=rs("varName')
second=rs("varValue")
Execute(first & "=" & second) ?????????
rs.movenext
Loop

I think that is a bad idea,
because you will get into trouble over reserved names.

Try ASP-jscript and an enumerated object/array.

var namesObject[]
namesObject[first] = second

No eval-like evil tures necessary.
 
B

Bob Barrows [MVP]

Michael said:
Hi. I am trying to declare variables based on DB column.
I have a table with 2 columns:
"varname" and "varvalue".

values for example:
varName: 'varPhone'
varValue: '450-111-2222'

During the looping I'd like to declare on asp page variables that
taken from varname column and assign values taken from varValue
column. i think the Execute method can help me, but failed to do this.

Execute is "evil" :
http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx.
Some code I am going to use is here:

Do while not rs.eof
first=rs("varName')
second=rs("varValue")
Execute(first & "=" & second) ?????????
rs.movenext
Loop

Thanks a lot for help
Michael

Why are you doing this? It's not like you are going to even know the names
of these variables until runtime. What is the point?

Let's assume you have some valid reason for doing this. Are you planning to
store these things in Session or Application?

If so, I would suggest storing it as an xml document (if you need some code
for this, let us know).

If not, I would recommend using a Dictionary object instead of using the
"evil" Execute method For the best efficiency, I would suggest using a
GetRows array, like this:

dim ar, dict
sql="select varname,varvalue from table where ... "
set rs=conn.execute(sql,,1)
if not rs.eof then ar=rs.Getrows
rs.close:set rs=nothing
conn.close: set conn=nothing

if isArray(ar) then
set dict=createobject("scripting.dictionary")
for i = 0 to ubound(ar,2)
dict.add ar(0,i), ar(1,i)
next
else
response.write "no data was retrieved"
end if
 
M

Michael

Thank you for respond. I'd like to explain Idea of script I need. All
variable i am using on website I'd like to use as constants during web
session. I mean to load all constants(that kept in varName field) at the
beginning of session and after that to use them in all pages while web user
is connected.

The another ideas I think could be generating the asp file(every time after
editing my table) which will "include" file and will be used by "include"
statement. The XML style Code very appreciated. Thanks again
Michael
 
B

Bob Barrows [MVP]

OK, start with the same framework, but instead of the Dictionary object, use
an XML Document:
dim ar, xmldoc, root, node
sql="select varname,varvalue from table where ... "
set rs=conn.execute(sql,,1)
if not rs.eof then ar=rs.Getrows
rs.close:set rs=nothing
conn.close: set conn=nothing

if isArray(ar) then
set xmldoc=createobject("msxml2.FreeThreadedDomDocument")
set root=xmldoc.createElement("root")
set xmldoc.documentElement=root
for i = 0 to ubound(ar,2)
set node=xmldoc.createElement(ar(0,i))
node.text= ar(1,i)
root.appendChild node
Set Session("variables")=xmldoc
next
else
response.write "no data was retrieved"
end if



Here is an example of processing that document:
dim xmldoc, root, node
set xmldoc=Session("variables")
set rootxmldoc.documentElement
for each node in root.childNodes
response.write node.nodeName & ": """
response.write node.text & """<BR>"
next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top