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!

Hi, I get confused, code(1) does no

Status
Not open for further replies.

123ASP

MIS
Nov 2, 2002
239
0
0
US
Hi, I get confused, code(1) does not cause error, but code (2) cause error. If I put actual field names,it work, but when I use the position in the rst collection does not work for the second field. Can any one help to know why doest not work. in Wrox book "asp 3.0 page 339", shows that I can use one of the following and it will work:
rst.fields("state_id").value
rst("").value
rst(1).value
rst.fields(1).value
--------------------
While Not rs.EOF
response.Write &quot;<option value='&quot;& rs.fields(1).value& &quot;'>&quot;& rs.fields(&quot;state&quot;).value&&quot;</option>&quot;
rs.MoveNext
Wend

Code (2)
While Not rs.EOF
response.Write &quot;<option value='&quot;& rs.fields (1).value& &quot;'>&quot;& rs.fields(2).value&&quot;</option>&quot;
rs.MoveNext
Wend

Thanks for your hlep
 
How many fields do you have in your database table?

If you only have 2 fields then the first one should be referred to as rs(0) and the second field as rs(1)...

...or rs.fields(0).value and rs.fields(1).value

Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
Thanks, it worked, Yet, I am getting an error when I replace the number (1) with iCount . like this code:
iCount= 0
for each fld in rst.Fields
iCount=iCount +1
vcol = rst.fields(iCount).name
response.Write vcol
next
----
running the above code cause error ADODB.Recordset
item cannot be found in the collection corresponding to the requested name or ordinal. But when I replace iCount with (1) or any number corresponding to the field order, it work, is there a way to use a counter instead.. thanks again for all your input and help.
Al
 
Switch these 2 lines around
iCount=iCount +1
vcol = rst.fields(iCount).name

so they should be
vcol = rst.fields(iCount).name
iCount=iCount +1

You are incrementing iCount before processing the fields, so your code is checking for fields 1 and 2 instead of 0 and 1 (and field 2 will give this error)

Hope this helps,
Cathy
 
Hi 123ASP
just a thought. why are you using a varaible in the loop and sucking resources when not needed
just do this

for each fld in rst.Fields
response.Write rst.fields(iCount).name
iCount=iCount +1
next
that may have been a example only though and
just a optimization I thought I'd add

_________________________________________________________
for the best results to your questions: FAQ333-2924
[sub]01001111 01101110 01110000 01101110 01110100[/sub]
onpnt2.gif
[sup] [/sub]
 
onpnt and cathyw , Thanks for your input. It works.
Al
 
One quick change, why use a for-each loop with a counter?
Code:
For iCount = 0 to rst.fields.count - 1
   response.Write rst.fields(iCount).name 
Next

:)

-Tarwn

[sub]01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101 [/sub]
[sup]29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19[/sup]
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top