-7

我的代码的第一部分在数组列表中添加项目。

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

*CODE/FLOW 我首先想知道程序下有哪些事件。我单击搜索按钮,然后从数据库中读取并将其结果放入数组中。

 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
                }


            }
4

1 回答 1

1

您的语句失败的原因是您试图访问超出集合范围的元素。迭代时programEvents,您将索引分配给evectr0programEvents.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];
于 2012-06-15T05:49:54.063 回答