Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends.
I am using a datalist in my application and storing Id in the datakey field.
When iam retrieving the value in datakey it gives an erro of index out of range error.
Must be non negative.
Here is the code

C#
var button = sender as Button;
var dataListItem = button.NamingContainer as DataListItem;
int id = Convert.ToInt32(dlCategories.DataKeys[dataListItem.ItemIndex]);



where dlcategories is the Datalist Id.

please help me guys to get out of this.
Posted

1 solution

ItemIndex is returning (probably) -1 which means nothing selected or similar.
Without knowing what event you are handling and when, we can say no more.
You can try checking it at the start of the handler, and do nothing if it is below zero...
 
Share this answer
 
Comments
RashdSiddique 2-May-12 3:17am    
yes it is returning -1.
I am using button click event
OriginalGriff 2-May-12 3:27am    
Then it is saying that it is not in a Items collection.
RashdSiddique 2-May-12 3:30am    
so how can i accomplish the task
OriginalGriff 2-May-12 4:11am    
I don't know - I don't have anywhere near enough information.
You will have to look at your whole system, and work out why it isn't in a collection that you think it should be!
Sorry, but I can't read minds, or HDDs :laugh:
RashdSiddique 2-May-12 5:24am    
Thanks buddy for the time.
I have tried a different option and succeeded in that.
Tc

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