Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please find my code below:

If dtApp.Rows.Count <> 0 Then


If dtApp.Rows(0).Item(0).ToString = Nothing Then
ddlChangeStatus.SelectedValue = 0
Else
ddlChangeStatus.SelectedValue = dtApp.Rows(0).Item(0).ToString
Session("ChangeStatus") = ddlChangeStatus.SelectedItem.Text
End If

If ddlChangeStatus.SelectedItem.Text = "Change In Progress" Then

ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("Awaiting Change Management Approval")))
ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("Awaiting M&S CAB Approval")))
ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("Awaiting Ceridian CAB Approval")))
ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("Awaiting Ceridian Email CAB Approval")))
ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("Authorised & Awaiting Implementation")))
ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("Cancelled or Rejected")))
ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("Completed")))
ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("Awaiting Environment Approval")))
ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("On Hold/Pending")))
ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("Change Expired–On Hold")))
ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("Change Expired-Cancelled")))


End If

What I have tried:

Can someone please advise what is wrong with the index?
Posted
Updated 19-May-16 1:00am
Comments
jimmson 19-May-16 6:50am    
Hi, what is wrong with the index - explanation is in the title of your question. Did you tried to debug your code? Without debug, you hardly solve this problem..
F-ES Sitecore 19-May-16 7:07am    
When you write code like

If dtApp.Rows(0).Item(0).ToString = Nothing Then

if dtApp.Rows has no elements, ie it is empty, of if .Item is empty then you'll get that error as you are trying to get the first element of an empty array. Debug your code to find out exactly what array is empty and either work out why it is empty when it shouldn't be (if that's the case) or else check that the array has at least one item in it before accessing it.

1 solution

Hello Jimmson,

When I debug, the code crashes after this line:

ddlChangeStatus.Items.RemoveAt(ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("On Hold/Pending")))
 
Share this answer
 
Comments
jimmson 19-May-16 7:08am    
Hello, please don't post a your reply as a solution, use comments or "Improve your question" button. Anyway, your code is not very clear, you doing a lot of stuff in one line of code.. I assume exception is thrown from RemoveAt method - put everything inside into separate variable and check that index is in the range. Something like this:
var index = ddlChangeStatus.Items.IndexOf(ddlChangeStatus.Items.FindByText("On Hold/Pending"));
if(index >= 0 && index < ddlChangeStatus.Items.Count)
{
ddlChangeStatus.Items.RemoveAt(index);
}
jimmson 19-May-16 7:15am    
I'm sorry, I didn't notice you use VB, not C#. But the idea is still same. Put index into separate variable and then check that this index is in the range of the collection you working with -> equal/bigger than zero AND smaller than the count of items in the collection.

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