3

why is this not working ? It's something about the commandstring syntax near = but I can't seem to figure it out, the online examples seem exactly the same. Edit: Activated In is a column.

examples from How to select data from database with many filter options?

private void btnDist_Click(object sender, EventArgs e)
{
    string cmdText = "SELECT * FROM Inventory WHERE Rep <> '#NA' AND Activated In = '#NA'";
    SqlDataAdapter adapter = new SqlDataAdapter(cmdText, GetConnection());
    DataSet distDS = new DataSet();
    adapter.Fill(distDS);

    dataGridView1.DataSource = distDS.Tables[0].DefaultView;
}

6 Answers 6

6

If the name of the column on your database is Activated In (with a space) then you need to use square brackets when referring to the object / column name in queries, for example:

SELECT * FROM Inventory WHERE Rep <> '#NA' AND [Activated In] = '#NA'

This is because SQL treats spaces as a separator in the query language - square brackets are used to "escape" this (and other characters) in names.

Alternatively if the column is just called Activated then you don't need the IN bit - either test for equality or test to see if the value is in a given range, but don't do both.

-- Use this
SELECT * FROM Inventory WHERE Rep <> '#NA' AND Activated = '#NA'
-- Or this
SELECT * FROM Inventory WHERE Rep <> '#NA' AND Activated IN ('#NA', 'Some other value')
Sign up to request clarification or add additional context in comments.

2 Comments

Which is why you should avoid using spaces or keywords in the table design inteh first place. If this is a new table not yet pushed to prod, I would immediately fix the fieldname.
@Kragen, thanks a lot for the answer man, Activated In is(was) a column name so the brackets fixed the problem. Wouldn't even know what the actual IN keyword is for
4

change you query to There is no need of IN keyword

SELECT * FROM Inventory WHERE Rep <> '#NA' AND Activated = '#NA'"

IN - used when you want to filter out data from the list or subquery is there.

Comments

2

The In keyword is used as follows:

...AND Activated IN ('#NA')
...AND Activated IN ('#NA', 'other filter value', 'more filter values')

Comments

1

Activated In = '#NA'"

This needs to be Activated In ('#NA')" or Activated = '#NA'".
The In operator in SQL is reserved.

Comments

1

Remove the = after In

string cmdText = "SELECT * FROM Inventory WHERE Rep <> '#NA' AND Activated In ('#NA')";

You might also want to use = instead of In since you are only specifying one item.

string cmdText = "SELECT * FROM Inventory WHERE Rep <> '#NA' AND Activated = '#NA'";

If Activated In is a column, then use:

string cmdText = "SELECT * FROM Inventory WHERE Rep <> '#NA' AND [Activated In] = '#NA'";

Comments

1

Activated In is a column ? then it should have been ActivatedIn or [Activated In]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.