0

i want to prevent record insertion when nameTextBox and addressTextBox are null. but it always throws an exception. the exception message is pointed to submitchanges() which is use to insert the record. below is my code

                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);

                }
            }

do you know how to fix it? thanks

1 Answers1

2

the condition should be something like this

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);
}
Amin Sayed
  • 1,240
  • 2
  • 13
  • 26