Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top