Dinamically Declare variable in ASP
Dinamically Declare variable in ASP
am 17.12.2006 17:34:34 von 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
Re: Dinamically Declare variable in ASP
am 17.12.2006 18:05:09 von exjxw.hannivoort
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)
Re: Dinamically Declare variable in ASP
am 17.12.2006 18:19:29 von reb01501
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/archive/2003/11/01/53329.a spx.
>
> 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"
Re: Dinamically Declare variable in ASP
am 17.12.2006 18:31:10 von Jon Paal
--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 & "
" & vbcrlf)
rs.movenext
Loop
"Michael" wrote in message news:%23NO7ElfIHHA.3424@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
>
>
>
Re: Dinamically Declare variable in ASP
am 18.12.2006 07:30:32 von 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
"Bob Barrows [MVP]" wrote in message
news:ef5S79fIHHA.3424@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/archive/2003/11/01/53329.a spx.
>
>>
>> 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"
>
Re: Dinamically Declare variable in ASP
am 18.12.2006 13:01:45 von reb01501
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 & """
"
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]" wrote in message
> news:ef5S79fIHHA.3424@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/archive/2003/11/01/53329.a spx.
>>
>>>
>>> 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"
Re: Dinamically Declare variable in ASP
am 18.12.2006 16:49:41 von Michael
Thank you very much Bob. It works perfect.!!!!!!!!!
Michael
"Bob Barrows [MVP]" wrote in message
news:%23Y5sJxpIHHA.1468@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.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 & """
"
> 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]" wrote in message
>> news:ef5S79fIHHA.3424@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/archive/2003/11/01/53329.a spx.
>>>
>>>>
>>>> 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"
>