我制作了一个方法,分别使用从组合框中选择的“项目名称”获取所有信息。
这是我的代码:
private void comboBox1_KeyPress(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string query = "select project_name from JO.dbo.Proj left join JO.dbo.Comp on Proj.company_id = Comp.company_id where Proj.company_name = '" + comboBox1.SelectedItem + "'";
SqlCommand command = new SqlCommand(query, conn);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
comboBox2.Items.Clear();
while (reader.Read())
{
comboBox2.Items.Add(reader["project_name"].ToString());
}
reader.Close();
}
conn.Close();
conn.Dispose();
}
}
;
void getAllInfoProj()
{
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string query2 = "select contact_person,contact_no,address from JO.dbo.Proj left join JO.dbo.Comp on Proj.company_id = Comp.company_id where project_name = '" + comboBox2.SelectedItem + "'";
SqlCommand command = new SqlCommand(query2, conn);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
txtAddress.Text = reader["address"].ToString();
txtContactNum.Text = reader["contact_no"].ToString();
txtContactPerson.Text = reader["contact_person"].ToString();
}
reader.Close();
}
conn.Close();
conn.Dispose();
}
当我在上面的方法上插入这个方法时,它根本没有任何结果。因为当我从组合框中选择“项目名称”时,我试图自动填充这些文本框