What you’re trying to do is not feasible. It’s not that you can’t do (it can’t even, directly) it’s that doing it would be a mistake. You are potentially provoking a running condition, that is, between a consultation and another may be that the condition is not the same. This is more or less like you checking if a file exists to open it. When opening it, it may no longer exist.
If you read what you wrote in the question you already have the solution: "I need to check if there is record in the table". Then do this. The variable filtro
is what you need to check? Then it’s ready, if it’s not, add to the SQL expression.
You should only do a count if you need the count. Clearly you’re saying you want something else. Apparently you’re hurting performance by thinking you’re getting better. You’re making robustness worse. And you’re doing something semantically wrong.
Even if I need the count later, I would still do it via C# code to avoid the mentioned race condition. But apparently not even this you need.
As much as you want to do it another way, you’re making a mistake. You may not like this answer but it’s the only one valid for your situation is this.
It is necessary that the count be made before to go through the results? Why?
– dcastro
So dcastro. The need for counting is to check if there are records, otherwise a message will appear "There are no records at the moment".
– user24136
I understand the need to count, my question was not about that. The question was about the need for that count to be made before the results (and not afterward or during).
– dcastro