首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >插入到ms sql数据库不为空

插入到ms sql数据库不为空
EN

Stack Overflow用户
提问于 2017-03-23 12:11:46
回答 1查看 45关注 0票数 1

我有一个表单,它添加了员工的信息,但是我希望所有的字段都被填充。我不知道这是否是正确的代码,但即使我没有选中单选按钮和复选框,仍然可以从文本框中添加数据库中的值。我将感谢所有类型的回应。提前谢谢你。

代码语言:javascript
复制
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();
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-23 12:19:06

你的条件应该是这样的

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42975666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档