Index out of range exception was unhandled by user errror?
I have this codes, the problem is, whenever I press the upload button, the error mentioned at the title appears, how can I solve it? Here are my codes: aspx file(I didnt set anything in the GridView except for the ID which was 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>
and here is my .cs page:
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();
Please help? =/
I would guess its in the part where you index into the columns array? Do you have that many columns?
精彩评论