user1440862
user1440862

Reputation: 21

Retrieving Data in Webmatrix without 'foreach'

I want to retrieve a single item of data from my database to then use as a value for an input element. Using the below will retrieve each instance of SubmittedBy in the DB so results in multiple results.

var UserId = WebSecurity.CurrentUserId;

var db = Database.Open("testDB");
var selectQueryString = "Select SubmittedBy FROM Posts WHERE UserId=@0";

var data = db.Query(selectQueryString, UserId);

@foreach(var row in data)
{
    <input type="email" name="emailfrom" id="emailfrom" value="@SubmittedBy"/>  
}

How do I retrieve SubmittedBy so it only gives the one result i.e. without the foreach loop?

Thanks in advance!!!

Upvotes: 2

Views: 315

Answers (3)

Thomas Fonseca
Thomas Fonseca

Reputation: 542

There is a method created specifically for this purpose called QuerySingle - just change your query like this:

var data = db.QuerySingle(selectQueryString, UserId);

I hope this helps!

Upvotes: 1

mjsr
mjsr

Reputation: 7590

If by your data restriccions are you going to obtain 1 and only 1 value for an specific UserId, you could use

   var SubmyttedValue = db.QueryValue(selectQueryString, UserId);

Upvotes: 3

sebataz
sebataz

Reputation: 1043

Change your query like this:

Select SubmittedBy FROM Posts WHERE UserId=@0 LIMIT 1

Upvotes: 0

Related Questions