A simple way could be the following:
- Build the SQL statement without conditions
- If company is selected modify the SQL to contain the WHERE clause and add the parameter
- complete the command and execute
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
string sql = @"Select fname,ename
FROM dbo.Users u
inner join dbo.Company c on c.companyName = u.company"
if (companyList.SelectedIndex > -1) {
sql = sql + " WHERE c.companyName = @companyname";
cmd.Parameters.AddWithvalue("@companyname", companyList.SelectedItem);
}
cmd.CommandText = sql;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
...