2017-09-15 41 views
0

我有一個使用SQL數據源綁定它的中繼器,我試圖將用戶選擇 - 某些標籤值插入到數據庫中。將重複項目保存到SQL數據庫

我想弄清楚爲什麼代碼只插入一條記錄到數據庫,而它應該插入所有的中繼器值。

非常感謝您的幫助。

背後

代碼:

Public Sub InsertFuncation() 

    ' Define data objects 
    Dim myConn As SqlConnection 
    Dim cmd As SqlCommand 
    Dim sqlstring As String 

    ' Read the connection string 
    myConn = New SqlConnection("xxx") 

    ' Create command 
    sqlstring = "INSERT INTO tbl_QZ_AgentResults (Username, ExamID, QuestionID, Question_CorrectAnswer, Question_UserSelection, Question_Score, TRN_AddedOn) VALUES (@Agnt_LdnUsername, @ExamID, @QuestionID, @Question_CorrectAnswer, @Question_UserSelection, @Question_Score, @TRN_AddedOn)" 
    cmd = New SqlCommand(sqlstring, myConn) 
    For Each rpItem As RepeaterItem In Repeater1.Items 

     Dim QID As Label = TryCast(rpItem.FindControl("QuestionIDLabel"), Label) 
     Dim QCorrectAnswer As Label = TryCast(rpItem.FindControl("lbl_CORRECTANS"), Label) 
     Dim QUserAnswer As Label = TryCast(rpItem.FindControl("lbl_USERANS"), Label) 
     Dim QScore As Label = TryCast(rpItem.FindControl("lbl_QSCORE"), Label) 

     'Perform your insert operation. 

     ' Add command parameters 
     cmd.Parameters.Add("@Username", Data.SqlDbType.NVarChar) 
     cmd.Parameters("@Username").Value = User.Identity.Name.Remove(0, 11) 

     cmd.Parameters.Add("@ExamID", Data.SqlDbType.Int) 
     cmd.Parameters("@ExamID").Value = Request.QueryString("ExamID").ToString 

     cmd.Parameters.Add("@TRN_AddedOn", Data.SqlDbType.DateTime) 
     cmd.Parameters("@TRN_AddedOn").Value = DateTime.Now 

     If QID IsNot Nothing Then 
      cmd.Parameters.Add("@QuestionID", Data.SqlDbType.Int) 
      cmd.Parameters("@QuestionID").Value = QID.Text 
     End If 

     If QCorrectAnswer IsNot Nothing Then 
      cmd.Parameters.Add("@Question_CorrectAnswer", Data.SqlDbType.Int) 
      cmd.Parameters("@Question_CorrectAnswer").Value = QCorrectAnswer.Text 
     End If 

     If QUserAnswer IsNot Nothing Then 
      cmd.Parameters.Add("@Question_UserSelection", Data.SqlDbType.Int) 
      cmd.Parameters("@Question_UserSelection").Value = QUserAnswer.Text 
     End If 

     If QScore IsNot Nothing Then 
      cmd.Parameters.Add("@Question_Score", Data.SqlDbType.Int) 
      cmd.Parameters("@Question_Score").Value = QScore.Text 
     End If 

     ' Enclose database code in Try-Catch-Finally 
     Try 
      ' Open the connection 
      myConn.Open() 
      ' Execute the command 
      cmd.ExecuteNonQuery() 
      ' Display success message 
      MsgBox("Entered Successfully !", Me.Page, Me) 
      'ClearInputs(Page.Controls) 
     Catch ex As Exception 
      ' Display error message 
      MsgBox("Error !", Me.Page, Me) 
     Finally 
      ' Close the connection 
      myConn.Close() 
      Response.Redirect("GetResult.aspx?ExamID=" & Request.QueryString("ExamID")) 
     End Try 
     ' End If 
    Next 
End Sub 

的.aspx代碼

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">    
     <ItemTemplate> 
       <asp:Label ID="Label1" runat="server" Text="QuestionID:" Visible="false"></asp:Label> 
       <asp:Label ID="QuestionIDLabel" runat="server" Text='<%# Eval("QuestionID") %>' Visible="false"/> 
       <br /> 
       <asp:Label ID="Label3" runat="server" Text="ExamID:" Visible="false"></asp:Label> 
       <asp:Label ID="ExamIDLabel" runat="server" Text='<%# Eval("ExamID") %>' Visible="false"/> 
       <br /> 
       <asp:Label ID="Label2" runat="server" Text="Q:"></asp:Label> 
       <asp:Label ID="QuestionTextLabel" runat="server" Text='<%# Eval("QuestionText") %>' /> 
       <br /> 

      <table> 
       <tr> 
        <td rowspan="4"> 
         <asp:RadioButtonList ID="rb_SELECTANSWER" runat="server" AutoPostBack="True"> 
          <asp:ListItem Text="" Value="1"></asp:ListItem> 
          <asp:ListItem Text="" Value="2"></asp:ListItem> 
          <asp:ListItem Text="" Value="3"></asp:ListItem> 
          <asp:ListItem Text="" Value="4"></asp:ListItem> 
         </asp:RadioButtonList> 
        </td> 
        <td> 
        <asp:Label ID="Label4" runat="server" Text=""></asp:Label> 
        <asp:Label ID="Q_Answer_1Label" runat="server" Text='<%# Eval("Q_Answer_1") %>' /> 
        <br /> 
        </td> 
       </tr> 
       <tr> 
        <td> 
        <asp:Label ID="Label9" runat="server" Text=""></asp:Label> 
        <asp:Label ID="Label10" runat="server" Text='<%# Eval("Q_Answer_2") %>' /> 
        <br /> 
        </td> 
       </tr> 
       <tr> 
        <td> 
        <asp:Label ID="Label11" runat="server" Text=""></asp:Label> 
        <asp:Label ID="Label12" runat="server" Text='<%# Eval("Q_Answer_3") %>' /> 
        <br /> 
        </td> 
       </tr> 
       <tr> 
        <td> 
        <asp:Label ID="Label13" runat="server" Text=""></asp:Label> 
        <asp:Label ID="Label14" runat="server" Text='<%# Eval("Q_Answer_4") %>' /> 
        <br /> 
        </td> 
       </tr> 
       <tr> 
        <td> 
        <asp:Label ID="lbl_CORRECTANS" runat="server" Text='<%# Eval("Q_Correct_Ans") %>' /> 
        </td> 
        <td> 
        <asp:Label ID="lbl_USERANS" runat="server" Text=""></asp:Label> 
        </td> 
        <td> 
        <asp:Label ID="lbl_QSCORE" runat="server" Text=""></asp:Label> 
        </td> 

       </tr> 
      </table> 
     </ItemTemplate> 
     </asp:Repeater>  
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:FBB-DSL-DBConnectionString %>" 
      SelectCommand="SELECT TOP (5) QuestionID, QuestionText, ExamID, Q_Answer_1, Q_Answer_2, Q_Answer_3, Q_Answer_4, Q_Correct_Ans FROM tbl_QZ_Question WHERE (ExamID = @ExamID) ORDER BY NEWID()"> 
      <SelectParameters> 
       <asp:QueryStringParameter DefaultValue="0" Name="ExamID" 
        QueryStringField="ExamID" Type="Int32" /> 
      </SelectParameters> 
     </asp:SqlDataSource> 

    <br /> 

    <asp:Button ID="btn_SAVEANSWERS" runat="server" Text="Save Answers !" /> 

在此先感謝。

回答

0

移動響應從嘗試catch子句重定向是後每個

 Response.Redirect("GetResult.aspx?ExamID=" & Request.QueryString("ExamID")) 
相關問題