Hello All,
I am trying to use If/Else statements to meet certain conditions, and I am stuck on 1 condition.
My web application displays a list of topics.
The topics listed will have Like/Dislike button visible if a user has not yet liked or disliked a topic, if User has liked/disliked a topic it will display 'You Like this' or 'You Dislike this' accordingly. Till here my code works just fine, now I want to display
Like/Dislike buttons on topics which user has not yet Liked or disliked.
How do I do this. Below is my Code
@{
var dbase = Database.Open("Rating");
var topic = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc");
}
<div>
@foreach (var row in topic)
{
<div style="border: 1px solid">
<b>Topic: </b>@row.Topic
<br />
<b>Likes: </b>@row.CountLikes
<br />
<b>DisLikes: </b>@row.CountDisLikes
<br />
@if(WebSecurity.IsAuthenticated){
var dbase2 = Database.Open("Rating");
var squery= dbase2.Query("SELECT * from LikeDislike");
foreach(var rows in squery)
{
if(@row.id==@rows.topicID && @rows.Clikes==true && WebSecurity.CurrentUserId==@rows.UserName)
{
<p style="color: #00ff90"><b>You Like this</b></p>
}
if(@row.id==@rows.topicID && @rows.Clikes==false && WebSecurity.CurrentUserId==@rows.UserName)
{
<p style="color: #f00"><b>You DisLike this</b></p>
}
}
}
else {
//onClick event to redirect user to login page
<button onclick="redirectbutton()">Like</Button>
<button onclick="redirectbutton()">DisLike</button>
}
<br /> <br />
</div>
}
</div>
Below is my SQL Database Table, with sample data.
Table Name: Rating
ID Topic CountLikes CountDisLikes Extra CreatedByUser
1 Do You Like This 211 58 YesId 2
2 Or This 17 25 This also 3
79 Testing at home 1 0 Testing at home 2
80 Testing at home again 1 0 Testing 2
82 testing dislikes 0 1 Testing 2
76 Testing part 3 7 5 Testing 3 4
77 Testing part 4 16 6 Testing 4 5
Try this example that gets the LikeDislike data just once rather than hitting the database multiple times to get the same data which is likely to produce poor performance:
@{
var dbase = Database.Open("Testsv2");
var topics = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc");
if(WebSecurity.IsAuthenticated){
var ratings = dbase.Query("SELECT TopicID, UserName, Clikes FROM LikeDislike WHERE UserName = @0", WebSecurity.CurrentUserId);
}
}
<div>
@foreach (var row in topics){
<div style="border: 1px solid">
<b>Topic: </b>@row.Topic<br />
<b>Likes: </b>@row.CountLikes<br />
<b>DisLikes: </b>@row.CountDisLikes<br />
@if(WebSecurity.IsAuthenticated){
@if(ratings.Any(r => r.UserName == 2 && r.TopicID == row.ID && r.Clikes)){
<p style="color: #00ff90"><b>You Like this</b></p>
}
@if(ratings.Any(r => r.UserName == 2 && r.TopicID == row.ID && !r.Clikes)){
<p style="color: #00ff90"><b>You Dislike this</b></p>
}
@if(ratings.FirstOrDefault(r => r.UserName == 2 && r.TopicID == row.ID) == null){
//onClick event to redirect user to login page
<button onclick="redirectbutton()">Like</Button>
<button onclick="redirectbutton()">DisLike</button>
}
}
</div>
}
</div>
I tried the code and got error "Unexpected "if" keyword after "@" character. Once inside code, you do not need to prefix constructs like "if" with "@". "
I remove the @ prefix from 'If' and then got the following error "CS0103: The name 'ratings' does not exist in the current context"
I tried your code and got the error "CS0103: The name 'topic' does not exist in the current context"
Sorry. Replace the first block of code with the following:
@{
IEnumerable<dynamic> topic;
var dbase = Database.Open("Rating");
if(WebSecurity.IsAuthenticated){
var topic = dbase.Query(@"SELECT t1.ID, t1.Topic, t1.CountLikes, t1.CountDisLikes, t1.Extra,
t1.CreatedByUser, t2.Clikes FROM Rating AS t1 LEFT JOIN (SELECT * FROM LikeDislike
WHERE UserName = @0) AS t2 ON t1.ID = t2.TopicID ORDER BY t1.ID desc", WebSecurity.CurrentUserId);
} else {
var topic = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc");
}
}
If you open a "New query" window in the Database workspace of your site, copy in it the following query
SELECT t1.ID, t1.Topic, t1.CountLikes, t1.CountDisLikes, t1.Extra,
t1.CreatedByUser, t2.Clikes FROM Rating AS t1 LEFT JOIN (SELECT * FROM LikeDislike
WHERE UserName = 3) AS t2 ON t1.ID = t2.TopicID ORDER BY t1.ID desc
and execute it, you achieve the best explanation of my code.
The result is your Rating table with any row enriched with the Clikes value for the authenticated user, if exists, or a null value.
The page displays any row of the query result accordingly.
@{
IEnumerable<dynamic> topic;
var dbase = Database.Open("Rating");
if(WebSecurity.IsAuthenticated){
var topic = dbase.Query(@"SELECT t1.ID, t1.Topic, t1.CountLikes, t1.CountDisLikes, t1.Extra,
t1.CreatedByUser, t2.Clikes FROM Rating AS t1 LEFT JOIN (SELECT * FROM LikeDislike
WHERE UserName = @0) AS t2 ON t1.ID = t2.TopicID ORDER BY t1.ID desc", WebSecurity.CurrentUserId);
} else {
var topic = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc");
}
}
Sorry - I tested the code without the if(Websecurity.IsAuthenticated) block so I forgot to remove the @ signs after I added that in.
Aafi
CS0103: The name 'ratings' does not exist in the current context
Like GianMaria's, my code block needs changing for the same reason:
@{
IEnumerable<dynamic> ratings = null;
var dbase = Database.Open("Testsv2");
var topics = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc");
if(WebSecurity.IsAuthenticated){
ratings = dbase.Query("SELECT TopicID, UserName, Clikes FROM LikeDislike WHERE UserName = @0", WebSecurity.CurrentUserId);
}
}
The SQL should be self-explanatory. The more complex part of my approach involves using LINQ to Objects to query the data obtained from the database in-memory:
This basically queries the data to see if there are any entries in the LikeDislike data for the current user that have the same topicID as the current record with "true" in the Clikes column. If so, the current user already liked it.
@{
IEnumerable<dynamic> topic;
var dbase = Database.Open("Rating");
if(WebSecurity.IsAuthenticated){
topic = dbase.Query(@"SELECT t1.ID, t1.Topic, t1.CountLikes, t1.CountDisLikes, t1.Extra,
t1.CreatedByUser, t2.Clikes FROM Rating AS t1 LEFT JOIN (SELECT * FROM LikeDislike
WHERE UserName = @0) AS t2 ON t1.ID = t2.TopicID ORDER BY t1.ID desc", WebSecurity.CurrentUserId);
} else {
topic = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc");
}
}
Thank you for the codes guys, I really appreciate it !
Mike,
Can you please explain the below 2 statements in little more detail, and also what does IEnumerable does, I would really appreciate if you can explain
it with the code in discussion.
Aafi
Member
32 Points
53 Posts
Need help with If/Else conditions
Jan 21, 2013 07:15 PM|LINK
Hello All,
I am trying to use If/Else statements to meet certain conditions, and I am stuck on 1 condition.
My web application displays a list of topics.
The topics listed will have Like/Dislike button visible if a user has not yet liked or disliked a topic, if User has liked/disliked a topic it will display 'You Like this' or 'You Dislike this' accordingly. Till here my code works just fine, now I want to display Like/Dislike buttons on topics which user has not yet Liked or disliked.
How do I do this. Below is my Code
@{ var dbase = Database.Open("Rating"); var topic = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc"); } <div> @foreach (var row in topic) { <div style="border: 1px solid"> <b>Topic: </b>@row.Topic <br /> <b>Likes: </b>@row.CountLikes <br /> <b>DisLikes: </b>@row.CountDisLikes <br /> @if(WebSecurity.IsAuthenticated){ var dbase2 = Database.Open("Rating"); var squery= dbase2.Query("SELECT * from LikeDislike"); foreach(var rows in squery) { if(@row.id==@rows.topicID && @rows.Clikes==true && WebSecurity.CurrentUserId==@rows.UserName) { <p style="color: #00ff90"><b>You Like this</b></p> } if(@row.id==@rows.topicID && @rows.Clikes==false && WebSecurity.CurrentUserId==@rows.UserName) { <p style="color: #f00"><b>You DisLike this</b></p> } } } else { //onClick event to redirect user to login page <button onclick="redirectbutton()">Like</Button> <button onclick="redirectbutton()">DisLike</button> } <br /> <br /> </div> } </div>Below is my SQL Database Table, with sample data.
Table Name: Rating
Table Name: LikeDislike
TopicID in LikeDislike table is FK to the ID table in Rating table.
Thank you All.
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Need help with If/Else conditions
Jan 21, 2013 09:28 PM|LINK
Try this example that gets the LikeDislike data just once rather than hitting the database multiple times to get the same data which is likely to produce poor performance:
@{ var dbase = Database.Open("Testsv2"); var topics = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc"); if(WebSecurity.IsAuthenticated){ var ratings = dbase.Query("SELECT TopicID, UserName, Clikes FROM LikeDislike WHERE UserName = @0", WebSecurity.CurrentUserId); } } <div> @foreach (var row in topics){ <div style="border: 1px solid"> <b>Topic: </b>@row.Topic<br /> <b>Likes: </b>@row.CountLikes<br /> <b>DisLikes: </b>@row.CountDisLikes<br /> @if(WebSecurity.IsAuthenticated){ @if(ratings.Any(r => r.UserName == 2 && r.TopicID == row.ID && r.Clikes)){ <p style="color: #00ff90"><b>You Like this</b></p> } @if(ratings.Any(r => r.UserName == 2 && r.TopicID == row.ID && !r.Clikes)){ <p style="color: #00ff90"><b>You Dislike this</b></p> } @if(ratings.FirstOrDefault(r => r.UserName == 2 && r.TopicID == row.ID) == null){ //onClick event to redirect user to login page <button onclick="redirectbutton()">Like</Button> <button onclick="redirectbutton()">DisLike</button> } } </div> } </div>Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
GmGregori
Contributor
5438 Points
730 Posts
Re: Need help with If/Else conditions
Jan 21, 2013 10:01 PM|LINK
I try to propose to you a solution with a more complex initial query but a simpler exam of the topics:
@{ var dbase = Database.Open("Rating"); if(WebSecurity.IsAuthenticated){ var topic = dbase.Query(@"SELECT t1.ID, t1.Topic, t1.CountLikes, t1.CountDisLikes, t1.Extra, t1.CreatedByUser, t2.Clikes FROM Rating AS t1 LEFT JOIN (SELECT * FROM LikeDislike WHERE UserName = @0) AS t2 ON t1.ID = t2.TopicID ORDER BY t1.ID desc", WebSecurity.CurrentUserId); } else { var topic = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc"); } } <div> @foreach (var row in topic) { <div style="border: 1px solid"> <b>Topic: </b>@row.Topic <br/> <b>Likes: </b>@row.CountLikes <br/> <b>DisLikes: </b>@row.CountDisLikes <br/> @if(WebSecurity.IsAuthenticated){ { switch((bool?)@row.Clikes) { case true: <p style="color: #00ff90"><b>You Like this</b></p> break; case false: <p style="color: #f00"><b>You DisLike this</b></p> break; default: <button onclick="redirectbutton()">Like</Button> <button onclick="redirectbutton()">DisLike</button> break; } } </div> } </div>Aafi
Member
32 Points
53 Posts
Re: Need help with If/Else conditions
Jan 22, 2013 03:24 PM|LINK
Hello Mike,
Thank you for the answer.
I tried the code and got error "Unexpected "if" keyword after "@" character. Once inside code, you do not need to prefix constructs like "if" with "@". "
I remove the @ prefix from 'If' and then got the following error " CS0103: The name 'ratings' does not exist in the current context"
And can you also please explain the code.
Warm Regards,
Aafi
Member
32 Points
53 Posts
Re: Need help with If/Else conditions
Jan 22, 2013 03:30 PM|LINK
Hello Gregori,
I tried your code and got the error "CS0103: The name 'topic' does not exist in the current context"
And can you also please explain your code.
Regards,
GmGregori
Contributor
5438 Points
730 Posts
Re: Need help with If/Else conditions
Jan 22, 2013 05:05 PM|LINK
Sorry. Replace the first block of code with the following:
@{ IEnumerable<dynamic> topic; var dbase = Database.Open("Rating"); if(WebSecurity.IsAuthenticated){ var topic = dbase.Query(@"SELECT t1.ID, t1.Topic, t1.CountLikes, t1.CountDisLikes, t1.Extra, t1.CreatedByUser, t2.Clikes FROM Rating AS t1 LEFT JOIN (SELECT * FROM LikeDislike WHERE UserName = @0) AS t2 ON t1.ID = t2.TopicID ORDER BY t1.ID desc", WebSecurity.CurrentUserId); } else { var topic = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc"); } }If you open a "New query" window in the Database workspace of your site, copy in it the following query
SELECT t1.ID, t1.Topic, t1.CountLikes, t1.CountDisLikes, t1.Extra, t1.CreatedByUser, t2.Clikes FROM Rating AS t1 LEFT JOIN (SELECT * FROM LikeDislike WHERE UserName = 3) AS t2 ON t1.ID = t2.TopicID ORDER BY t1.ID descand execute it, you achieve the best explanation of my code.
The result is your Rating table with any row enriched with the Clikes value for the authenticated user, if exists, or a null value.
The page displays any row of the query result accordingly.
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Need help with If/Else conditions
Jan 22, 2013 05:39 PM|LINK
I don't think you intended var topic...
Not that I'm in any position to find fault!
;o)
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Need help with If/Else conditions
Jan 22, 2013 05:45 PM|LINK
Sorry - I tested the code without the if(Websecurity.IsAuthenticated) block so I forgot to remove the @ signs after I added that in.
Like GianMaria's, my code block needs changing for the same reason:
@{ IEnumerable<dynamic> ratings = null; var dbase = Database.Open("Testsv2"); var topics = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc"); if(WebSecurity.IsAuthenticated){ ratings = dbase.Query("SELECT TopicID, UserName, Clikes FROM LikeDislike WHERE UserName = @0", WebSecurity.CurrentUserId); } }The SQL should be self-explanatory. The more complex part of my approach involves using LINQ to Objects to query the data obtained from the database in-memory:
This basically queries the data to see if there are any entries in the LikeDislike data for the current user that have the same topicID as the current record with "true" in the Clikes column. If so, the current user already liked it.
Same thing, but tests to see if there is a current matching record with "false" in the Clikes column.
If there are no matches, they haven't rated this topic.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
GmGregori
Contributor
5438 Points
730 Posts
Re: Need help with If/Else conditions
Jan 22, 2013 07:59 PM|LINK
I should apologize for the second time ....
@{ IEnumerable<dynamic> topic; var dbase = Database.Open("Rating"); if(WebSecurity.IsAuthenticated){ topic = dbase.Query(@"SELECT t1.ID, t1.Topic, t1.CountLikes, t1.CountDisLikes, t1.Extra, t1.CreatedByUser, t2.Clikes FROM Rating AS t1 LEFT JOIN (SELECT * FROM LikeDislike WHERE UserName = @0) AS t2 ON t1.ID = t2.TopicID ORDER BY t1.ID desc", WebSecurity.CurrentUserId); } else { topic = dbase.Query("SELECT ID, Topic, CountLikes, CountDisLikes from Rating ORDER BY ID desc"); } }Aafi
Member
32 Points
53 Posts
Re: Need help with If/Else conditions
Jan 23, 2013 01:25 PM|LINK
Thank you for the codes guys, I really appreciate it !
Mike,
Can you please explain the below 2 statements in little more detail, and also what does IEnumerable does, I would really appreciate if you can explain it with the code in discussion.
Thanks You.