im useless
im useless

Reputation: 7451

Index outside the bounds of the array

During local my system its working but when i uploaded it on-site i encountered problem.. Do i need some dll?

Index was outside the bounds of the array.
Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Line 6:      if (Request.Params["mode"] != null) Mode = Request.Params["mode"];
Line 7:      if (!Path.Split('/')[3].Equals("Default.aspx") && (String)Session["accesslevel"] == ("0"))

Upvotes: 0

Views: 1870

Answers (5)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

The problem is in this condition; if (!Path.Split('/')[3].Equals("Default.aspx") This might be the reason the server path will not be like it is on your localhost.

Replace this if (!Path.Split('/')[3].Equals("Default.aspx") with

if (!Path.Split('/')[2].Equals("Default.aspx")

Upvotes: 2

rsbarro
rsbarro

Reputation: 27339

If I had to guess I'd say it's probably this:

Path.Split('/')[3]

If you're running under http://localhost/myapp you'll have more elements in the array after calling Path.Split than if you are running under http://www.myapp.com. Chances are you only have 3 elements in that array in production, not the 4 you probably have in dev.

EDIT:

For the page you posted, a call to Request.Path is going to return:

"/backend/default.aspx"

When you do a split on '/', you're only going to get back 3 elements:

[0] = ""
[1] = "backend"
[2] = "default.aspx"

That's why Path.Split('/')[3] will throw an IndexOutOfRangeException. So the short answer is you should switch it to Path.Split('/')[2] in production, but a better solution would be to come up with a way where the case is handled using the same code in both environments.

Upvotes: 3

Tushar
Tushar

Reputation: 1262

Instead of

if (!Path.Split('/')[3].Equals("Default.aspx") && (String)Session["accesslevel"] == ("0"))

use this:

Path.SubString(Path.LastIndexOf('/')).Equals("Default.aspx") .....

Upvotes: 1

Richard Schneider
Richard Schneider

Reputation: 35477

The Path.Split seems to be the problem. Why not use the Uri class to parse the url.

Upvotes: 1

x2.
x2.

Reputation: 9668

I think problem is here Path.Split('/')[3], length of array is smaller than 4.

Upvotes: 2

Related Questions