C#ASP arraylist被清除

时间:2012-06-15 05:31:18

标签: c# asp.net arraylist

我的代码的第一部分在arraylist中添加了项目。

当我稍后尝试使用for循环访问其内容时,arraylist被清除并且不返回任何值。

* CODE / FLOW 我首先想知道哪些事件属于一个程序。 我点击SEARCH按钮然后从数据库中读取并将其结果放入数组中。

 while (dr.Read())
        {
            string eventID = Convert.ToString(dr["Event ID"]);
            programEvents.Add(eventID);
        }

当事件已经显示时,我输入人名以在事件

下注册它们
for (int ctr = 0; ctr < spPeoplePicker.ResolvedEntities.Count; ctr++)
            {
                PickerEntity selectedEntity = (PickerEntity)spPeoplePicker.ResolvedEntities[ctr];
                //get the email of the person added
                //use this email as search tool for Peoplesoft database
                string entityEmail = Convert.ToString(selectedEntity.EntityData["Email"]);
                //string email = Convert.ToString(selectedEntity.EntityData["Title"]);
                //line below can get display name of entity
                PickerEntity pe = (PickerEntity)spPeoplePicker.ResolvedEntities[ctr];

                for (int evectr = 0; evectr <= programEvents.Count; evectr++)
                {

这就是失败的地方。

                    paramID = programEvents[evectr];

                    #region getEventDetails
                    ProgramManagement.Conn connect = new ProgramManagement.Conn();
                    SqlConnection conn = new SqlConnection(@connect.getConnString());
                    conn.Open();
                    SqlCommand stmnt = conn.CreateCommand();
                    stmnt.CommandText = "SELECT * FROM [TrainingAndScheduling].[dbo].[trainersView]" +
                        " WHERE EventID='" + paramID + "'"; ;
                    SqlDataReader thisReader = stmnt.ExecuteReader();
                    if (thisReader.HasRows)
                    {
                        thisReader.Read();                            
                        eveName = Convert.ToString(thisReader["CourseName"]);
                        eveStart = Convert.ToString(thisReader["StartDate"]);
                        eveEnd = Convert.ToString(thisReader["EndDate"]);
                        eveVenue = Convert.ToString(thisReader["RoomID"]);
                    }
                    else
                    { }
                    thisReader.Close();
                    conn.Close();
                    #endregion

                    #region registration for a single event
                    ProgramManagement.Registration register = new ProgramManagement.Registration();
                    if (!register.register(entityEmail, paramID))
                    {
                        //this block is executed if user is not registered
                        errorAdding = errorAdding + "<br/>" + "Engineer: " +
                            pe.DisplayText + " was not registered.";
                    }
                    else
                    {
                        ProgramManagement.Email sendemail = new ProgramManagement.Email();
                        sendemail.sendNotification(entityEmail, pe.DisplayText, eveName, eveStart, eveEnd, eveVenue, paramID);
                    }
                    #endregion
                }


            }

1 个答案:

答案 0 :(得分:1)

您的语句失败的原因是您尝试访问集合边界之外的元素。在遍历programEvents时,您要将索引分配到evectr,范围从0programEvents.Count 包含。但是,由于索引是从零开始的,因此最后一个元素的索引实际上是programEvents.Count - 1;访问programEvents[programEvents.Count]会抛出IndexOutOfRangeException

您需要替换:

for (int evectr = 0; evectr <= programEvents.Count; evectr++)
{
    paramID = programEvents[evectr];

...与:

for (int evectr = 0; evectr < programEvents.Count; evectr++)
{
    paramID = programEvents[evectr];