0

当 nameTextBox 和 addressTextBox 为空时,我想防止记录插入。但它总是抛出异常。异常消息指向用于插入记录的 submitchanges()。下面是我的代码

                if (nameTextBox.Text != "" &&
                addressTextBox.Text != "")
            {
                _temporaryMember.Nama = nameTextBox.Text;
                _temporaryMember.Alamat = addressTextBox.Text;
                _temporaryMember.Telp = phoneNbrTextBox.Text;
            }
            else
            {
                {
                    string pesan = "";

                    if (nameTextBox.Text == "")
                        pesan += "Kolom Nama harus diisi.\n";
                    if (addressTextBox.Text == null)
                        pesan += "Kolom Alamat harus diisi.\n";

                    MessageBox.Show(pesan, "Maaf..", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }

你知道怎么解决吗?谢谢

4

1 回答 1

2

条件应该是这样的

if(!string.IsNullOrEmpty(nameTextBox.Text) 
   && !string.IsNullOrEmpty(addressTextBox.Text))
{
  // here goes your rest of the code

}
else
{
   string pesan = "";
   // no need to check again whether the textbox is empty.
   pesan += "Kolom Nama harus diisi.\n"
   ...
    MessageBox.Show(pesan, "Maaf..", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
于 2012-11-19T07:42:24.883 回答