C# If Else 在 For 循环中不起作用
C# If Else Inside For Loop Didn't Work
我正在创建一个 Web 应用程序以将 excel 文件导入 MS Dynamics Crm。这是代码片段
for (i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][3].ToString() != String.Empty)
{
try
{
Entity contractline = new Entity("new_contractline");
contractline["new_lineitem"] = dt.Rows[i][1].ToString();
contractline["new_sublineitem"] = dt.Rows[i][3].ToString();
createChildData = service.Create(contractline);
string guidString = createChildData.ToString();
guidRecord[i] = guidString;
}
catch (Exception ex)
{
}
}
else if (dt.Rows[i][3].ToString() == string.Empty) /**THIS BLOK BELOW WON'T FIRE **/
{
try
{
Entity contractline = new Entity("new_contractline");
contractline["new_lineitem"] = dt.Rows[i][2].ToString();
contractline["new_sublineitem"] = dt.Rows[i][3].ToString();
contractline["new_quantity"] = "33";
service.Create(contractline);
}
catch (Exception ex)
{
}
}
问题是 else if
块在 dt.Rows[i][3]
为空时没有触发。可能是什么问题呢?
只需将 If...Else 与 string.IsNullOrEmpty(s) 一起使用,如下所示....
if (string.IsNullOrEmpty(dt.Rows[i][3].ToString()) == true)
{
// True.
Console.WriteLine("Null or empty");
}
else
{
Console.WriteLine("Not null and not empty");
}
你在下面尝试过吗?
else if (dt.Rows[i][3].ToString().Trim() == "")
你为什么不做 else
?如果你没有其他的 else-ifs.
if (dt.Rows[i][3].ToString() != String.Empty)
如果 .ToString()
为空, 将触发。但是,如果 dt.Rows[i][3]
为空,您将在此处获得异常,因为您不能 ToString()
空对象。如果您在此代码块之外有一个 try catch,我建议您检查一下,因为 dt.Rows[i][3]
可能为 null 并抛出异常。
如果你有任何其他 else-ifs,你应该使用
else if (string.IsNullOrWhiteSpace(dt.Rows[i][3].ToString()))
并更改您的 if 块(实际上我建议您在任何情况下都这样做)
if (!string.IsNullOrWhiteSpace(dt.Rows[i][3].ToString()))
原因是并非所有 spaces 实际上都是 space 条字符。您可能会得到其他 white space characters 不会触发您的 String.Empty 块,因为它不是空的。这将匹配您的单元格可能具有的 space 的数量。这也将捕获不可见的非 spacebar spaces。一个示例相当于 HTML 代码
(非中断 space),它实际上不是 spacebar space(并且不会匹配“ ") 但会占用 space。
我正在创建一个 Web 应用程序以将 excel 文件导入 MS Dynamics Crm。这是代码片段
for (i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][3].ToString() != String.Empty)
{
try
{
Entity contractline = new Entity("new_contractline");
contractline["new_lineitem"] = dt.Rows[i][1].ToString();
contractline["new_sublineitem"] = dt.Rows[i][3].ToString();
createChildData = service.Create(contractline);
string guidString = createChildData.ToString();
guidRecord[i] = guidString;
}
catch (Exception ex)
{
}
}
else if (dt.Rows[i][3].ToString() == string.Empty) /**THIS BLOK BELOW WON'T FIRE **/
{
try
{
Entity contractline = new Entity("new_contractline");
contractline["new_lineitem"] = dt.Rows[i][2].ToString();
contractline["new_sublineitem"] = dt.Rows[i][3].ToString();
contractline["new_quantity"] = "33";
service.Create(contractline);
}
catch (Exception ex)
{
}
}
问题是 else if
块在 dt.Rows[i][3]
为空时没有触发。可能是什么问题呢?
只需将 If...Else 与 string.IsNullOrEmpty(s) 一起使用,如下所示....
if (string.IsNullOrEmpty(dt.Rows[i][3].ToString()) == true)
{
// True.
Console.WriteLine("Null or empty");
}
else
{
Console.WriteLine("Not null and not empty");
}
你在下面尝试过吗? else if (dt.Rows[i][3].ToString().Trim() == "")
你为什么不做 else
?如果你没有其他的 else-ifs.
if (dt.Rows[i][3].ToString() != String.Empty)
如果 .ToString()
为空, 将触发。但是,如果 dt.Rows[i][3]
为空,您将在此处获得异常,因为您不能 ToString()
空对象。如果您在此代码块之外有一个 try catch,我建议您检查一下,因为 dt.Rows[i][3]
可能为 null 并抛出异常。
如果你有任何其他 else-ifs,你应该使用
else if (string.IsNullOrWhiteSpace(dt.Rows[i][3].ToString()))
并更改您的 if 块(实际上我建议您在任何情况下都这样做)
if (!string.IsNullOrWhiteSpace(dt.Rows[i][3].ToString()))
原因是并非所有 spaces 实际上都是 space 条字符。您可能会得到其他 white space characters 不会触发您的 String.Empty 块,因为它不是空的。这将匹配您的单元格可能具有的 space 的数量。这也将捕获不可见的非 spacebar spaces。一个示例相当于 HTML 代码
(非中断 space),它实际上不是 spacebar space(并且不会匹配“ ") 但会占用 space。