【发布时间】:2015-12-21 04:39:03
【问题描述】:
我使用 C# 从 10 列 Excel 电子表格(到 SQL Server Express)中加载数据,并在加载之前测试空值。我使用的代码适用于非关键字段,但是当它在关键字段中遇到空值时,我收到以下系统生成的错误消息:对象引用未设置为对象的实例。我希望在系统生成错误之前弹出 Response.Write 并停止代码执行。我的代码如下:
if ((string)(range.Cells[rowCount, columnCount] as Excel.Range).Value2.ToString() != null)
{
myVariable = (string)(range.Cells[rowCount, columnCount] as Excel.Range).Value2.ToString();
}
else
{
Response.Write(@"<script language='javascript'>alert('Update Aborted... \n \nThe load " +
"process has encountered an incomplete record on row " + rowCount + ". Please " +
"correct your Excel file and reload.');</script>");
return;
}
【问题讨论】: