Solved

Looping through a datalist control

Posted on 2006-06-22
2
861 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

816 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