錯誤,我有這樣的代碼:檢查並顯示在GridView控件
private string ErrorMessage(string input)
{
{
if (!string.IsNullOrEmpty(input))
return input;
BtnImport1.Visible = false;
}
return "No value entered!";
}
protected void btnUpload_Click(object sender, EventArgs e)
{
string strFileNameOnServer = fileUpload.PostedFile.FileName;
string fileExt =
System.IO.Path.GetExtension(fileUpload.FileName);
if (fileUpload.PostedFile != null && fileExt == ".csv")
{
try
{
fileUpload.PostedFile.SaveAs(Server.MapPath("~/Uploads"));
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]) });
myGridView.DataSource = data;
myGridView.DataBind();
}
目前,如果在GridView的空字段,則顯示「無輸入值」,但是,這只是列[1 ]列[7]。如果我要上載包含無值的列[8]和列[0]的csv文件,則調試器會因出現錯誤而停止。我如何避免這種情況?請幫忙!
ErrorMessage方法的作用是什麼?它的參數是什麼?它返回什麼? – Icarus
調試器向您顯示的錯誤是什麼? – Tim
@Icarus - ErrorMessage方法顯示在OP代碼的開頭。 – Tim