cavill
cavill

Reputation: 610

Cannot get the current user ID from an SQL statement using Razor in WebMatrix

I'm learning razor syntax, using the startersite in WebMatrix and trying to display some data from a table. I want to select only the data from the logged in user, so in effect want to say

...WHERE UserId [email protected]";

but that doesn't work.

Using the @0 method doesn't work for me either - I get an 'A parameter is missing. [ Parameter ordinal = 1 ]' error.

Do I have to set a variable then use the @0 / @1 / @whichever way? If so - what's wrong with this code?

@{
//Is the user logged in?
WebSecurity.RequireAuthenticatedUser();

//Get the user's data

var TheUser = WebSecurity.CurrentUserId;

   var db= Database.Open("StarterSite");
   var sqlQ = "SELECT * FROM Days WHERE UserId =@0";
   var data = db.Query(sqlQ);
   }

I'm sure this is very much a beginners issue but I'm just trying to get started. Any help greatly appreciated.

Upvotes: 0

Views: 1060

Answers (1)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196207

You need to pass the value to the query
documentation

var data = db.Query(sqlQ, TheUser);

Upvotes: 1

Related Questions