我有一个使用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 :(得分:0)
将响应重定向从try catch子句移到每个
之后 Response.Redirect("GetResult.aspx?ExamID=" & Request.QueryString("ExamID"))