• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Subscript out of range: Error

Hi, I am retreiving values from a database to display them on a page as below

sql = "select genres, commision, bio, longbio, workinprogress, publication, showHidePrefs,otherworkshops from tblProfile where userid = " & Session("userID")
set rs = conn.execute(sql)

setting my array as below
if rs("showHidePrefs") <> "" then
      arrSH = Split(rs("showhideprefs"), ",")
else
      dim arrSH()
      redim arrSH(10)

      arrSH(0) = "0"
      arrSH(1) = "0"
      arrSH(2) = "0"
      arrSH(3) = "0"
      arrSH(4) = "0"
      arrSH(5) = "0"
      arrSH(6) = "0"
      arrSH(7) = "0"
end if

it is in the below part of the page where i get the error
r1 = ""
r2 = ""
if arrSH(5) = 1 then
 r1 = "checked"
else
 r2 = "checked"
end if


i am getting a "Subscript out of range: '[number: 5]" at the "ELSE" line
please do suggest how can I solve this error

thanks
0
Mona020200
Asked:
Mona020200
1 Solution
 
SquareHeadCommented:
I usually check for a recordset value like this (there are several ways to do this):

if not(IsNull(rs("showhideprefs")) or rs("showhideprefs") = "") then


Another thing to do may be to make sure you are getting records back in your recordset before you check the value of a specific field...

if not(rs.EOF and rs.BOF) then   'you have records being returned...
   'code goes here...
else
   response.write "No records returned!"
end if
0
 
clockwatcherCommented:

Your showHidePrefs field for that user doesn't contain at least 6 commas.  You could use redim preserve to set your array length to 10 no matter what:

if rs("showHidePrefs") <> "" then
     arrSH = Split(rs("showhideprefs"), ",")
     redim preserve arrSH(10)
else ...
0
 
kelvinwkwCommented:
try this


for i = 0 to ubound(arrSH)

if arrSH(i) = 1 then
 execute("r"&i+1&" = 'checked'")
else
 execute("r"&i+2&" = 'checked'")
end if

next
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now