Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Looping through a datalist control

Posted on 2006-06-22
2
Medium Priority
?
883 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 2000 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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

715 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