Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_discuss thread: Re: Array Problem in Assigning Values


Message #1 by "Matt Duncan-Jones" <matt@t...> on Wed, 20 Nov 2002 12:59:49
> <% 
d> im path,conn,rs,mySQL
p> ath="provider=microsoft.Jet.OLEDB.4.0;Data Source=" &Server.Mappath 
(> "..\db\transaction.mdb")
s> et conn=server.createobject("adodb.connection")
s> et rs = Server.CreateObject("ADODB.Recordset")
'> conn.open session("connection_string")
c> onn.open path
m> ySQL="select * from customers order by CustomerID"
s> et rs = conn.execute(mySQL)
%> >

> <%
d> im arr()
d> im i
r> ecCount=rs.RecordCount
r> edim preserve arr(recCount)
d> o while not rs.eof
a> rr(i)=rs("CustomerID")
r> esponse.wrire arr(i)
i> =i+1
L> oop
%> >

> The Above Code Gave Subscript out of range: '' ERROR
I>  couldn't figure out what's wrong with the code.
I> s this assigning  mehtod wrong?
T> hen,how can i assign the each recorset value to the array usin loop?
P> lease Help!
D> ibyendra




I think it's probably when you are declaring the array

you haven't set the size of the array 

as you have just dim arr() the number of records your trying to put in 
exceeds the size of the array

so just put a value in the brackets that you know will be more than the 
ammount of records returned


dim arr(400)
Message #2 by Imar@S... on Wed, 20 Nov 2002 17:19:12
Hmmmm, I thought the tagline of this forum was: No coding 
questions....... ;-)

Anyway, the problem you face here is caused by the Execute method of the 
Connection object. This method ALWAYS returns a forward only read only 
recordset. These kind of recordsets don't support the RecordCount 
property and return -1 instead. So effectively, you are redimming your 
array to -1 which obviously is not going to work.

To get this to work, create a recordset that supports the RecordCount 
property by manually creating one and the use its Open method.

Alternatively (and even better), you can use the GetRows and GetString 
methods to get the contents of the Recordset in an array.

For more info on the Execute method, check:

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/ado270/htm/mdmthcnnexecute.asp?frame=true

HtH

Imar




> <% 
d> im path,conn,rs,mySQL
p> ath="provider=microsoft.Jet.OLEDB.4.0;Data Source=" &Server.Mappath 
(> "..\db\transaction.mdb")
s> et conn=server.createobject("adodb.connection")
s> et rs = Server.CreateObject("ADODB.Recordset")
'> conn.open session("connection_string")
c> onn.open path
m> ySQL="select * from customers order by CustomerID"
s> et rs = conn.execute(mySQL)
%> >

> <%
d> im arr()
d> im i
r> ecCount=rs.RecordCount
r> edim preserve arr(recCount)
d> o while not rs.eof
a> rr(i)=rs("CustomerID")
r> esponse.wrire arr(i)
i> =i+1
L> oop
%> >

> The Above Code Gave Subscript out of range: '' ERROR
I>  couldn't figure out what's wrong with the code.
I> s this assigning  mehtod wrong?
T> hen,how can i assign the each recorset value to the array usin loop?
P> lease Help!
D> ibyendra
Message #3 by Imar@S... on Wed, 20 Nov 2002 17:23:25
One more thing: You'll need to add a rs.MoveNext inside your loop code, 
otherwise it will loop forever.

Imar


> Hmmmm, I thought the tagline of this forum was: No coding 
q> uestions....... ;-)

> Anyway, the problem you face here is caused by the Execute method of 
the 
C> onnection object. This method ALWAYS returns a forward only read only 
r> ecordset. These kind of recordsets don't support the RecordCount 
p> roperty and return -1 instead. So effectively, you are redimming your 
a> rray to -1 which obviously is not going to work.

> To get this to work, create a recordset that supports the RecordCount 
p> roperty by manually creating one and the use its Open method.

> Alternatively (and even better), you can use the GetRows and GetString 
m> ethods to get the contents of the Recordset in an array.

> For more info on the Execute method, check:

> http://msdn.microsoft.com/library/default.asp?url=/library/en-
u> s/ado270/htm/mdmthcnnexecute.asp?frame=true

> HtH

> Imar

> 

> 
>>  <% 
d> > im path,conn,rs,mySQL
p> > ath="provider=microsoft.Jet.OLEDB.4.0;Data Source=" &Server.Mappath 
(> > "..\db\transaction.mdb")
s> > et conn=server.createobject("adodb.connection")
s> > et rs = Server.CreateObject("ADODB.Recordset")
'> > conn.open session("connection_string")
c> > onn.open path
m> > ySQL="select * from customers order by CustomerID"
s> > et rs = conn.execute(mySQL)
%> > >

> > <%
d> > im arr()
d> > im i
r> > ecCount=rs.RecordCount
r> > edim preserve arr(recCount)
d> > o while not rs.eof
a> > rr(i)=rs("CustomerID")
r> > esponse.wrire arr(i)
i> > =i+1
L> > oop
%> > >

> > The Above Code Gave Subscript out of range: '' ERROR
I> >  couldn't figure out what's wrong with the code.
I> > s this assigning  mehtod wrong?
T> > hen,how can i assign the each recorset value to the array usin loop?
P> > lease Help!
D> > ibyendra

  Return to Index