Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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
 
Share this answer
 
Comments
Sandeep Mewara 3-Jun-10 13:11pm    
Reason for my vote of 1
Whats different from other responses here?
I'm guessing that strval - 31 is less than zero.
 
Share this answer
 
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?
 
Share this answer
 
v2
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.
 
Share this answer
 
v3

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