Solved

Looping through a datalist control

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 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

20 Experts available now in Live!

Get 1:1 Help Now