mhcodner
mhcodner

Reputation: 563

Webmatrix recognises the wrong "}"

My page consists of the following code:

@{
var db= Database.Open("Surgestuff");
var sqlQ = "SELECT * FROM Comments";
var data = db.Query(sqlQ);
Page.Title = "Add Comment"; 
}
@{
    var CommenterrorMessage = "";
    var LengtherrorMessage = "";
    var date = @DateTime.Now;
        if (IsPost) {
        var CommName = "";
        var Comment = "";
        CommName=Request["formName"];
        Comment=Request["formComm"];
        var Dt = @DateTime.Now;
        var isValid = true;
        if (Comment.IsEmpty()){
            CommenterrorMessage = "This field is required";
            isValid = false;
        }

        else if (Comment.Length > 400){
            LengtherrorMessage = "Comment must be less than 400 characters";
            isValid = false;
        }

        else if (CommName.IsEmpty()){
            CommName = "Anonymous";
        }

        if (isValid){
        var SQLINSERT = "INSERT INTO Comments (Name, Comment, Dt) " + "VALUES (@0, @1, @2)";
        db.Execute(SQLINSERT, CommName, Comment, Dt);
        Response.Redirect("~/Comments");
        }

        else
        {
            <p class="message error">Please correct the errors and resubmit the form.</P> 
        }
    }
}
<fieldset><legend>Add Comment</legend>
<form action="" method="post" enctype="multipart/form-data">
  Name:
  <p><input type="text" id="Name" name="formName"/></p>
  Comment:
  <p><textarea cols="35" rows="5" id="Comment" name="formComm"/></textarea></p>
     @if(!CommenterrorMessage.IsEmpty()) {
        <label for="formComm" class="validation-error">
            @CommenterrorMessage
        </label>
    }
     @if(!LengtherrorMessage.IsEmpty()) {
        <label for="formComm" class="validation-error">
            @LengtherrorMessage
        </label>
    }

  <p><input type="submit" value="Add Comment" /></p>
</form>
</fieldset>

And when i try to run this page it gives me the error:

' "2" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid. ' When i look at the files workspace in webmatrix it recognises the first closing "}" as the end for the whole code block then disregards the rest until it gets to "@2". I have had the exact same code working on my website before i upgraded to webmatrix 2.0. How can i fix this?

Upvotes: 1

Views: 189

Answers (2)

Tohid
Tohid

Reputation: 6679

I finally copy/pasted your code into WebMatrix (on my Virtual Box). You have 2 @DateTime.Nows inside your Razor @{ ... } code. if you change them to:

... = DateTime.Now;

it solves the problem.

Upvotes: 1

Tohid
Tohid

Reputation: 6679

In Razor, anywhere you need to use @ as itself (not Razor indicator), use: @@.

So Try: @@0, @@1, @@2.

Source: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

Upvotes: 0

Related Questions