0

Where can I create this object in the asp.net lifecycle methods without receiving an out of range exception. Right now the only place that I can actually get a resource collection containing values is in onreasourcefetched method for webschedule info. But I need to do this before webscheduleinfo is created and it's views are populated with users.

protected void Page_Init(object sender, EventArgs e)
        {
            ResourcesCollection resources = WebScheduleInfo1.VisibleResources;

            int count = resources.Count;
            Resource obje = (Resource)resources.GetItem(1);
            string name = obje.Name;
            resources.Clear();
            resources.Add(obje);
            this.WebScheduleInfo1.ActiveResourceName = name;
        }
Dismissile
  • 32,564
  • 38
  • 174
  • 263
zms6445
  • 317
  • 6
  • 22
  • Are there items in the collection in the Page_Load? If not, please provide the relevant code for how you are binding the WebSchedule. – alhalama Feb 21 '13 at 19:37
  • This code will not work in the page load either (i have nothing there at the moment). Right now I'm using binding the webschedule using controls in the design view. You need to have atleast one webscheduleinfo control created so that you can access the resource collection. From there I wanted to to find out how many resources I have so that I can dynamically create the webdayview controls for each resource. WebscheduleInfo fetches resources after the page_load event so you cannot access the collection before that. I can't dynamically create my controls without knowing how many I need to create – zms6445 Feb 21 '13 at 21:45
  • What are you currently binding to in the design view to get this information? Is it a SqlDataSource using the SqlDataProvider? Is it an option for you to get the count of resources from the database directly or change the way that you get the data so that you have access to it possibly by using the GenericDataProvider? – alhalama Feb 22 '13 at 15:56

1 Answers1

2

You are getting a count of resources but you are not checking to make sure that count is greater than 0.

(Resource)resources.GetItem(1) will fail unless the resources collection has at least 2 items in it.

The collection is 0 based, so if you want the first item do something like this:

protected void Page_Init(object sender, EventArgs e)
{
    ResourcesCollection resources = WebScheduleInfo1.VisibleResources;

    int count = resources.Count;

    if( count > 0 )
    {
        Resource obje = (Resource)resources.GetItem(0);
        string name = obje.Name;
        resources.Clear();
        resources.Add(obje);
        this.WebScheduleInfo1.ActiveResourceName = name;
    }
}
Dismissile
  • 32,564
  • 38
  • 174
  • 263