Click here to Skip to main content
15,747,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HttpUsrName = (HttpContext.Current.User.Identity.Name.ToString().Split('\\')[1]);

splitting of hardcoded value (Domain\\UserName)works
but above code line giving error
"System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. Parameter name: startIndex"

I have gone through google for several solution but still not get any solution.
Any solution from anyone will be appreciate.

You assume there will be two elements in the string array returned by Split function, and you take the second element (index 1).

But, if the array has zero or one element only, you get an ArgumentOutOfRangeException, telling you that you tried to access the second element of an array that holds less than two elements.

Solution: validate that the array you are trying to access is actually of the length you think it is.
Something like:
string[] parts = HttpContext.Current.User.Identity.Name.ToString().Split('\\');
if (parts.Length == 2) {
   HttpUsrName = parts[1];
else {
   HttpUsrName = "Problem in username parsing from http context";
Share this answer
There's something "fishy" about this:
The error message, as reported, indicates the name of the erroneous parameter is startIndex and that it is an issue with it being "larger than the length of string".
These rule out the issue of the error being the [1] indexing.
.Split() doesn't have a startIndex parameter.

So either there's a different string method being called (.Substring()?) that isn't shown,
one of the properties in the HttpContext.Current.User.Identity.Name chain calls such a method, under the covers!!

(Or this isn't the code that is causing the exception!)
Share this answer
Obviously the '[1]' is out of range because the length of the string returned by HttpContext.Current.User.Identity.Name.ToString() must be 0 ie blank.
Share this answer
HttpUsrName = (HttpContext.Current.User.Identity.Name.ToString().Split('\\')[1]);

I found very funny reason behind this issue. It is not due to indexing of element.
After splitting the string, I was getting proper value in HttpUsrName variable.
After getting value in HttpUsrName value I was passed this value to one function
which was having a System.Windows.Forms reference in it's dll and that dll added in reference of Web application.

It is the reason to get following error "System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. Parameter name: startIndex".

Still thanks to all of you for providing your valuable comments on issue.
Share this answer
check the value of this "HttpContext.Current.User.Identity.Name.ToString()", check whether "\\" tags are there or not. If the array of string length is 2 then only it won't give any error in your case.
Share this answer
Naveen.Sanagasetti 7-Oct-15 1:54am    
May I know who give downvote for this, on what basis.? Please provide your comment for downvote so that I try to avoid those mistakes don't downvote unnecessarily.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900