简体   繁体   English

错误:索引超出范围

[英]Error : Index out of bound

protected void btnDownload_Click(object sender, EventArgs e)
    {

        //to request the name of the event from the listbox from Main.aspx
        string EventName = Request.QueryString["ename"];

        //Select event id statement
        //const string S = "SELECT EventName FROM Event WHERE EventID = @EventID";
        const string q = "SELECT EventID from Event WHERE EventName = @EventName"; 
        string eventid = "";
        using (SqlConnection c = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true"))
        using (SqlCommand Command = new SqlCommand(q, c))
        {
            Command.Parameters.AddWithValue("@EventName", EventName);

            c.Open();
            using (SqlDataReader rdr = Command.ExecuteReader())
                while (rdr.Read())
                {
                    Command.CommandText = "Select * from Attendance where EventID=@EventID";
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append(String.Format("\"{0}\",\"{1}\", \"{2}\", \"{3}\", \"{4}\", \"{5}\", \"{6}\", \"{7}\"n",
               rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6], rdr[7]));
               // I have an error here(Index out of bound)

                    // to get event id from the Event name
                    eventid = rdr.GetString(0);
                    rdr.Close();
                    c.Close();

                    byte[] ar = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.AddHeader("Content.Type", "application/octet-stream");
                    Response.AddHeader("Content-Length", ar.Length.ToString());
                    Response.AddHeader("Content-Disposition", "attachment; filename=download.csv");
                    Response.BinaryWrite(ar);
                    Response.Flush();
                    Response.End();
                }

        }

The error was - "Index was outside the bounds of the array." 错误是-“索引超出了数组的范围。” I'm trying to download the file according to the event. 我正在尝试根据事件下载文件。 so far i have done this much of codes. 到目前为止,我已经完成了很多代码。 but i do not understand what the error means. 但我不明白错误的含义。 pls explain to me what the error "Index was outside the bounds of the array means" and pls give me solutions. 请向我解释错误“索引超出数组范围”意味着什么,请给我解决方案。 thanks 谢谢

you try to access up to 7 columns in that line, but you only have 1 column (EventId). 您尝试访问该行中的最多7列,但只有1列(EventId)。

Edit: 编辑:

You can't change the commandtext of a command while reading it. 阅读命令时,无法更改命令的命令文本。 Well, apparently you can, but you won't get the expected results. 好吧,显然可以,但是不会获得预期的结果。

The reader contains the result of the following statement 读者包含以下语句的结果

SELECT EventID from Event WHERE EventName = @EventName

and not for this statement 而不是为了这个陈述

   Select * from Attendance where EventID=@EventID

I would replace const string q with 我将const字符串q替换为

 const string q = @" Select * from dbo.Attendance 
                  where EventID = (SELECT EventID from dbo.Event WHERE EventName = @EventName");

And instead of * I would use the column names, for two reasons: the database server will like it more and you will be sure which columns you'll have 我使用*而不是*列名称,有两个原因:数据库服务器会更喜欢它,并且您将确定拥有哪些列

This error could happen if you read an array and ask for an index greater or equal than the lenght of the array. 如果您读取一个数组并要求一个大于或等于数组长度的索引,则可能发生此错误。 Checks if the table you're reading has 8 fields or if you're selecting 8 fields. 检查您正在读取的表是否具有8个字段,或者您是否选择8个字段。

Index out of bounds means exactly that, you are trying to access somewhere that is past the end of the array. 索引越界意味着您正在尝试访问超出数组末尾的位置。

eg: 例如:

var array = new[] {0, 1, 2};
var temp = array[10];

will throw an index out of bounds exception because there is no item at position 10 in array (it only has 3 items and so positions 0, 1 & 2). 将抛出索引超出范围异常,因为array中位置10处没有项目(它只有3个项目,因此位置0、1和2)。

This should be enough for you to try solving your problem. 这足以让您尝试解决问题。 If you are still stuck let me know and I'll take a look at your code. 如果您仍然遇到问题,请告诉我,我将看一下您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM