locked
Braces error occur in razor v2 while using 2 foreach in a single if condition RRS feed

  • Question

  • User774838899 posted

    Hi, here is my code where in trying to validate the blocked users from my user search engine. But whenere i add blocked user check in else part braces error occur even i closed all braces properly.

    Here is my code:

    @if (IsPost && Request["submit"] != null) //Search users engine
    {
    var searchUser = "select * from Profile where FName LIKE '%" + Request["text"] + "%' OR LName LIKE '%" + Request["text"] + "%' ";
    <div class="FriendRequestWraper">
    <h2>Searched Users List</h2>
    @foreach (var data in db.Query(searchUser)) //this foreach is used to check information of search user profile exist or not
    {
    pic = @data.ProfileImage;
    userid = @data.UserID;
    fname = @data.FName;
    lname = @data.LName;
    }
    @if (userid == "") //user not exist
    {
    <div class="position">
    <div class="no_result">
    <p>No User Found</p>
    </div>
    </div>
    }
    else
    {
    var id = ""; var blockuserid = "";
    var CheckBlockedUser = "select * from Report where (Sender='" + @WebSecurity.CurrentUserName + "' AND SendedFor='"+Request["userid"]+"') AND IsBlock='1'";
    foreach (var user in db.Query(CheckBlockedUser))
    {
    id = @user.Sender;
    blockuserid = @user.SendedFor;
    }
    @if(id != "" && blockuserid != "") //check for block user
    {
    <font>You are Block for this User</font>
    }
    else
    {
    foreach (var value in db.Query(searchUser))
    {
    <div id="body-right">
    <div class="box">
    <img src="../@value.ProfileImage" width="130" height="86" alt="Pic 1" class="left" />
    <p><a href="#">@value.FName @value.LName</a></p>
    <form method="post" action="">
    <input type="hidden" value="@value.UserID" name="addFriend" />
    <input type="submit" class="btn" name="Add Friend" value="Add Friend" />
    </form>
    <div class="clear"></div>
    </div>
    </div>
    }
    }

    }
    </div>
    }

    Sunday, September 13, 2015 12:23 PM

Answers

  • User1313602441 posted
    I think you should not place your QueryString directly in your SQL statements. Aside from the fact that it's not safe, it sometimes is confusing.
    Try putting the QueryStrings in different variables then pass them as values (@0, @1, etc) to the SQL statement.
    For your second question, the "@" is used to render Razor code directly into HTML (inline code).
    For example:
    <h1>Signed in as @username</h1>
    While "@{...}" is used for Razor code blocks.
    For example:
    @{
    var num1 = 25;
    var num2 = 50;
    var saysumtin = "The numbers were:";
    if (num1 > num2)
    {
    <p>@saysumtin
    Num1: @num1 and Num2: @num2 <br />
    Num1 is greater than Num2</p>
    }
    else
    {
    <p>@saysumtin
    Num1: @num1 and Num2: @num2 <br />
    Num2 is greater than Num1</p>
    }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 14, 2015 3:13 PM

All replies

  • User753101303 posted

    Hi,

    Delete @ before WebSecurity.CurrentUserName and on the if that follows. Does it work?

    Sunday, September 13, 2015 3:23 PM
  • User774838899 posted

    Yup it works braces error is removed but now values are not passing in foreach loop of checkBlockedUser ... dont know why ?

    i also want to ask another question What is relation between braces and @, i mean why braces error occur by using @ sign ?

    Thanks

    Monday, September 14, 2015 1:21 AM
  • User1313602441 posted
    I think you should not place your QueryString directly in your SQL statements. Aside from the fact that it's not safe, it sometimes is confusing.
    Try putting the QueryStrings in different variables then pass them as values (@0, @1, etc) to the SQL statement.
    For your second question, the "@" is used to render Razor code directly into HTML (inline code).
    For example:
    <h1>Signed in as @username</h1>
    While "@{...}" is used for Razor code blocks.
    For example:
    @{
    var num1 = 25;
    var num2 = 50;
    var saysumtin = "The numbers were:";
    if (num1 > num2)
    {
    <p>@saysumtin
    Num1: @num1 and Num2: @num2 <br />
    Num1 is greater than Num2</p>
    }
    else
    {
    <p>@saysumtin
    Num1: @num1 and Num2: @num2 <br />
    Num2 is greater than Num1</p>
    }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 14, 2015 3:13 PM
  • User774838899 posted

    Thanks Abubad

    Monday, September 14, 2015 8:54 PM