1

I have a view in my DB and the view has a row I am trying to search for. I've tested it in sql server and it returned the correct result. However when I try it with parameters from vb it won't return anything. The Sql code that I get a query to return a correct result looks like

SELECT * 
FROM 
   (SELECT 
       ROW_NUMBER() OVER (ORDER BY groupID DESC) AS Row, * 
    FROM 
       SchedulingGroup_VIEW 
    WHERE 
       (scheduled = 1) 
       AND ((building LIKE '%dunn%') OR (room LIKE '%dunn%') 
            OR (requestBy LIKE '%dunn%') OR (requestFor LIKE '%dunn%') 
            OR (groupID LIKE '%dunn%') OR (description LIKE '%dunn%')) 
       AND (NOT EXISTS (SELECT gID FROM facilitiesForm 
                        WHERE facilitiesForm.gID <> gID))) AS TMP 
WHERE 
    (Row BETWEEN 0 AND 100)

The SQL with parameter looks like

SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY groupID DESC) AS Row, * 
FROM schedulingGroup_VIEW 
WHERE (scheduled = 1) AND 
( (building LIKE '%' + @search + '%') 
   OR (room LIKE '%' + @search + '%') 
   OR (requestBy LIKE '%' + @search + '%') 
   OR (requestFor LIKE '%' + @search + '%') 
   OR (groupID LIKE '%' + @search + '%') 
   OR (description LIKE '%' + @search + '%')) 
AND 
  (NOT EXISTS (SELECT gID FROM facilitiesForm 
     WHERE facilitiesForm.gID <> gID))) AS TMP WHERE (Row BETWEEN 0 AND 100)

sqlComm.Parameters.AddWithValue("@search", info.search)

with info.search = "dunn".

The sql query returns the appropriate row but the vb.net with parameters returns nothing.

1 Answer 1

2

Move your wildcards (i.e. your % characters) into your VB.net string before you pass it as a parameter.

For example, do this in VB.net code...

sqlComm.Parameters.AddWithValue("@search", "%" + info.search + "%");

And in your SQL when you use the @search parameter in the LIKE statement, don't add in wildcards, like shown below...

WHERE building LIKE @search

See this SO post which is essentially the same question...

How to use wildcards in SQL query with parameters

Sign up to request clarification or add additional context in comments.

3 Comments

I'm up voting this and placing it as the answer. I have seen the parameters set both ways though. it also helps to make the test db mirror the live. Thanks.
This is better though as it prevents SQL injection.
Do I also have the same issue?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.