Solved

Looping through a datalist control

Posted on 2006-06-22
2
879 Views
Last Modified: 2008-06-14
I am trying to loop through a datalist control (asp.net 2.0) and get the selected values.  Each time this code runs, I get this error:

"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

It highlights this line: int key = (int)this.dlstDemoList.DataKeys[item.ItemIndex];



Here's the code:


                foreach (DataListItem item in dlstDemoList.Items)
                {
                    int key = (int)this.dlstDemoList.DataKeys[item.ItemIndex];
                    // Do something with the value of key

                    // Select the sales rep assigned to this demo.
                    DropDownList ddl = item.FindControl("ddlSalesReps") as DropDownList;
                    string keyd = ((HtmlInputControl)item.FindControl("Sales_Rep_ID")).Value;
                    ddl.SelectedValue = keyd;
                }

This ran perfectly before I moved it to a different page.  It originally ran on ASP.NET 1.1, but now is 2.0 - so I wonder if the control is a little different or something.  Regardless, there are several values that are displayed in this list.

Thank you,

STank
0
Comment
Question by:stankstank
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
vinodhsomasekharan earned 500 total points
ID: 16966319
Hai

DataKeys() collection is populated only if u assign the DataKeyfield property while binding the grid

ie dlstDemoList.DataKeyField = "ID" .. before binding the grid..

but in u r code.. u r binding the grid in the page_load event..u r using the funtion

        dlstDemoList.DataSource = DataSet1
        dlstDemoList.DataBind()


but u have not set the DataKeyField value here..
so try the code dlstDemoList.DataKeyField = "ID"  in the bindgrid funtion before DataBind()

dlstDemoList.DataKeyField = "Give u r key field name"


hope this helps

Regards
Vinodh
0
 

Author Comment

by:stankstank
ID: 16968249
Ahh, that's it.  Makes perfect sense.  When I changed this code to another page, I didn't give it the datakey field.  Thanks a  million!  I would have still been looking for the answer. :)

Stank
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question