我的组合框列出了数据库中的记录。我的组合框如下:
<ComboBox Grid.Column="3" Grid.Row="1" Height="30" VerticalAlignment="Center" Name="comboBoxEmp" SelectionChanged="comboBoxAdministrator_SelectionChanged" BorderBrush="{x:Null}" Foreground="#FFF90C0C" />
我有方法将组合框填充为:
void FillComboEmpName()
{
MySqlConnection conn4;
conn4 = new MySqlConnection("server=localhost;uid=root;database=newcompanydatabase;pwd=Admin@123;");
string Query = "select * from nWorksUser";
MySqlCommand cmd2 = new MySqlCommand(Query, conn4);
try
{
conn4.Open();
MySqlDataReader rdr;
rdr = cmd2.ExecuteReader();
while (rdr.Read())
{
string EmpNo = rdr.GetInt32("uid").ToString();
string fname = rdr.GetString("fname");
string lname = rdr.GetString("lname");
comboBoxEmp.Items.Add(EmpNo + "-" + fname + " " + lname);
}
}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message);
}
}
由于我使用以下方法重置组合框的内容,我收到错误: &#34;指数超出范围。必须是非负的或小于集合的大小。参数名称:索引&#34;
public Reset()
{
comboBoxEmp.Items.Clear();
FillComboEmpName();
}