Community discussion forum

Index out of Range

  • 1 year ago
    Plz solve my problem.Error like Index out of Range appear in following code. not always but some time t1.Text = Session("etime") pos = InStr(t1.Text, ":") h1.Value = t1.Text.Substring(0, pos - 1) h2.Value = t1.Text.Substring(pos, 2) Thanks & Regards Kausar
  • 1 year ago
    hi, You can do the debugging and easily findout the solution to your problem. As the index says "Index out of Range", it seems that the method substring is returning this error, coz you are looking for something which is not there in the string. So just do the debugging and I hope you can easily know where you need to make the changes. Regards, Royal
  • 1 year ago
    h2.Value = Right(t1.Text, t1.Text.Length - pos), because etime obviously returns not only 13:15 but also 14:2 instead 14:02 ... you can also solve it when you store Session time in variable of DateTime type - then you can remove all next parsing code, because DateTime variable has .Minutes or .Hours etc properties :)
  • 1 year ago
    Thanks
  • 1 year ago
    Plz solve my problem code is... singlecourse = Session("singlecourse") ename.Text = singlecourse.CourseName Session("testname") = singlecourse.CourseName Seclist = que.GetSection(singlecourse.CourseCode) Qnolist = que.GetQnumber(singlecourse.CourseCode) Session("totsec") = Seclist Session("totqno") = Qnolist Session("queslist") = queslist t1.Text = Profile.Ttime pos = InStr(t1.Text, ":") h1.Value = t1.Text.Substring(0, pos - 1) 'h2.Value = t1.Text.Substring(pos, 2) h2.Value = Right(t1.Text, t1.Text.Length - pos) End If Else ViewState("qno") = CType(Session("summary"), Integer) Session("summary") = "" summbtn.Visible = True queslist = Session("queslist") ename.Text = Session("testname") t1.Text = Session("etime") pos = InStr(t1.Text, ":") h1.Value = t1.Text.Substring(0, pos - 1) h2.Value = t1.Text.Substring(pos, 2) End If queserial.Text = "Question : " + CType(queslist.Item(ViewState("qno")).QNo, String) & " of " & queslist.Count (Error : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index) aquestion.Text = queslist.Item(ViewState("qno")).Question Regards Kausar
  • 9 months ago
    :) try to use code button for posting such code.. it puts 4 special characters into message box, you paste code between two and two of them, and code is fine readable :) now I am not very able to help you :) best wishes
  • 9 months ago
    t1.Text = Session("etime") pos = InStr(t1.Text, ":") h1.Value = t1.Text.Substring(0, pos - 1) <-- error prone You need to verify that pos <> -1 for example t1.Text = Session("etime") pos = InStr(t1.Text, ":") if pos = -1 then throw new exception(session("etime") & " does not contain the character ':'") h1.Value = t1.Text.Substring(0, pos - 1) Also this may be an easier that using substring: Dim values() as string= split(session("etime"), ":") if values.length>1 then h1.value=values(0) h2.value=values(1) else 'somehting bad happened we didn't find : in the etime variable throw new exception(session("etime") & " does not contain the character ':'") end if
  • 9 months ago
    the code button is buggy :/ t1.Text = Session("etime") pos = InStr(t1.Text, ":") h1.Value = t1.Text.Substring(0, pos - 1) <-- error prone check the pos before using the substring function t1.Text = Session("etime") pos = InStr(t1.Text, ":") if pos = -1 then throw new exception(session("etime") & " does not contain the character ':'") else h1.Value = t1.Text.Substring(0, pos - 1) end if

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!