2013-09-30 34 views
-2

我有如下代码...if/else语句不执行某些节点

if(txtEditName.Text.Trim() == "" || txtEditAddress.Text.Trim() == "") 
{ 
    lblBError.Enabled = true; 
    lblBError.Visible = true; 
    lblBError.Text = "Please provide the required field."; 
    return; 
} 
else 
{ 
    if(txtControl.Text.Trim() == "") 
    { 
     if(DropDownClient.Enabled) 
     { 
      if(DropDownClient.SelectedItem.Value == "select") 
      { 
       lblBError.Enabled = true; 
       lblBError.Visible = true; 
       lblBError.Text = "Please select Client."; 
       return; 
      } 
     } 
     else 
     { 
      if(lblClientName.Text.Trim() != "") 
      { 
       sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
        VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + lblClientName.Text + "'))"; 
      } 
      else 
      { 
       sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
        VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")"; 
      } 
     } 
    } 
    else 
    { 
     sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
      VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + Convert.ToInt32(txtControl.Text.Trim()) + ")"; 
     // SqlCommand cmd = new SqlCommand(sql, connection); 
    } 
} 

我遇到的问题是,代码的某些部分没有执行。当我运行,它忽略了其他部分,其中

if(lblClientName.Text.Trim() != "") 
{ 
} 

else 
{ 
    sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
     VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")"; 
} 

它跳的sql =”“在其他部分,而通过SQL屁股空字符串。我不知道为什么会发生这种情况?我检查了一切,看起来都很好。请问有一点,代码的问题是什么?

+1

可能是因为DropDownClient.Enabled始终为真? .. – Tigran

+0

当您尝试使用调试器时,他们告诉您关于lblClientName的信息? – BorHunter

+0

@NuruSalihu你可以给if语句块添加一个调试打印语句。这将告诉我们,如果条件'lblClientName.Text.Trim()!=“”'是true或false。 – Nikhil

回答

0

首先,在评论中提到的,使用string.IsNullOrWhitespace,或string.IsNullOrEmpty为您排忧解难:

if (string.IsNullOrWhitespace(txtEditName.Text) || string.IsNullOrWhitespace(txtEditAddress.Text)) 
{ 
    lblBError.Enabled = true; 
    lblBError.Visible = true; 
    lblBError.Text = "Please provide the required field."; 
    return; 
} 
else 
{ 
    if (!string.IsNullOrWhitespace(txtControl.Text)) 
    { 
     if (DropDownClient.Enabled && DropDownClient.SelectedItem.Value == "select") 
     { 
      lblBError.Enabled = true; 
      lblBError.Visible = true; 
      lblBError.Text = "Please select Client."; 
      return; 
     } 
     else 
     { 
      if (!string.IsNullOrWhitespace(lblClientName.Text)) 
      { 
       sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
         VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + lblClientName.Text + "'))"; 
      } 
      else 
      { 
       sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
       VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")"; 
      } 
     } 
     else 
     { 
      sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
      VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + Convert.ToInt32(txtControl.Text.Trim()) + ")"; 
       // SqlCommand cmd = new SqlCommand(sql, connection); 
     } 
    } 
} 

如果现在正在执行没有sql= ...线,那么它必须是dropdownclient启用其选定的项目=“选择”。没有别的我可以看到它可能。

编辑:

我试着重构你的代码。原谅任何错误,我没有太多时间这样做:

private bool EditFieldsAreValid() 
{ 
    if (string.IsNullOrWhiteSpace(txtEditName.Text) || string.IsNullOrWhiteSpace(txtEditAddress.Text)) 
     return false; 

    return true; 
} 

private string CreateSql(string value) 
{ 
    return @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
     VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + value + "'))"; 
} 

    if (!EditFieldsAreValid()) 
    { 
     lblBError.Enabled = true; 
     lblBError.Visible = true; 
     lblBError.Text = "Please provide the required field."; 
     return; 
    } 
    if (string.IsNullOrWhiteSpace(txtControl.Text)) 
    { 
     if (DropDownClient.Enabled && DropDownClient.SelectedItem.Value == "select") 
     { 
      lblBError.Enabled = true; 
      lblBError.Visible = true; 
      lblBError.Text = "Please select Client."; 
     } 
     else 
     { 
      if (string.IsNullOrWhiteSpace(lblClientName.Text)) 
      { 
       sql = CreateSql(lblClientName.Text); 
      } 
      else 
      { 
       sql = CreateSql(DropDownClient.SelectedItem.Value); 
      } 
     } 
    } 
    else 
    { 
     sql = CreateSql(txtControl.Text.Trim()); 
    } 
+0

嗨,只有txtControl.text的其他部分不执行。在单词中它不检查lblClientName .... –

0

谢谢你们的帮助。 Tigran先生指出的是非常有帮助的。我发现程序忽略的else语句不应该放在我所说的地方。它应该像这样。

if (txtControl.Text.Trim() == "") 
     { 
      if (DropDownClient.Enabled) 
      { 
       if (DropDownClient.SelectedItem.Value == "select") 
       { 
        lblBError.Enabled = true; 
        lblBError.Visible = true; 
        lblBError.Text = "Please select Client."; 

        return; 

       } 
       else 
       { 
        sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
        VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")"; 

       } 

      } 
      else 
      { 
       if (lblClientName.Text.Trim() != "") 
       { 
        sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
         VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + lblClientName.Text + "'))"; 

       } 
       else 
       { 
        sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID) 
         VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")"; 
       } 
      } 

虽然花了我几个小时。谢谢大家。