I make a method that get all the infos respectively with the "project name" selected from a comboBox.
我创建了一个方法,分别使用从comboBox中选择的“项目名称”获取所有信息。
Here's my code:
这是我的代码:
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();
}
When I insert this method on the method above it doesn't have any result at all. because I'm trying to autofill those textboxes when I choose a "project name" from comboBox
当我在上面的方法上插入此方法时,它根本没有任何结果。因为当我从comboBox中选择“项目名称”时,我正在尝试自动填充这些文本框
2 个解决方案
#1
1
You can call the method on SelectedIndexChanged
event of the comboBox2
您可以在comboBox2的SelectedIndexChanged事件上调用该方法
private void comboBox2_SelectedIndexChanged(object sender,
System.EventArgs e)
{
getAllInfoProj();
}
Note: better to use sql Paramemeters instead of inline parameters in the sql statement.
注意:最好在sql语句中使用sql参数,而不是内联参数。
and also in yoour getAllInfoProj()
you are overwriting textbox text properties in a loop, you can only see the last record value at the end on the UI.
并且在yoour getAllInfoProj()中,您在循环中覆盖文本框文本属性,您只能在UI的末尾看到最后一个记录值。
#2
0
As you want to load the textboxes when some selection is change ( a project is selected), therefore you need to handle the event SelectedIndexChanged
of your combobox2
. And call your method getAllInfoProj()
in that event.
由于您希望在更改某些选择(选择项目)时加载文本框,因此您需要处理组合框2的事件SelectedIndexChanged。并在该事件中调用您的方法getAllInfoProj()。
#1
1
You can call the method on SelectedIndexChanged
event of the comboBox2
您可以在comboBox2的SelectedIndexChanged事件上调用该方法
private void comboBox2_SelectedIndexChanged(object sender,
System.EventArgs e)
{
getAllInfoProj();
}
Note: better to use sql Paramemeters instead of inline parameters in the sql statement.
注意:最好在sql语句中使用sql参数,而不是内联参数。
and also in yoour getAllInfoProj()
you are overwriting textbox text properties in a loop, you can only see the last record value at the end on the UI.
并且在yoour getAllInfoProj()中,您在循环中覆盖文本框文本属性,您只能在UI的末尾看到最后一个记录值。
#2
0
As you want to load the textboxes when some selection is change ( a project is selected), therefore you need to handle the event SelectedIndexChanged
of your combobox2
. And call your method getAllInfoProj()
in that event.
由于您希望在更改某些选择(选择项目)时加载文本框,因此您需要处理组合框2的事件SelectedIndexChanged。并在该事件中调用您的方法getAllInfoProj()。