当我执行代码时,我得到一个System.IndexOutOfRangeException

时间:2014-01-09 19:53:51

标签: c# asp.net

当我执行代码时,我在frmtime中获得异常是

  

System.IndexOutOfRangeException:索引超出了范围   阵列

public IList<ExamSeatAllotmentEntity> GetAttendence1(string frmtime, string totime, DateTime date, int RoomNo)
{
    List<ExamSeatAllotmentEntity> ObjTestList = new List<ExamSeatAllotmentEntity>();


    //List<QuestionTypeEntity> ObjQTList = new List<QuestionTypeEntity>();
    SqlParameter[] Parms = new SqlParameter[1];
    Parms[0] = new SqlParameter("@RoomNo", SqlDbType.Int);
    Parms[0].Value = RoomNo;
    Parms[1] = new SqlParameter("@FromTime", SqlDbType.Time);
    Parms[1].Value = frmtime;
    Parms[2] = new SqlParameter("@Totime", SqlDbType.Time);
    Parms[2].Value = totime;
    Parms[3] = new SqlParameter("@Date", SqlDbType.DateTime);
    Parms[3].Value = date;


    DataSet dsTest = SqlHelper.ExecuteDataset(SqlHelper.connString, CommandType.StoredProcedure, SQL_PROC_GET_Attendence1, Parms);
    if (dsTest != null && dsTest.Tables.Count == 1)
    {
        ObjTestList = (from test in dsTest.Tables[0].AsEnumerable()
                       select new ExamSeatAllotmentEntity()
                       {
                           // CourseID = test.Field<int>("CourseID"),
                           StudentId = test.Field<int>("StudentId"),

                           Fname = test.Field<string>("Fname"),
                           SeatNo = test.Field<string>("SeatNo"),
                           Attendence = test.Field<string>("Attendence")
                       }).ToList<ExamSeatAllotmentEntity>();
    }
    return ObjTestList;
}

3 个答案:

答案 0 :(得分:4)

new SqlParameter[1]

你刚刚创建了一个只能容纳一个项目的数组。

答案 1 :(得分:1)

这就是问题所在:

SqlParameter[] Parms = new SqlParameter[1];

您指定的长度为1个元素:Parms[0]

更正数组的大小以允许4个元素:

SqlParameter[] Parms = new SqlParameter[4];

如果您想添加更多参数,可能需要创建一个列表,然后通过ToArray()进行转换。

答案 2 :(得分:1)

更改

SqlParameter[] Parms = new SqlParameter[1];

SqlParameter[] Parms = new SqlParameter[4];