You don't state this in your question, so I assume that you're not getting any results back from the query as it is?
Probably there are simply no Tweets with IDs strictly greater and strictly smaller than the range you're providing where the TweetText
matches. You could debug to see what the values for StartPoint
and EndPoint
are and if there really is an ID in the database that's strictly between them where the TweetText
matches.
You could change your statement as follows to include the values for StartPoint
and EndPoint
.
select ID from Tweets
where @VsTweetText = TweetText
and ID <= @VsEndPoint
and ID >= @VsStartPoint
or even
select ID from Tweets
where @VsTweetText = TweetText
and ID BETWEEN @VsStartPoint AND @VsEndPoint
Please also note that the statement above may very well return more than one record, but you're using ExecuteScalar
in your code, so you're using the first ID you encounter - randomly. If you're really only interested in one ID, you should modify your query so that it is more predictable which one is chosen.
Maybe like this:
select top 1 ID from Tweets
where @VsTweetText = TweetText
and ID BETWEEN @VsStartPoint AND @VsEndPoint
order by ID desc
Which would return the biggest ID found within the range.