我的if else语句存在问题。当我提交信息时,它应该读取组合框中的文本并给它一个定义的数字。然而,它直接掉到了其他地方并输入了。你能告诉我我做错了吗?
try
{
string combo;
if (comboBox1.SelectedText == "Random Pool")
{
combo = "10";
}
if (comboBox1.SelectedText == "Other")
{
combo = "20";
}
if (comboBox1.SelectedText == "DOT Pool")
{
combo = "30";
}
if (comboBox1.SelectedText == "Follow up")
{
combo = "40";
}
if (comboBox1.SelectedText == "Pre-employement Screening")
{
combo = "50";
}
if (comboBox1.SelectedText == "Aberrant Behavior")
{
combo = "60";
}
if (comboBox1.SelectedText == "Incident/Near Miss Investigation")
{
combo = "70";
}
if (comboBox1.SelectedText == "Investigation")
{
combo = "80";
}
else
{
combo = "20";
}
string cmdstring = "INSERT INTO Test (SELECTION_DATE, TEST_REASON_CODE, PEOPLESOFT_EMPL_ID, TEST_TYPE_CODE) VALUES(@date, '" + combo + "', @emp, 10);";
using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
{
cmd.Parameters.Add("@date", OleDbType.Date).Value = dateTimePicker1.Value;
cmd.Parameters.Add("@emp", OleDbType.Char).Value = textBox1.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Submitted Successfully");
}
}
catch (Exception ex)
{
MessageBox.Show("Failed due to " + ex.Message);
}
答案 0 :(得分:2)
问题在于,在您的代码中,它正在检查if (comboBox1.SelectedText == "Investigation")
,如果不是,则将组合设置为"20"
。写if,else if,else if等。即使先前的if条件已经执行了,底部的条件总是胜出,你可以做如下所示的事情:
string combo;
if (comboBox1.SelectedText == "Random Pool")
{
combo = "10";
}
else if (comboBox1.SelectedText == "Other")
{
combo = "20";
}
else if (comboBox1.SelectedText == "DOT Pool")
{
combo = "30";
}
else if (comboBox1.SelectedText == "Follow up")
{
combo = "40";
}
else if (comboBox1.SelectedText == "Pre-employement Screening")
{
combo = "50";
}
else if (comboBox1.SelectedText == "Aberrant Behavior")
{
combo = "60";
}
else if (comboBox1.SelectedText == "Incident/Near Miss Investigation")
{
combo = "70";
}
else if (comboBox1.SelectedText == "Investigation")
{
combo = "80";
}
else
{
combo = "20";
}
或者,你可以简单地创建一个函数:
string GetCombo()
{
if (comboBox1.SelectedText == "Random Pool")
return "10";
if (comboBox1.SelectedText == "Other")
return "20";
if (comboBox1.SelectedText == "DOT Pool")
return "30";
if (comboBox1.SelectedText == "Follow up")
return "40";
if (comboBox1.SelectedText == "Pre-employement Screening")
return "50";
if (comboBox1.SelectedText == "Aberrant Behavior")
return "60";
if (comboBox1.SelectedText == "Incident/Near Miss Investigation")
return "70";
if (comboBox1.SelectedText == "Investigation")
return "80";
return "20";
}
并在您的代码中
string combo = GetCombo();
string cmdstring = "INSERT INTO Test (SELECTION_DATE, TEST_REASON_CODE, PEOPLESOFT_EMPL_ID, TEST_TYPE_CODE) VALUES(@date, '" + combo + "', @emp, 10);";
// ...
希望它有所帮助! :)
编辑:正如Asad所提到的,您可以使用switch语句,如下所示:
string GetCombo()
{
switch(comboBox1.SelectedText)
{
case "Random Pool":
return "10";
case "Other":
return "20";
case "DOT Pool":
return "30";
case "Follow up":
return "40";
case "Pre-employement Screening":
return "50";
case "Aberrant Behavior":
return "60";
case "Incident/Near Miss Investigation":
return "70";
case "Investigation":
return "80";
default:
return "20";
}
}
答案 1 :(得分:0)
尝试这样
更改Text
而不是Selected Text
try
{
string combo;
if (comboBox1.Text== "Random Pool")
{
combo = "10";
}
if (comboBox1.Text== "Other")
{
combo = "20";
}
if (comboBox1.Text== "DOT Pool")
{
combo = "30";
}
if (comboBox1.Text== "Follow up")
{
combo = "40";
}
if (comboBox1.Text== "Pre-employement Screening")
{
combo = "50";
}
if (comboBox1.Text== "Aberrant Behavior")
{
combo = "60";
}
if (comboBox1.Text== "Incident/Near Miss Investigation")
{
combo = "70";
}
if (comboBox1.Text== "Investigation")
{
combo = "80";
}
else
{
combo = "20";
}
string cmdstring = "INSERT INTO Test (SELECTION_DATE, TEST_REASON_CODE, PEOPLESOFT_EMPL_ID, TEST_TYPE_CODE) VALUES(@date, '" + combo + "', @emp, 10);";
using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
{
cmd.Parameters.Add("@date", OleDbType.Date).Value = dateTimePicker1.Value;
cmd.Parameters.Add("@emp", OleDbType.Char).Value = textBox1.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Submitted Successfully");
}
}
catch (Exception ex)
{
MessageBox.Show("Failed due to " + ex.Message);
}