The following code as I understand it should create a parameterized statement and adds a value to that parameter. The parameter being "@exp" and the value being added to it is determined by user input. I store the user input in String exp;
(Not my choice someone else wrote the majority of this function).
SQl_Command.CommandText = "SELECT COUNT(*) As MyCount FROM members WHERE ([Primary Exp] = '@exp') AND ([Approved] = 'True') OR ([Approved] = 'True') AND ([Secondary Exp] = '@exp')";
SQl_Command.Parameters.Add("@exp", SqlDbType.NVarChar, 255);
SQl_Command.Parameters["@exp"].Value = exp;
The desired result for my test case is 16 but this query returns 0. The query works when running from the SQL Server 2008 and looks like this.
SELECT COUNT(*) As MyCount
FROM members
WHERE ([Primary Exp] = 'Risk Management')
AND ([Approved] = 'True') OR ([Approved] = 'True')
AND ([Secondary Exp] = 'Risk Management')
I should be able to use Response.Write(SQl_Command.CommandText);
where I have selected "Risk Management" and the parameterized query should print out:
SELECT COUNT(*) As MyCount
FROM members
WHERE ([Primary Exp] = 'Risk Management')
AND ([Approved] = 'True') OR ([Approved] = 'True')
AND ([Secondary Exp] = 'Risk Management')`
It however does not. It leaves @exp
as if that were the value I wanted to compare [Primary Exp] and [Secondary Exp] to. Is that correct?
I have seen multiple ways of doing this but I have in general been following the example here
I don't think this is the problem but this is how I use the query.
SQl_Reader = SQl_Command.ExecuteReader();
int numRows = 0;
while(SQl_Reader.Read()){
numRows = Convert.ToInt32(SQl_Reader["MyCount"]);
}
I'm sure it is something simple I am missing but I havn't been able to see it.