Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi am using dynamic in my page .while updating record am getting error like
"
startIndex cannot be less than zero
parameter name :startIndex
"
in this line
fieldNames.Remove(fieldNames.Length - 1, 1);

can any one tel me wat type of error and how to solve this..
thanks
Posted
Updated 30-Apr-12 23:44pm
v4
Comments
bhagirathimfs 1-May-12 5:10am    
what is fieldNames here?
ythisbug 1-May-12 5:11am    
StringBuilder fieldNames = new StringBuilder();

hi ,

your code will not work because fieldNames variable length equal to zero.

Try below code

if(fieldNames.Length > 0)
{
fieldNames.Remove(fieldNames.Length - 1, 1);
}


hop it will help!!
 
Share this answer
 
Comments
ythisbug 1-May-12 6:20am    
thanks dude..
startIndex cannot be less than zero
fieldNames.Remove(fieldNames.Length - 1, 1);

These simply means that your fieldNames.Length was zero at some instance and hence an error.

See the counts in the fieldNames. Use VS DEBUGGER and check. It looks like empty, hence zero count/length and so when you tried to remove from (0-1 = -1) index, error occurred.
 
Share this answer
 
Comments
ythisbug 1-May-12 5:32am    
so now where i have to modify?
ythisbug 1-May-12 5:50am    
m getting fieldNames.Lenth=0 value
Sandeep Mewara 1-May-12 8:23am    
Wrap it in a if else. Check for length >0 before doing it.
Obviously the length of fieldNames is zero. Thus the Remove command throws an error.
 
Share this answer
 
Comments
ythisbug 1-May-12 5:32am    
so now where i have to modify?
If stringBulider length is 0
C#
fieldNames.Remove(fieldNames.Length - 1, 1);

the above code will not work.

So before run the above code please check whether the stringbuilder length is not equal t zero.

This error is coming because your are assigning the start index less than zero.
The start index is always greater than zero.
 
Share this answer
 
Comments
ythisbug 1-May-12 5:32am    
so now where i have to modify?
ythisbug 1-May-12 5:50am    
am getting length 0
bhagirathimfs 1-May-12 5:52am    
first insert into the stringbuilder than remove
or put a if statement before execute above code.
hi..
i suppose you have got the reason for your problem, here is one way out for this
you can put if condition to check that the index i.e. fieldNames.Length - 1
should not be negative.
or you can also use try catch block for this..
thanks..
 
Share this answer
 
Comments
bhagirathimfs 1-May-12 6:19am    
In the above query you are getting the above error?

Because when the objFieldProp.FieldType is not match with any of the cases than the stringbuilder is remain empty (means length of the stringbuilder is zero).At that time,above error will show .

so before the remove statement write if clause like

if(fieldNames.Length != 0)
{
fieldNames.Remove(fieldNames.Length - 1,1);
fieldValues.Remove(fieldValues.Length - 1, 1);
}

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