Solved

Looping through a datalist control

Posted on 2006-06-22
2
839 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
2 Comments
 
LVL 5

Accepted Solution

by:
vinodhsomasekharan earned 500 total points
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now