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 ;)
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
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.