Working on my first Web Pages project using C# and Webmatrix. So for thing have been going pretty well but have now run into an issue that has me stumped. What I am doing is posting a form to this page which contains a message and I am then saving the message to the DB (and will later also get the email part going).
I've posted the code below, it's actually longer than need be to illustrate the problem but thought I'd post it all nonetheless. WebMatrix was already highlighting an issue at hand before I tried running it when I saw that the penultimate closing curly brackets was highlighted yellow. When running the code (and the post values are all OK) I get the error "CS1513: } expected". I initially thought I had by mistake added an extra closing curly bracket, but after checking and double checking and then slowly rebuilding up the code line by line to identify after which line it started going wrong, I have now come to the conclusion that without any of the nested "if" and "foreach" statements within the "if(IsPost && WebSecurity.IsAuthenticated)" "if" statement that it then "works" (although obviously not with the desired logic).
I've checked around and I've found plenty of C# code examples where this is done. Any help, pointers or anything else much appreciated.
Q
@{
if(IsPost && WebSecurity.IsAuthenticated){
var fromEmail = WebSecurity.CurrentUserName;
var fromUserId = @WebSecurity.CurrentUserId;
var message = Request["message"];
var uid = Request["uid"];
bool multiSend = false;
var db = Database.Open("Tennegize");
var skillLevel = 0;
if(Request["multi"]=="1"){
multiSend = true;
}
//Save to message to BD
var insertQuery = "INSERT INTO Messages (UserEmail, UserId, Message) VALUES (@0, @1, @2)";
db.Execute(insertQuery, fromEmail, fromUserId, message);
decimal messageId = db.GetLastInsertId();
if(multiSend){
//Get email address' from all recipients
var sql2 = "SELECT Email FROM UserProfile WHERE SkillLevel = @0";
var data2 = db.Query(sql, skillLevel);
var totalRecipents = data2.Count();
//Log all recipients who will recieve this notification
foreach(var recipient in data2){
insertQuery = "INSERT INTO Message_Recipients (UserId, UserEmail, MessageId) VALUES (@0, @1, @2)";
db.Execute(insertQuery, uid, data.Email, messageId);
}
}else{
// Get to recipients email address
var sql = "SELECT Email FROM UserProfile WHERE UserId = @0";
var data = db.QuerySingle(sql, uid);
//Log the recipient of this norification
insertQuery = "INSERT INTO Message_Recipients (UserId, UserEmail, MessageId) VALUES (@0, @1, @2)";
db.Execute(insertQuery, uid, data.Email, messageId);
}
}
}
My guess is that the line var fromUserId = @WebSecurity.CurrentUserId; is causing the problem. Remove the @ before WebSecurity.CurrentUserId.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With