Click here to Skip to main content
14,022,423 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
protected void btn_Logout_Click1(object sender, EventArgs e)
    {
        string tempval = urlval;
        int strval = tempval.Length;
        string logoutstring = tempval.Substring(0, strval - 31);
        Response.Redirect(logoutstring);

    }

getting error in the above code....someone please help me.....
Length cannot be less than zero. Parameter name: length
Posted
Updated 3-Jun-10 3:08am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The substring functions work by starting at an index and then returning the next X characters.

So, Substring(0, strval-31) is telling it to start at index 0 and travel strval-31 places. strval-31 is the length that it will travel so when you minus 31 from strval it is less than 0 which is not valid.
   
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

well, the problem is that strval is probably less than 31. A quick fix would be to do this:

string logoutstring = tempval.Substring(0, Math.Max(0, strval - 31));


That way, the ssecond parameter will always be at least 0.

EDIT ---------

I don't get it. I actually provided code to fix the problem, and my response is voted lower?
   
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I'm guessing that strval - 31 is less than zero.
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

check strval length as
if(tempval.Length=>31)
{
string logoutstring = tempval.Substring(0, strval - 31);
}
else
{

// write your code whatever you want
}


hope it will help you
   
Comments
Sandeep Mewara 3-Jun-10 13:11pm
   
Reason for my vote of 1
Whats different from other responses here?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190417.4 | Last Updated 3 Jun 2010
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100