locked
Using two 'Where' variables within a Select CommandText RRS feed

  • Question

  • User1382151166 posted

    I'd like to find the value of Field1 in my database table "TableA" where VariableA=1 and VariableB=2.

     This is the code I have:

    dataX.CommandText =

    "Select Field1 FROM TableA WHERE VariableA=1,VariableB=2";

    However I get error:

    Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near ','.

    So presumably it doesn't like having two things in the WHERE part. Is it impossible to have two variables in this section?

    Thanks!

    Tuesday, March 15, 2011 11:23 AM

Answers

  • User77042963 posted
    dataX.CommandText = "Select Field1 FROM TableA 
    WHERE  someCol1=@var1 AND   someCol2=@var2"
    
    Assign your variable values through paramters:
    
    dataX.Parameters.AddWithValue(@var1,VariableA)
    dataX.Parameters.AddWithValue(@var2,VariableB)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2011 11:31 AM
  • User794480159 posted

    Replace comma in your commandtext with and  operator.

    dataX.CommandText =  "Select Field1 FROM TableA WHERE VariableA=1 and VariableB=2";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2011 11:35 AM
  • User-121157572 posted

    Hi,

    If you have multiple condition in where clause then you have to use "and"

    Check below query,

     

    "Select Field1 FROM TableA WHERE VariableA=1 AND VariableB=2";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2011 11:36 AM

All replies

  • User77042963 posted
    dataX.CommandText = "Select Field1 FROM TableA 
    WHERE  someCol1=@var1 AND   someCol2=@var2"
    
    Assign your variable values through paramters:
    
    dataX.Parameters.AddWithValue(@var1,VariableA)
    dataX.Parameters.AddWithValue(@var2,VariableB)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2011 11:31 AM
  • User794480159 posted

    Replace comma in your commandtext with and  operator.

    dataX.CommandText =  "Select Field1 FROM TableA WHERE VariableA=1 and VariableB=2";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2011 11:35 AM
  • User-121157572 posted

    Hi,

    If you have multiple condition in where clause then you have to use "and"

    Check below query,

     

    "Select Field1 FROM TableA WHERE VariableA=1 AND VariableB=2";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2011 11:36 AM
  • User1382151166 posted

    Thanks all!

    I can't believe that was the problem, I was sure I'd tried that. I've been struggling to get to the bottom of this for hours, so simple!

    Maybe I only tried using the + symbol instead of AND. I must have seen that used in an example somewhere online.

    Grrrrrr.......

    Tuesday, March 15, 2011 12:20 PM