我有一个表单,它添加了员工的信息,但是我希望所有的字段都被填充。我不知道这是否是正确的代码,但即使我没有选中单选按钮和复选框,仍然可以从文本框中添加数据库中的值。我将感谢所有类型的回应。提前谢谢你。
if (((textBox2.Text == string.Empty ||
textBox3.Text == string.Empty ||
textBox4.Text == string.Empty ||
textBox5.Text == string.Empty ||
textBox6.Text == string.Empty ||
textBox7.Text == string.Empty ||
textBox8.Text == string.Empty) &&
(radioButton1.Checked == true ||
radioButton2.Checked == true) &&
(checkBox1.Checked == true ||
checkBox2.Checked == true)))
{
MessageBox.Show("All fields are required!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
connect.Open();
int age = Convert.ToInt32(textBox4.Text);
string save = "INSERT INTO emp (empID, empLName, empFName, empAge, empGender, empAddress, empEmail, empUser, empPass, empType) values('"
+ eid + "','" + textBox2.Text + "','" + textBox3.Text + "','" + age + "','" + gender + "','" + textBox5.Text + "','"
+ textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + postn + "')";
SqlCommand cmdsave = new SqlCommand(save, connect);
cmdsave.ExecuteNonQuery();
MessageBox.Show("Data Saved!");
connect.Close();
Admin adm = new Admin();
adm.Show();
this.Close();
}发布于 2017-03-23 12:19:06
你的条件应该是这样的
if ((textBox2.Text == string.Empty ||
textBox3.Text == string.Empty ||
textBox4.Text == string.Empty ||
textBox5.Text == string.Empty ||
textBox6.Text == string.Empty ||
textBox7.Text == string.Empty ||
textBox8.Text == string.Empty ||
(radioButton1.Checked == false &&
radioButton2.Checked == false ) ||
!checkBox1.Checked ||
!checkBox2.Checked ))或
您可以使用FOREACH
bool invalid = false;
foreach(Control ctr in this.Controls)
{
if(ctr is TextBox)
{
if(ctr.Text == string.empty)
{
invalid = true;
break;
}
}
}
if(invalid)
{
MessageBox.Show("All fields are required!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}https://stackoverflow.com/questions/42975666
复制相似问题