索引超出范围异常未被用户错误处理

本文关键字:用户 错误 处理 异常 范围 索引 | 更新日期: 2023-09-27 18:07:00

我有这个代码,问题是,每当我按下上传按钮,在标题中提到的错误出现,我该如何解决?以下是我的代码:aspx文件(我没有设置任何在GridView除了ID是myGridView):

<p>
<asp:Label ID="Label1" runat="server"></asp:Label>
</p>

<p>
    <asp:Button ID="BtnImport1" runat="server" onclick="BtnImport1_Click" 
        Text="Import" />
    <asp:Button ID="Cancel" runat="server" onclick="Cancel_Click" 
        Text="Cancel Import" />
</p>

<asp:GridView ID="myGridView" runat="server" CellPadding="4" 
    EnableModelValidation="True" ForeColor="#333333" GridLines="None" 
    Width="716px">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>

,这是我的。cs页面:

        string strFileNameOnServer = fileUpload.PostedFile.FileName;
        string fileExt =
        System.IO.Path.GetExtension(fileUpload.FileName);
        //string appDataPath = HttpContext.Current.Server.MapPath("~/App_Data");

        if (fileUpload.PostedFile != null && fileExt == ".csv")
        {
            try
            {
                //fileUpload.PostedFile.SaveAs(ConfigurationManager.AppSettings + appDataPath + "''" + strFileNameOnServer);
                fileUpload.PostedFile.SaveAs(Server.MapPath("~/Uploads"));
                //string appPath = HttpContext.Current.Request.ApplicationPath;
               // string physicalPath = HttpContext.Current.Request.MapPath("~/MajorProject");
                Label1.Text = "File name: " +
                       fileUpload.PostedFile.FileName + "<br>" +
                       fileUpload.PostedFile.ContentLength + " kb<br>" +
                       "Content type: " +
                       fileUpload.PostedFile.ContentType;
            }
            catch (Exception ex)
            {
                Label1.Text = "Error saving <b>" + strFileNameOnServer + "</b><br>.  " + ex.Message;
            }
            BtnImport1.Visible = true;
            Cancel.Visible = true;
            fileUpload.Visible = false;
            btnUpload.Visible = false;
        }
        else
        {
            Label1.Text = "Error - a file name must be specified/only csv files are allowed";
            return;
        }

        var data = File.ReadAllLines(Server.MapPath("~/Uploads"))
          .Select(line => line.Split(','))
          .Select(columns => new { GuestID = ErrorMessage(columns[0]), IC_No = ErrorMessage(columns[1]), Grouping = ErrorMessage(columns[2]), Remarks = ErrorMessage(columns[3]), GuestName = ErrorMessage(columns[4]), Class_Group = ErrorMessage(columns[5]), Staff = ErrorMessage(columns[6]), Attendance_Parents_Only = ErrorMessage(columns[7]), Registration = ErrorMessage(columns[8]), Event_ID = ErrorMessage(columns[9]) });


        myGridView.DataSource = data; 
        myGridView.DataBind();

请帮忙吗?div =/

我猜它在你索引到列数组的部分?你有那么多列吗?

索引超出范围异常未被用户错误处理