尝试从下拉列表输入我的值并从GridViewRow输入值时,我一直收到上述错误。
它还说“标准表达式中的数据类型不匹配”,我从列表/行中提取问题和论文的ID,并将它们输入到我的数据库中。
以下是显示错误的代码。
Using con As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString)
con.Open()
Dim allCheckedRows = From row In CreateTest.Rows.Cast(Of GridViewRow)()
Let chkQuestion = DirectCast(row.FindControl("QuestionSelector"), CheckBox)
Where chkQuestion.Checked
Select row
For Each checkedRow As GridViewRow In allCheckedRows
Dim QuestionID As String = (checkedRow.Cells(0).Text)
Dim PaperID As String = DropDownList1.SelectedValue
Dim insertExamQuery = "INSERT INTO QuestionInPaper VALUES ('QuestionID', 'PaperID');"
Dim insertExamCmd = con.CreateCommand()
insertExamCmd.CommandText = insertExamQuery
insertExamCmd.ExecuteNonQuery()
Next
End Using
这是aspx所有这些都指的是:
<asp:GridView ID="CreateTest" runat="server"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="QuestionID" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="QuestionSelector" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="QuestionID" HeaderText="QuestionID"
InsertVisible="False" ReadOnly="True" SortExpression="QuestionID" />
<asp:BoundField DataField="Answer" HeaderText="Answer"
SortExpression="Answer" />
<asp:BoundField DataField="Question" HeaderText="Question"
SortExpression="Question" />
<asp:BoundField DataField="SubjectID" HeaderText="SubjectID"
SortExpression="SubjectID" />
<asp:TemplateField></asp:TemplateField>
</Columns>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [QuestionID], [Answer], [Question], [SubjectID] FROM [Question]">
</asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource3" DataTextField="PaperID" DataValueField="PaperID">
</asp:DropDownList>
你能看到这种不匹配的地方吗?非常感谢。
答案 0 :(得分:0)
您说在QuestionInPaper中的两个字段都是数字类型,但您正在尝试插入字符串数据 - “QuestionID”和“PaperID”。您需要将它们作为参数添加到命令中。您也将它们创建为String,代码和数据库中的两种类型应该相等。
Using con As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString)
con.Open()
Dim allCheckedRows = From row In CreateTest.Rows.Cast(Of GridViewRow)()
Let chkQuestion = DirectCast(row.FindControl("QuestionSelector"), CheckBox)
Where chkQuestion.Checked
Select row
For Each checkedRow As GridViewRow In allCheckedRows
'// change type and parse
Dim QuestionID As Decimal = Decimal.Parse(checkedRow.Cells(0).Text)
Dim PaperID As Decimal = Decimal.Parse(DropDownList1.SelectedValue)
'// changed
Dim insertExamQuery = "INSERT INTO QuestionInPaper (QuestionID, PaperID) VALUES (?, ?);"
Dim insertExamCmd = con.CreateCommand()
'// added below
insertExamCmd.Parameters.Add("@QuestionID", OleDb.OleDbType.Numeric).Value = QuestionID
insertExamCmd.Parameters.Add("@PaperID", OleDb.OleDbType.Numeric).Value = PaperID
'// end add
insertExamCmd.CommandText = insertExamQuery
insertExamCmd.ExecuteNonQuery()
Next
End Using