我在C#中的if else语句不起作用

时间:2016-08-16 10:30:42

标签: c#

当用户将文本框留空时,我确实无法添加显示消息的条件。我已经尝试了所有我知道的更改方式,但它根本无法正常工作我有" 5"文本框并隐藏和显示它们基于选定的组合框项目值,例如,如果我选择了枪支它应该全部五个文本框,如果我选择弹药然后它应该只显示三个文本框(1,2和3)。它应该显示消息框警告我,如果我留下一个文本框空白。

但问题是在弹药设置期间,即使我填写了所有文本字段,消息框仍然显示出来。我的if else语句是否有问题?

private void insertbtn_Click(object sender, EventArgs e)
{
    string mysql = "";
    if (comboBox1.SelectedItem.ToString() == "Firearm")
    {
        mysql = "insert into Firearm(Fid,Fname,Ftype,Manufacturer,Price) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "')";
    }
    if (comboBox1.SelectedItem.ToString() == "Ammo")
    {
        mysql = "insert into Ammo(Aid,Atype,Coating,Metal) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
    }

    if (comboBox1.SelectedItem.ToString() == "Ammo" && textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" )
    {
        MessageBox.Show("Please Fill all of the text fields");
    }
    else if (comboBox1.SelectedItem.ToString()=="Firearm" && textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text=="" )
    {           
         MessageBox.Show("Please Fill all of the text fields"); 
    }
    else
    {
        try
        {
            SqlConnection conn = new SqlConnection(dbsource);
            conn.Open();
            SqlCommand cmd = new SqlCommand(mysql, conn);
            cmd.ExecuteNonQuery();
            MessageBox.Show("New Data Inserted!");
            conn.Close();
        }
        catch (SqlException)
        {
            MessageBox.Show("Error!!!");
        }
    }           
}

0 个答案:

没有答案