我正在使用datareader从我的数据库中读取数据并将它们存储到字符串中。但我似乎无法使用存储在字符串上的数据来对我的comboBox做任何事情,就像我想要它在if语句上做的那样。
我有一个comboBox选择的索引语句,它正常工作。
if (cboBase.SelectedIndex == 0)
{
cboTeam.Items.Clear();
cboTeam.Items.Add("1");
cboTeam.Items.Add("2");
cboTeam.Items.Add("3");
cboTeam.Items.Add("4");
cboTeam.Items.Add("6");
cboTeam.Items.Add("8");
cboTeam.SelectedIndex = 0;
}
else if (cboBase.SelectedIndex == 1)
{
cboTeam.Items.Clear();
cboTeam.Items.Add("5");
cboTeam.Items.Add("7");
cboTeam.Items.Add("9");
cboTeam.Items.Add("10");
cboTeam.SelectedIndex = 0;
}
else
{
cboTeam.Items.Clear();
cboTeam.Items.Add("11");
cboTeam.Items.Add("12");
cboTeam.Items.Add("13");
cboTeam.Items.Add("14");
cboTeam.SelectedIndex = 0;
}
当我使用我的datareader数据来选择cboBase时,一切正常。但是,当我尝试使用相同的方法来选择cboTeam并执行clickButton上的点击时,它无法正常工作......有人可以指导我走正确的道路吗?
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT EmployeeID, HomeBase, TeamID, StaffType, PIC From Employee WHERE EmployeeID ='" + txtEmployeeID.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlDataReader reader = cmd.ExecuteReader();
string EmployeeID = null;
string HomeBase = null;
string TeamID = null;
string Position = null;
string PIC = null;
while (reader.Read())
{
EmployeeID = reader.GetInt32(0).ToString();
HomeBase = reader.GetString(1).ToString();
TeamID = reader.GetInt32(2).ToString();
Position = reader.GetInt32(3).ToString();
PIC = reader.GetInt32(4).ToString();
}
if (HomeBase == "JUR")
{
cboBase.SelectedIndex = 0;
}
else if (HomeBase == "LVR")
{
cboBase.SelectedIndex = 1;
}
else if (HomeBase == "SBW")
{
cboBase.SelectedIndex = 2;
}
else if (TeamID == "1")
{
cboTeam.SelectedIndex = 0;
}
else if (TeamID == "2")
{
cboTeam.SelectedIndex = 1;
}
else if (TeamID == "3")
{
cboTeam.SelectedIndex = 2;
}
else if (TeamID == "4")
{
cboTeam.SelectedIndex = 3;
}
else if (TeamID == "6")
{
cboTeam.SelectedIndex = 4;
}
else if (TeamID == "8")
{
cboTeam.SelectedIndex = 5;
}
else if (TeamID == "5")
{
cboTeam.SelectedIndex = 0;
}
else if (TeamID == "7")
{
cboTeam.SelectedIndex = 1;
}
else if (TeamID == "8")
{
cboTeam.SelectedIndex = 2;
}
else if (TeamID == "10")
{
cboTeam.SelectedIndex = 3;
}
else if (TeamID == "11")
{
cboTeam.SelectedIndex = 0;
}
else if (TeamID == "12")
{
cboTeam.SelectedIndex = 1;
}
else if (TeamID == "13")
{
cboTeam.SelectedIndex = 2;
}
else if (TeamID == "14")
{
cboTeam.SelectedIndex = 3;
}
else if (Position == "1")
{
cboPosition.SelectedIndex = 0;
}
else if (Position == "2")
{
cboPosition.SelectedIndex = 1;
}
else if (Position == "3")
{
cboPosition.SelectedIndex = 2;
}
else if (PIC == "1")
{
radioPIC.PerformClick();
}
else if (PIC == "0")
{
radioNo.PerformClick();
}