我想在nameTextBox和addressTextBox为null时阻止记录插入。但它总是抛出异常。异常消息指向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);
}
}
你知道如何解决这个问题吗?感谢
答案 0 :(得分: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);
}