'Not able to apply three filters in one SQL query

I have created a Report.aspx page where 3 filters are applied for fetching table from database, the filters are Firstname, City, Languages. But if FirstName and City is null and only Language is selected from Dropdown, no records are showing. And Firstname and City is working fine. Could anyone tell me what is the issue here!! Why Languages filter is not working?

SQL query from Storeprocedure:

SELECT distinct T1.EmpId, T1.FirstName, T1.LastName, T1.Contact, T1.Gender, T1.City, T1.Username, T1.Password, 
    T1.ConfirmPassword, T1.Gmail, T2.Languages 
    FROM Employeedata AS T1 left outer join(SELECT EmpId, string_agg(Languages, ' , ') AS Languages 
    from EmployeeLanguages group by EmpId) T2 on T1.EmpId = T2.EmpId 
        where
            ( T1.FirstName = @FirstName)
        OR
            (  T1.City = @City ) 
        OR
            (  T2.Languages= @Languages)

Search button code from Report.aspx.cs page:

 protected void btnSearch_Click(object sender, EventArgs e)
        {
            int EmpId = hfEmpId.Value == "" ? 0 : Convert.ToInt32(hfEmpId.Value);
            String FirstName = txtFirstName.Text.Trim();
            String City = txtCity.Text.Trim();
            String Languages = Convert.ToString(DropDownList1.SelectedValue); 
            con.Open();
            SqlDataAdapter sqlDa = new SqlDataAdapter("EmployeeSearchByID", con);
            sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlDa.SelectCommand.Parameters.AddWithValue("@EmpId",EmpId);
            sqlDa.SelectCommand.Parameters.AddWithValue("@FirstName",FirstName);
            sqlDa.SelectCommand.Parameters.AddWithValue("@City", City );
            sqlDa.SelectCommand.Parameters.AddWithValue("@Languages", Languages);
            
            DataTable dtbl = new DataTable();
            sqlDa.Fill(dtbl);
            con.Close();
            Gridview1.DataSource = dtbl;
            Gridview1.DataBind();
            Gridview1.Visible = true;
        }     

Languages dropdownlist code from Report.aspx page:

<td>Languages</td>
                    <td>
                        <asp:DropDownList ID="DropDownList1" runat="server" >
                          <asp:ListItem Text="Select Languages" Value="select" Selected="True"></asp:ListItem>
                            <asp:ListItem Text="Asp.Net" Value="Asp.Net" ></asp:ListItem>
                            <asp:ListItem Text="C#" Value="C#" ></asp:ListItem>
                            <asp:ListItem Text="JQuery" Value="JQuery" ></asp:ListItem>
                             <asp:ListItem Text="JavaScript" Value="JavaScript" ></asp:ListItem>
                             <asp:ListItem Text="SQL Server" Value="SQL Server" ></asp:ListItem>
                         </asp:DropDownList>
                    </td>

 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source