C# ASP arraylist gets cleared

本文关键字:cleared gets arraylist ASP | 更新日期: 2023-09-27 18:37:11

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

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

*代码/流程我首先想知道哪些事件属于一个程序。我单击搜索按钮,然后从数据库中读取并将其结果放入数组中。

 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
                }

            }

C# ASP arraylist gets cleared

语句失败的原因是您尝试访问超出集合边界的元素。循环访问 programEvents 时,您将索引分配给从 0programEvents.Count含)evectr。但是,由于索引是从零开始的,因此最后一个元素的索引实际上是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];