locked
help with code RRS feed

  • Question

  • User-2060576634 posted

    hi.. does anyone know whats wrong with this code on line 12 ? thanks

    @{
        Layout = "~/_SiteLayout.cshtml";
        Page.Title = "first exam";
     
        var db = Database.Open("Azmoon");
        var selectquestions = "SELECT * From Questions WHERE ExamId=1";
        var userid = WebSecurity.CurrentUserId;
        //  List to hold any error or update messages and error flag.
    //        List<string> statusMessages = new List<string>();
    //        bool errorOccurred = false;
                // Make sure user doesn't have a row in the answers table for this Exam.
                var existingrow = db.Query("SELECT * FROM Answers" + "WHERE UserId = @0 AND ExamId= @1",@userid,1);
     
                 if (existingrow.Count() == 0)
                 { 
                     //add row to table
                                if(IsPost)
                                    {
                                       foreach(var row in db.Query(selectquestions))
     	                                 {
    		                                var questionid = row.QuestionId;
    	                                	var answer = Request["Q" + row.QuestionId.ToString()];
                                            var insertanswers = "INSERT into Answers (UserId,QuestionId,Answer,ExamId) VALUES(@0, @1, @2, @3)";
    	        	                        db.Execute(insertanswers, @userid, @questionid, @answer,1);
    			                          }
                       
     //                                          statusMessages.Add("You have successfully submitted your answers"); 
                                    }
                            
     //                   else
     //                   {
     //                       errorOccurred = true;
    //                        statusMessages.Add("ERROR: you have already taken this exam before");
                          }
                   
    // Set the color value and display style for the status or error messages.
    //     string errorColor = errorOccurred ? "color:#ff0000;" : "color:#000000;";
    //     string showStatus = statusMessages.Count() > 0 
    //                       ? "display:block;" : "display:none;";         
     
     
    }
    Thursday, January 22, 2015 3:32 PM

Answers

  • User-431565850 posted
     var existingrow = db.Query("SELECT * FROM Answers" + "WHERE UserId = @0 AND ExamId= @1",@userid,1);

    You are missing a space in your query between Answers and Where.

    this "SELECT * FROM Answers" + "WHERE UserId = @0 AND ExamId= @1"  = this "SELECT * FROM AnswersWHERE UserId = @0 AND ExamId= @1"

    So change it to this

    var existingrow = db.Query("SELECT * FROM Answers WHERE UserId = @0 AND ExamId= @1",@userid,1);

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2015 3:58 PM
  • User-431565850 posted

    It does. Look at the space between MemberRides and "

    Also, the only reason they put the "+" in there was to make it fit in a smaller width for easier reading. You don't need to do that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2015 4:09 PM
  • User1853794821 posted

    No

     var existingrow = db.Query("SELECT * FROM Answers" + "WHERE UserId = @0 AND ExamId= @1",@userid,1);
    should be
     var existingrow = db.Query("SELECT * FROM Answers" + "WHERE UserId = @0 AND ExamId= @1",userid,1);
    
    C# will not confuse the variable name "userid" with a column name of "UserId" in the query string.  They are two totally different objects in two totally different environments.
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2015 5:49 PM

All replies

  • User-431565850 posted
     var existingrow = db.Query("SELECT * FROM Answers" + "WHERE UserId = @0 AND ExamId= @1",@userid,1);

    You are missing a space in your query between Answers and Where.

    this "SELECT * FROM Answers" + "WHERE UserId = @0 AND ExamId= @1"  = this "SELECT * FROM AnswersWHERE UserId = @0 AND ExamId= @1"

    So change it to this

    var existingrow = db.Query("SELECT * FROM Answers WHERE UserId = @0 AND ExamId= @1",@userid,1);

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2015 3:58 PM
  • User-2060576634 posted

    thanks but why doesn't this code from the soccer tutorial have a space?

    // Check if user has a row in the MemberRides table for this match.
              var existingRow = db.Query("SELECT * FROM MemberRides "
                  + "WHERE MemberId = @0 AND MatchId = @1", 
                  userId, match.matchId);
              if (existingRow.Count > 0)
              {
    Thursday, January 22, 2015 4:05 PM
  • User-431565850 posted

    It does. Look at the space between MemberRides and "

    Also, the only reason they put the "+" in there was to make it fit in a smaller width for easier reading. You don't need to do that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2015 4:09 PM
  • User1853794821 posted

    Also, you might want to remove the "@" from "@userId" in the query parameters

    Thursday, January 22, 2015 4:41 PM
  • User-2060576634 posted

    doesn't it get confused with the UserId column?

    Thursday, January 22, 2015 5:43 PM
  • User1853794821 posted

    No

     var existingrow = db.Query("SELECT * FROM Answers" + "WHERE UserId = @0 AND ExamId= @1",@userid,1);
    should be
     var existingrow = db.Query("SELECT * FROM Answers" + "WHERE UserId = @0 AND ExamId= @1",userid,1);
    
    C# will not confuse the variable name "userid" with a column name of "UserId" in the query string.  They are two totally different objects in two totally different environments.
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2015 5:49 PM