Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ALL,

Here I got the Exception:--
Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index exception

My Code IS:

I got the
caseStatusHistoryInfoList.Count value is 0,

C#
if (caseStatusHistoryInfoList.Count <= 1 && caseStatusHistoryInfoList[0].NewStatusDescription == "New Case")
{
                        dtlstatusHist.Visible = false;
                        imgStatusHistory.Visible = false;
}

Plz give me solution..
Posted
Updated 30-Nov-11 22:44pm
v2
Comments
uspatel 1-Dec-11 4:44am    
pre tag added

You are trying to access the first element in caseStatusHistoryInfoList i.e. caseStatusHistoryInfoList[0] while the count is actually 0. Thus you get an error in the line.

Try checking for caseStatusHistoryInfoList.Count = 1 instead of caseStatusHistoryInfoList.Count <= 1 .
 
Share this answer
 
Place a brake point on the "if" condition and check the size of
caseStatusHistoryInfoList

variable. You should check if this variable contains at least one element in it before accessing its index.
 
Share this answer
 
Use your debugger, have a look at the collection

Is caseStatusHistoryInfoList collection properly populated. Is there definitely an item in the collection at index 0

caseStatusHistoryInfoList[0] == "New Case"


That line will fail and throw the exception you are seeing if your collection count is zero. Your test for caseStatusHistoryInfoList.Count <= 1 is a bit strange really.
 
Share this answer
 
try:
C#
if (caseStatusHistoryInfoList.Count == 1 && caseStatusHistoryInfoList[0].NewStatusDescription == "New Case")
{
                        dtlstatusHist.Visible = false;
                        imgStatusHistory.Visible = false;
}



or
 
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