Link to home
Start Free TrialLog in
Avatar of RossLiversidge
RossLiversidge

asked on

SQL query using C# Razor using 2 variables

Hi all,

I'm trying to open a record from a database where 2 variables match (article ID and Page number). However, the query is ignoring the second variable (page number). I'm sure it's something simple, but I can't get it to fire.

var dataitem = db.QuerySingle("SELECT * FROM ArticlePages WHERE ArticleID=@0", articleID, "AND Page=@0",pageID);

Open in new window


It's not working when I hard-code the second variable either:

 var dataitem = db.QuerySingle("SELECT * FROM ArticlePages WHERE ArticleID=@0", articleID, "AND Page='2'");

Open in new window


The record always opens on page 1. Am I missing something staggeringly obvious?
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

I'm not that familiar with C#, but I'll give this a whack..

C# (at least in SSIS C# scripting) uses a + to concatenate a string
Numbers are not delineated with anything, such as what's currently in articleID (Other than @0??), and I believe strings are delineated with single tick marks ', so if page is not a string then this might be the problem.
add a space before the AND so SQL treats it correctly.
var dataitem = db.QuerySingle("SELECT * FROM ArticlePages WHERE ArticleID=@0" + articleID + " AND Page='2'");

Open in new window

Does this work?
var dataitem = db.QuerySingle("SELECT * FROM ArticlePages WHERE ArticleID=" + articleID + " AND Page=" + pageID);
ASKER CERTIFIED SOLUTION
Avatar of Barry Cunney
Barry Cunney
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RossLiversidge
RossLiversidge

ASKER

Perfect.