Ciaran Donoghue
Ciaran Donoghue

Reputation: 828

Retrieve ID from database in mvc

I am trying to retrieve a specific id from a table so that I can get the correct record in my database and use that information. Is there anyway where I can get the actually numerical value of the id from the table. I have tried using linq to SQL but it just queries the database and returns the string like this:

Dim id = From data In dbServer.UserTables Where data.Username = user.username And data.userPassword = user.userPassword Select data.UserID

It does work because then if I count id if it is greater than 1 it will work. But trying to send the value on for example in session state will only have "From data In dbServer.UserTables Where data.UserID = user.username And data.userPassword = user.userPassword Select data.UserID" instead of the actual number. How can I do this correctly?

Upvotes: 0

Views: 57

Answers (1)

thomasb
thomasb

Reputation: 6035

A Linq query will return a set of data, even if there is only one result.

This will get you an integer (possibly a nullable integer, I can't test right now), that you will have to check for value different than 0 (or null, if it's a nullable).

Dim id = (From data In dbServer.UserTables Where data.UserID = user.username And data.userPassword = user.userPassword Select data.UserID).FirstOrDefault()

Upvotes: 1

Related Questions