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?
RossLiversidgeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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

0
AndyAinscowFreelance programmer / ConsultantCommented:
Does this work?
var dataitem = db.QuerySingle("SELECT * FROM ArticlePages WHERE ArticleID=" + articleID + " AND Page=" + pageID);
0
Barry CunneyCommented:
var dataitem = db.QuerySingle("SELECT * FROM ArticlePages WHERE ArticleID=@0 AND Page=@1", articleID, pageID);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RossLiversidgeAuthor Commented:
Perfect.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.