在我们的调查应用程序中,当用户登录并参加我们的某项调查时,下面的代码应该根据调查对象和surveyId对调查进行评分。
这些被声明为会话变量。
但是,由于某些原因,代码无法识别surveyId或正在进行此调查的用户的用户名。
因此,它显示每个用户的重复答案。
我试图发布截图,但我遇到了困难。
任何想法我可能做错了什么?
'//first the surveyId and username are declared in pageLoad event:
If Session("UserName") Is Nothing Or Session("Username") = "" Then
Response.Redirect("~/Login.aspx?redirect=List.aspx")
Else
userLB.Text = "You are logged in as " & Session("FullName")
userLB.ForeColor = System.Drawing.Color.DarkOrange
End If
'//然后是gridview中的代码:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="WITH CorrectChoices AS
(
SELECT Distinct QuestionID, LEFT(CorrectChoice , LEN(CorrectChoice )-1) AS Choice
FROM SurveyChoices AS extern
CROSS APPLY
(
SELECT Choice + ', '
FROM SurveyChoices AS intern
WHERE extern.QuestionID = intern.QuestionID
AND intern.IsCorrect=1
ORDER BY Choice
FOR XML PATH('')
) pre_trimmed (CorrectChoice)
GROUP BY QuestionID, CorrectChoice
)
, Answer AS
(
SELECT QuestionID, UserName, LEFT(CorrectChoice , LEN(CorrectChoice )-1) AS Choice
FROM SurveyAnswers AS extern
CROSS APPLY
(
SELECT Choice + ', '
FROM SurveyAnswers intern
INNER JOIN SurveyChoices SC ON SC.QuestionId = intern.QuestionID
AND SC.ChoiceID = intern.ChoiceID
WHERE extern.QuestionID = intern.QuestionID
ORDER BY Choice
FOR XML PATH('')
) pre_trimmed (CorrectChoice)
GROUP BY QuestionID, UserName, CorrectChoice
)
SELECT SQ.Question, CC.Choice, A.Choice Answer,
CASE WHEN CC.Choice = A.Choice THEN 'Correct' ELSE 'Wrong' END AS Status
FROM SurveyQuestions SQ
LEFT JOIN CorrectChoices CC ON CC.QuestionID = SQ.QuestionID
LEFT JOIN Answer A ON A.QuestionID =SQ.QuestionID
WHERE SQ.SurveyID = @SurveyId AND UserName=@UserName">
<SelectParameters>
<asp:SessionParameter Name="UserName" SessionField="UserName" />
<asp:SessionParameter Name="SurveyId" SessionField="SurveyId" />
</SelectParameters>
</asp:SqlDataSource>
答案 0 :(得分:0)
我已经解决了这个问题。
只需添加intern.UserName = @Username即可将问题修复为内部查询。
SELECT Choice +',' 来自SurveyAnswers实习生 INNER JOIN SurveyChoices SC ON SC.QuestionId = intern.QuestionID AND SC.ChoiceID = intern.ChoiceID 和intern.UserName = @UserName WHERE extern.QuestionID = intern.QuestionID