数组索引超出范围的异常 [英] indexing Array out of Range Exception

查看:219
本文介绍了数组索引超出范围的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图挽救一个数组中的SQL结果,然后返回。但我发现了异常:数组超出范围的错误。

I try to save the SQL Results in a array and return it back. but i'm getting an exception: array out of range error.

这是我的code:

 public BookingUpdate[] getBookingUpdates(string token)
{
    String command = "SELECT b.ID,b.VERANSTALTER, rr.VON ,rr.BIS, b.THEMA, b.STORNO, ra.BEZEICHNUNG from BUCHUNG b JOIN RESERVIERUNGRAUM rr on rr.BUCHUNG_ID = b.ID JOIN RAUM ra on ra.ID = rr.RAUM_ID WHERE b.UPDATE_DATE BETWEEN DATEADD (DAY , -20 , getdate()) AND getdate() AND b.BOOKVERNR = 0";
    SqlConnection connection = new SqlConnection(GetConnectionString());
    BookingUpdate[] bookingupdate = new BookingUpdate[1];
    connection.Open();
    try
    {
        SqlCommand cmd = new SqlCommand(command, connection);
        SqlDataReader rdr = null;
        int count = 0;
        rdr = cmd.ExecuteReader();


            while (rdr.Read())
            {
                DataTable dt = new DataTable();
                dt.Load(rdr);
                count = dt.Rows.Count;
                for (int c = 0; c < count; c++)
                {
                    bookingupdate = new BookingUpdate[c];
                    bookingupdate[c].bookingID = (long)rdr["ID"]; // <---- Error is here
                    bookingupdate[c].fullUserName = rdr["VERANSTALTER"].ToString();
                    bookingupdate[c].newStart = (DateTime)rdr["VON"];
                    bookingupdate[c].newStart = (DateTime)rdr["BIS"];
                    bookingupdate[c].newSubject = rdr["THEMA"].ToString();
                    bookingupdate[c].newlocation = rdr["BEZEICHNUNG"].ToString();
                    if (rdr["STORNO"].ToString() != null)
                    {
                        bookingupdate[c].deleted = true;
                    }
                    else
                    {
                        bookingupdate[c].deleted = false;
                    }

                }

            }

    }

    catch (Exception ex)
    {
        log.Error(ex.Message + "\n\rStackTrace:\n\r" + ex.StackTrace);
    }
    finally
    {
        connection.Close();
    }
    return bookingupdate;
}

我想什么?

推荐答案

您似乎创建和使用

bookingupdate = new BookingUpdate[c];

但实际上没有创造BookingUpdate的实例。当您尝试阵列元素上设置的属性没有实际BookingUpdate更新 - 只有一个持有人

but not actually creating instances of BookingUpdate. When you attempt to set properties on your array element there is no actual BookingUpdate to update - there is only a holder for one.

我建议改变你的code沿着线的东西:

I'd suggest changing your code to something along the lines of:

...
bookingupdate = new BookingUpdate[count];  // allocates space for the number of BookingUpdates to be created
for (int c = 0; c < count; c++)
{
    bookingupdate[c] = new BookingUpdate(); // create a new instance of BookingUpdate and assign it the array     
    bookingupdate[c].bookingID = (long)rdr["ID"];
    ...

我希望这有助于!

I hope that this helps!

这篇关于数组索引超出范围的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆