How can i take value from query result to label?
I have two label, one is labelName and one more is labelDepartment
So when i r
One way to do:
private void getData()
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
connection.Open();
SqlCommand sqlCmd = new SqlCommand(" SELECT 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", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@username",user);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
lable.Text = dt.Rows[0]["staffName"].ToString(); //Where "staffName" is ColumnName
}
connection.Close();
}
Suggest to use a stored procedure not in line query to avoid SQL injection attacks.
stored procedure example