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

时间:2011-08-31 02:27:31

标签: c# asp.net gridview

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

<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();

请帮帮忙? = /

1 个答案:

答案 0 :(得分:0)

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