I've searched several questions like this but could not find a good answer to my problem.
I have 2 listboxes, one contains a list of check numbers and the other contains a list of payees. The code was working fine and I did some error checking enhancements to prevent user errors and now I am getting this message
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
In debug I see that the Payee list box has 41 items - which is correct, so when the selection changes on the Check list in this event I have to select that particular check payee. It is the second one in the Payee List and my index has a value of 1 and this error shows up.
I've been hitting my brains on this error for the last 2 hours... here is the "offending" code.
(Inside the event handler) ...
queryReadType.Connection = conn;
conn.Open();
queryReadType.Parameters.Add("@fld1", SqlDbType.VarChar, 50).Value = checkNo;
SqlDataReader reader = queryReadType.ExecuteReader();
try
{
int i, zid;
while (reader.Read())
{
int.TryParse(reader["id"].ToString(), out zid);
txtCheckNo.Text = reader["checkno"].ToString();
if (option == 60) // Update
originalCheckNo = txtCheckNo.Text;
int payeeId;
int.TryParse(reader["payeeid"].ToString(), out payeeId);
txtPayeeId.Text = payeeId.ToString();
string payee = reader["payee"].ToString();
for (i = 0; i < payeecnt; i++)
{
if (String.Compare(payee, lstPayee.Items[i].ToString()) == 0)
{
lstPayee.ClearSelected();
//int j = lstPayee.Items.Count; <---- shows 41
lstPayee.SetSelected(i, true); <--- the value of i is 1 which //is the correct payee for this check
break;
}
}
DateTime dtissued = DateTime.MinValue;
//DateTime dtcleared = DateTime.MinValue;
colIndex = reader.GetOrdinal("dateissued");
if (!reader.IsDBNull(colIndex))
dtissued = reader.GetDateTime(colIndex);
.....
I'd appreciate someone shedding some light on what could be wrong - and it was working perfectly - again this code in inside the selection item change for the check listbox, and please only positive comments I have enough baggage in my work day already... I've notice that some members post comments that don't really address the issue, if this has been answered already please post the link.
Thanks in advance