Daniel Larsen
Daniel Larsen

Reputation: 3

Razor check if row exists

I am trying to do a check on the input data, to see if it already exists in the database.

I have tried this, but it did not help: Best method for determining if a row exists

I am still new to Razor, but getting better by the day :-)

Thank you!

Upvotes: 0

Views: 944

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could use the QueryValue method:

var db = Database.Open("SmallBakery");
var count = db.QueryValue("SELECT COUNT(*) FROM Product WHERE Id = @0", 123);
if (count > 0) {
    // the record exists
}

You will of course need to adapt the SQL query to match your database schema.

Useful read: http://www.asp.net/web-pages/overview/more-resources/asp-net-web-pages-api-reference

Upvotes: 2

Related Questions