Take value From listbox to label in C# [duplicate]

2019-09-15 13:15发布

Possible Duplicate:
Take value from query to label in C#

Im newbie in C#

I have 3 table in Ms SQL.. tbl_staff, tbl_logs and tbl_department

In Windows form, I have one list box and two label. List box will display userID in tbl_logs.

When the user chooses from the list box, the label will change a value and display the name and department in same form.

The problem is, how can i paste a value from list box to label based on query?

This is my SQL query

SELECT tbl_staff.userID, tbl_staff.staffName,tbl_department.department FROM tbl_staff,tbl_logs,tbl_department WHERE tbl_staff.userID = tbl_logs.userID and tbl_staff.idDepartment = tbl_department.idDepartment;

How to do in c# and paste value to label? Below is my current code

SqlConnection openCon = new SqlConnection(connString);

                openCon.Open();

                SqlCommand queryChangeName = new SqlCommand();

                int index = listStaff.SelectedIndex;

                DataSet ds = new DataSet();

                ds.Clear();

                SqlDataAdapter adapter = new SqlDataAdapter("SELECT tbl_staff.userID, tbl_staff.staffName,tbl_department.department FROM tbl_staff,tbl_logs,tbl_department WHERE tbl_staff.userID = tbl_logs." + listStaff.SelectedValue + " and tbl_staff.idDepartment = tbl_department.idDepartment";", openCon);

I dont have any idea to do, hope someone can help me to resolve this problem. And sorry for bad english.

Thanks alot ;)

2条回答
迷人小祖宗
2楼-- · 2019-09-15 14:14

What your code is doing now is just filling up the database.Maybe you should have a look here Start To Windows Form hope this helps

查看更多
一纸荒年 Trace。
3楼-- · 2019-09-15 14:20

It might be better to do an initial load of the dataset (including staff id, name, and department) at the start, into a local DataTable.

Then use this to populate your drop down. You can then refer back to your local DataTable when you have a selected index from the listbox, to get the associated name and department.

查看更多
登录 后发表回答