-2

I don't know what is the correct syntax for adding more columns to the WHERE clause of my SELECT statement. (department is one of the fields I want to add and one other one.)

protected void Button1_Click1(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            query = "select * from table_1 where Department = '" + DDl_Dep.SelectedItem.ToString() + "'";
            cmd = new SqlCommand(query, con);
            da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Gview.DataSource = ds;
            Gview.DataBind();
            con.Close();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }
HappyCoding
  • 641
  • 16
  • 36
Asaf_G
  • 1
  • 2
  • Your current query pulls all fields back. You are only filtering on `Department`. If you want to add another condition to the WHERE clause, use `AND` or `OR` and the other condition. If you want to access columns other than `Department`, do it - your query already selects them all. If none of that makes immediate sense - stop writing code, and go to the bookstore and get a book on SQL. – Patrick87 Mar 02 '16 at 17:40
  • https://msdn.microsoft.com/en-us/library/ms188047.aspx – Chris Mar 02 '16 at 17:40
  • http://stackoverflow.com/questions/4047484/selecting-with-multiple-where-conditions-on-same-column – laaksom Mar 02 '16 at 17:40

4 Answers4

1

You are already selecting all your fields.

SELECT * FROM Department

means nothing else than: SELECT all fields from Department table Or do you want to reduce the rows in the output? Then you have to increase the WHERE clause with AND, ORs, etc.

SELECT Field1, Field2, Field3, ... FROM Department WHERE Field1 = 'something' AND Field2 = 'something'
Bernd
  • 593
  • 2
  • 8
  • 31
  • the two fields is two drop down list, every drop down return a different value from DB. what i need is when i select one value or two from one drop down or from the two of them i except to see all the records from DB Related. – Asaf_G Mar 02 '16 at 18:34
0

Here is an example:

select * from table_1 where Department ="HR" and SomeOtherColumn=""
lucas
  • 4,445
  • 6
  • 28
  • 48
0

for example if this your Table: [id][bla1][bla2][department][somefield]

"select * from table_1 where Department = '" + DDl_Dep.SelectedItem.ToString() + "' AND somefield ='"+ someSelected!+"'";

  • Please don't post answers with SQL injection. – Alexei Levenkov Mar 02 '16 at 17:55
  • for some reason it's don't work, i try this select: query = "select * from table_1 where Department = '" + DDl_Dep.SelectedItem.ToString() + "' AND role = '" + DDL_Role.SelectedItem.ToString() + "'"; – Asaf_G Mar 02 '16 at 17:59
  • I Found a solution: query = "select * from table_1 where (Department like '%' + @ search1 + '%') and more..."; – Asaf_G Mar 03 '16 at 06:21
0

The correct way to add another column to a WHERE clause is to append it with the word and or AND.

I would format your query value as such:

query = "select * "
      + "from table_1 "
      + "where Department = '" + DDl_Dep.SelectedItem.ToString() + "'"
      + "and ColumnNameHere = 'value'";
HappyCoding
  • 641
  • 16
  • 36