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"), ",")
      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"
 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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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...
   response.write "No records returned!"
end if

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 ...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
try this

for i = 0 to ubound(arrSH)

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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.