I have done this before but this time it's just not working.
All I am trying to do is COUNT() the number of unread messages there are in a table.
Table def:
UserId(int), From(int), Type(nvarchar), Message(nvarchar), Read(bit)
The Read and UserId
columns are what I"m trying to check against. I have tried the following:
database.QuerySingle("SELECT COUNT(*) AS NumberOfNotifications FROM Notifications WHERE UserId = @0 AND Read = CONVERT(bit,0)", userid);
I have also tried many other versions but it's always the same result. I always get the following error:
Server Error in '/' Application.
Incorrect syntax near the keyword 'Read'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'Read'.
What I am trying to do, is this: "GET NUMBER OF UNREAD MESSAGES IN NOTIFICATIONSTABLE WHERE USERID = @0 AND READ = FALSE"
Any help at all is appreciated!
Thank you
UPDATED
Below is full code of the helper I am experiencing problems with. Now that I have got the error fixed up, I don't know why it's not displaying the "You have new notifications message" when i sign in - there are 4 rows in the corresponding table.
@helper RetrievePhotoWithName(int userid)
{
var database = Database.Open("DUDE");
var name = database.QuerySingle("select FirstName, LastName, ProfilePicture from UserProfile where UserId = @0", userid);
var notifications = database.QuerySingle("SELECT COUNT(*) AS NumberOfNotifications FROM Notifications WHERE UserId = @0 AND [Read] = @1", userid, false);
var DisplayName = "";
if(notifications["NumberOfNotifications"] < 1)
{
DisplayName = name["FirstName"] + " " + name["LastName"];
}
else
{
DisplayName = name["FirstName"] + ", you have " + notifications["NumberOfNotifications"] + " new messages.";
}
<a href="@Href("~/Home")" title="My Account"><img src="@Href("~/Shared/Assets/Images/" + name["ProfilePicture"] + ".png")" id="MiniProfilePicture" /> @DisplayName</a>
database.Close();
}