2013-09-30 39 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 + ")"; 
       } 
      } 

雖然花了我幾個小時。謝謝大家。