I have an SQLQuery:
SELECT * FROM (select ROW_NUMBER() OVER( ORDER BY @sortColumns ) AS 'RowNumber'
FROM MyTable WHERE '@keyword' = '' OR (AppId = '@keyword'))
atd WHERE (RowNumber between @startRecord and @endRecord);
There 4 parameters in the query:
sortColumns
keyword
startRecord
endRecord
I've used VB.NET language to open a connection and pass values into the query via 4 parameters using Command.Paremeters:
Dim sortColumns = "AppId ASC"
Dim keyword = "abc"
Dim startN As Integer = 1
Dim endN As Integer = 20
Dim ds As New DataSet()
Dim strDatabaseConnectionString As String = ConfigHelper.MainConnectionString
Using connection As New SqlClient.SqlConnection(strDatabaseConnectionString)
Using cmd As New SqlClient.SqlCommand(query, connection)
cmd.Parameters.Add("@sortColumns", SqlDbType.NVarChar).Value = sortColumns
cmd.Parameters.Add("@keyword", SqlDbType.NVarChar).Value = keyword
cmd.Parameters.Add("@startRecord", SqlDbType.Int).Value = startN
cmd.Parameters.Add("@endRecord", SqlDbType.Int).Value = endN
Dim adapter As New SqlClient.SqlDataAdapter(cmd)
adapter.Fill(ds)
End Using
End Using
When the code run into line "adapter.Fill(ds)", it generate an exception.
Exception Message: An expression of non-boolean type specified in a context where a condition is expected
My Suspicion:
- Whether the way I pass the parameters' values is not correct
- We cannot pass a parameter as a column name
Issue: I don't know what wrong with the code that I've used to pass the values into sqlquery. Could you give me a suggestion for this issue?