试图在没有用户手动输入页面上的FK的情况下将FK插入表中ASP.NET / C#

时间:2018-05-03 22:43:54

标签: c# asp.net visual-studio

如标题中所述,我试图在没有用户手动输入页面上的FK的情况下将FK插入表中。我有一个ListView填充SELECT语句从我的数据库中的表中提取。如何开始,是用户从上一页中选择一个考试(在Session变量中保存考试表的PK)并进入Maintain_Questions.aspx页面,该页面将向他们展示专门为该考试创建的问题。我试图插入表中的FK是ExaminationID(我假设这是通过SessionParameter完成的)。目前我觉得我非常接近解决方案,也许只是在SqlDataSource中缺少一行,这使我无法正确插入问题。旁注:ListView确实将问题插入到问题表中,但是它没有插入适当的FK - 因此在数据库表中,ExaminationID被视为NULL。 谢谢你的帮助!

代码:

<asp:SqlDataSource runat="server" ID="sdsListViewQuestion"
    ConnectionString="<%$ConnectionStrings:OPT%>" 
    OldValuesParameterFormatString="original_{0}"
    SelectCommand="SELECT QuestionID, Question, CorrectAnswer, OptionA, OptionB, OptionC, OptionD, Examination.ExaminationID, Examination.Examination
                     FROM Question, Examination
                     WHERE Examination.ExaminationID = @ExaminationID
                     AND Examination.ExaminationID = Question.ExaminationID
                     ORDER BY Examination
                     ;"
     InsertCommand="INSERT
                      INTO Question
                         ( 
                           Question,
                           CorrectAnswer,
                           ExaminationID,
                           OptionA,
                           OptionB,
                           OptionC,
                           OptionD
                         )
                    VALUES
                         (
                           @Question,
                           @CorrectAnswer,
                           @ExaminationID,
                           @OptionA,
                           @OptionB,
                           @OptionC,
                           @OptionD                            
                         ) ;
                    SELECT ExaminationID = @ExaminationID;"
     UpdateCommand="UPDATE Question
                       SET Question = @Question,
                           CorrectAnswer = @CorrectAnswer,
                           ExaminationID = @ExaminationID,
                           OptionA = @OptionA,
                           OptionB = @OptionB,
                           OptionC = @OptionC,
                           OptionD = @OptionD
                     WHERE QuestionID = @original_QuestionID;" 
     DeleteCommand="DELETE
                      FROM Question
                     WHERE QuestionID = @original_QuestionID;">
    <SelectParameters>
        <asp:SessionParameter Direction="Input" Name="ExaminationID" SessionField="intExaminationID" Type="Int32" />
    </SelectParameters>
    <InsertParameters>
        <asp:SessionParameter Direction="Output" Name="ExaminationID" SessionField="intExaminationID" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
        <asp:SessionParameter Direction="Output" Name="ExaminationID" SessionField="intExaminationID" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource> 

1 个答案:

答案 0 :(得分:0)

发现我的问题,我建立的SessionParameter的方向设置为“输出”而不是输入参数的“输入”。调整这个允许我能够相应地将FK(ExaminationID)的正确会话变量输入到Question表中。