1

例外を処理してアプリケーションを改善しようとしています。すべてのフィールドが入力されていない場合、ユーザーにメッセージ ボックスを表示するフォームがあります。以下は私が試みたものですが、すべてのフィールドが完了しても合格できません。

if (textBox1.Text == null || comboBox3.SelectedValue == null || 
    comboBox4.SelectedValue == null || 
    comboBox5.SelectedValue == null || comboBox8.SelectedValue == null)
{
    MessageBox.Show("Please make sure you don't have any missing fields");
}
else
{
    connection.Open();

    //update the settings to the database table 
    MySqlCommand command = connection.CreateCommand();
    // Insert into table_name values ("","name","1")
    command.CommandText = @"insert into CustomTests values ('','" + textBox1.Text + "'," + Convert.ToBoolean(checkBox1.CheckState) + ",'" + comboBox3.Text + "'," + comboBox4.Text + "," + comboBox5.Text + ",'" + comboBox8.Text + "'," + comboBox2.Text + "," + Timer_Enabled + ",'" + comboBox1.Text + "')";

    command.ExecuteNonQuery();
}
4

4 に答える 4

1

TextBox のText値は、 ではなく空の文字列 "" に設定されますnull。次に、ComboBox.SelectedValue を使用する代わりに、ComboBox.SelectedIndex を使用して、-1 (何も選択されていない場合の既定値) でないことを確認します。

 if (textBox1.Text == "" || comboBox3.SelectedIndex == -1 
     || comboBox4.SelectedIndex == -1 || comboBox5.SelectedIndex == -1 
     || comboBox8.SelectedIndex == -1)
于 2013-05-06T17:14:53.423 に答える
0

null かどうかをチェックしないでください。テキストの長さが 0 より大きいかどうかを確認します

于 2013-05-06T17:10:06.583 に答える
0

これを試すこともできます....

if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(this.comboBox3.Text) || string.IsNullOrEmpty(this.comboBox4.Text) ||
     string.IsNullOrEmpty(this.comboBoxSelect5.Text)|| string.IsNullOrEmpty(this.comboBox8.Text))
于 2014-03-11T11:19:18.627 に答える