I have a ListBox with multiple select values. I am trying to fetch data from the Employees table. Depending on values from this ListBox I tried to use in statement. I checked the query and it's true but still didn't fetch any data without any errors.
Here is the code:
StringBuilder sb = new StringBuilder("SELECT * FROM Emp_Attend LEFT JOIN Employees on Emp_Attend.Emp_ID = Employees.EmpID WHERE ");
StringBuilder sb = new StringBuilder("SELECT * FROM Emp_Attend LEFT JOIN Employees on Emp_Attend.Emp_ID = Employees.EmpID WHERE ");
using (SqlCommand cmdSQL = new SqlCommand())
{
sb.Append("Emp_Attend.Emp_Name IN (");
string InPartQuery = string.Join(",", ListBox1.Items
.Cast<ListItem>()
.Where(t => t.Selected)
.Select(r => "'" + r.Text + "'"));
sb.Append(InPartQuery);
sb.Append(")");
cmdSQL.CommandText = sb.ToString();
cmdSQL.Connection = sqlcon;
SqlDataAdapter da = new SqlDataAdapter(cmdSQL);
DataTable dtbl = new DataTable();
da.Fill(dtbl);
sqlcon.Close();
gvEmployees.DataSource = dtbl;
gvEmployees.DataBind();