很抱歉再次打扰您。
在我们的调查应用程序上,当用户登录并参加我们的一项调查时,下面的代码应该根据参加调查的用户和调查 ID 对调查进行评分。
这些被声明为会话变量。
但是,由于某种原因,该代码无法识别调查 ID 或参与此调查的用户的用户名。
结果,它为每个用户显示重复的答案。
我正在尝试发布屏幕截图,但我遇到了困难。
有什么想法我可能做错了吗?
'//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>