Click here to Skip to main content
15,908,274 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i got "StartIndex cannot be less than zero.
Parameter name: startIndex" 
After save changes in database it throws an error on this line.
C#
string pHOF = a.HOF.ToString().Substring(Convert.ToString(a.HOF).Length - 3);

Please Help me its very urgent.
Thank you.

What I have tried:

I'm new in MVC and don't know how to debug.
Posted
Updated 23-Feb-18 21:46pm
v2

@OriginalGriff is right, the Substring method do that one parameter. That case it will treat the parameter as start index. Try debug the code to see what in the HOF. The length of HOF must be 3 or more according to what posted here.

Example below will output: cde because 5 - 3 = 2, it will start to read from position 2 (0,1,2,3,4) which is c.
C#
object HOF = "abcde";
		
string pHOF = HOF.ToString().Substring(Convert.ToString(HOF).Length - 3);


Example below will output: StartIndex cannot be less than zero. Parameter name: startIndex because 2 - 3 = -1, the startIndex must be 0 or more.
C#
object HOF = "ab";
		
string pHOF = HOF.ToString().Substring(Convert.ToString(HOF).Length - 3);

As the error mentioned, the substring method is missing the start index. It should be substring(startinxex, endindex). In your scenario, it would be something like .Substring(0, a.HOF.Length -3)

c - Google Search[^]
 
Share this answer
 
v3
Comments
OriginalGriff 24-Feb-18 3:46am    
Um. string.Substring has two overloads: one of which takes two parameters, but one which just requires the start index.
Bryian Tan 24-Feb-18 12:00pm    
you are right.
Huzaifa Abbas 24-Feb-18 3:55am    
string pHOF = a.HOF.ToString().Substring(0, Convert.ToString(a.HOF).Length - 3);

Now Return Length cannot be less than zero.
Parameter name: length

Sorry Sir
Check your original input: if the length of <pre>Convert.ToString(a.HOF)</pre> is two or less, then the start index for the substring will be negative and you will get this error.

Use the debugger, and check exactly what is in a.HOF before you execute that code.
 
Share this answer
 

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