我正在使用数据库连接进行Windows窗体应用 我想点击listbox1项目并对db表进行查询并根据该条目填充listbox2。
listbox1有团队 listbox2有这些团队的成员。
目前,只要有人选择了一个项目,这就是我在listbox1上使用的方法:
string teamSelected = listBox1.GetItemText(listBox1.SelectedItem);
conexion = new dbconnector();
conexion.getMembersOfTeam(ref employeesM, teamSelected);
this.listBox2.DisplayMember = "userID";
this.listBox2.ValueMember = "userID"; //Field in the datatable which you want to be the value of the combobox
this.listBox2.DataSource = employeesM.Tables["users_table"];
但是我收到此错误消息:
对象引用未设置为对象的实例。
触发的代码行是:
datasetMembers.Tables.Add(employeesM);
该方法的完整代码是:
public void getMembersOfTeam(ref DataSet datasetMembers, string teamDesired)
{
string query4 = "select * from users_table Where userTeam = '" + teamDesired + "'"; //added by fran to retrieve all the teams
if (OpenConnection())
{
//retrieving all the teams using query3
MySqlCommand cmd4 = new MySqlCommand(query4, connection);
MySqlDataReader dataReader4 = cmd4.ExecuteReader();
dataReader4.Read();
if (dataReader4.HasRows)
{
//dataGridView1.Visible = true;
DataTable employeesM = new DataTable();
employeesM.Load(dataReader4);
datasetMembers.Tables.Add(employeesM);
}
else
{
//dataGridView1.Visible = false;
}
dataReader4.Close();
//end of retrieving teams
CloseConnection();
}
}
如何将查询中的所有结果发送到合适的数据集并更新listbox1?
提前感谢您提供任何帮助,我想我已经分享了一切以澄清我的问题,如果您需要更多数据,请告诉我们 谢谢!
答案 0 :(得分:0)
The datasetMembers is null. You have to initialized it before calling the getMembersOfTeam method.