469,951 Members | 2,379 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

Dinamically Declare variable in ASP

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

Dec 17 '06 #1
6 1602
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 17 '06 #2
Michael wrote:
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/ar.../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

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Dec 17 '06 #3
--try creating it as a string first,
--you also have a typo in : rs("varName')

Dim myString ,vfirst,vsecond

Do while not rs.eof
vfirst=rs("varName")
vsecond=rs("varValue")
myString = vfirst & "=" & vsecond
Execute(myString ) '?????????
response.write ( "vfirst = " & vfirst & "<br>" & vbcrlf)
rs.movenext
Loop
"Michael" <mi**************@intergraph.comwrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
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

Dec 17 '06 #4
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

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcomwrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...
Michael wrote:
>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/ar.../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

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Dec 18 '06 #5
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.FreeThreadedDomDocumen t")
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

Michael wrote:
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

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcomwrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...
>Michael wrote:
>>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/ar.../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

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so
I don't check it very often. If you must reply off-line, then remove
the "NO SPAM"
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Dec 18 '06 #6
Thank you very much Bob. It works perfect.!!!!!!!!!
Michael

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcomwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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.FreeThreadedDomDocumen t")
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

Michael wrote:
>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

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcomwrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...
>>Michael wrote:
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/ar.../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

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so
I don't check it very often. If you must reply off-line, then remove
the "NO SPAM"

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Dec 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by vegetax | last post: by
6 posts views Thread by rick | last post: by
2 posts views Thread by Marco | last post: by
2 posts views Thread by Fabio Cavassini | last post: by
32 posts views Thread by Mateo | last post: by
1 post views Thread by ares.lagae | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.