If InvoiceNumberTextBox.Text <> "" And ClientNoTextBox.Text <> "" And FirstNameTextBox.Text <> "" And LastNameTextBox.Text <> "" And ProductNameTextBox.Text <> "" And ProductCodeTextBox.Text <> "" And ProductUnitTextBox.Text <> "" And QuantityTextBox.Text <> "" And ProductPriceTextBox.Text <> "" And TotalPriceTextBox.Text <> "" And Label1.Text <> "" And DateToDeliverDateTimePicker.Text <> "" And AddressTextBox.Text <> "" And ContactNoTextBox.Text <> "" Then
Dim DT As String
DT = Label1.Text
cmdinsert.CommandText = "INSERT INTO Orders ([InvoiceNumber], [ClientNo], [FirstName], [LastName], [ProductName], [ProductCode], [Unit], [Quantity], [Price], [TotalPrice], [DateToday], [DateToDeliver], [Address], [ContactNo]) VALUES ('" & InvoiceNumberTextBox.Text & "', '" & ClientNoTextBox.Text & "', '" & FirstNameTextBox.Text & "', '" & LastNameTextBox.Text & "', '" & ProductNameTextBox.Text & "', '" & ProductCodeTextBox.Text & "', '" & ProductUnitTextBox.Text & "', '" & QuantityTextBox.Text & "', '" & ProductPriceTextBox.Text & "', '" & TotalPriceTextBox.Text & "', '" & Label1.Text & "', '" & DateToDeliverDateTimePicker.Text & "', '" & AddressTextBox.Text & "', '" & ContactNoTextBox.Text & "')"
cmdinsert.CommandType = CommandType.Text
cmdinsert.Connection = cnn
cmdinsert.ExecuteNonQuery()
MsgBox("Added")
ProductNameTextBox.Clear()
ProductPriceTextBox.Clear()
FirstNameTextBox.Clear()
ContactNoTextBox.Clear()
TotalPriceTextBox.Clear()
ProductUnitTextBox.Clear()
LastNameTextBox.Clear()
AddressTextBox.Clear()
ClientNoTextBox.Clear()
ProductCodeTextBox.Clear()
QuantityTextBox.Clear()
InvoiceNumberTextBox.Clear()
If QuantityTextBox.Text <> "" Then
cmdupdate.CommandText = "update Products set Quantity = '" & Label4.Text & "' where ProductCode = '" & ProductCodeTextBox.Text & "'"
cmdupdate.CommandType = CommandType.Text
cmd.Connection = cnn
cmdupdate.ExecuteNonQuery()
Else
MsgBox("PLEASE CHECK YOUR FORM")
End If
cmdupdate.Dispose()
Me.ProductsTableAdapter.Fill(Me.DatabaseCasanovaDataSet.Products)
ProductsDataGridView.Refresh()
ProductsDataGridView.Update()
End If
cmdinsert.Dispose()
Debug say是Mismatch表达式,但我在订单表中按数据库顺序检查3x 如果我错了,任何人都要检查一下吗?
Label1.text是日期
正确订购表格列表
InvoiceNumber ClientNo 名字 姓 产品名称 产品代码 单元 数量 价钱 TotalPrice DateToday DateToDeliver 地址 ContactNo
答案 0 :(得分:1)
您正在将14个字符串传递给您的表格。如果表中的任何字段不是Text类型,则可能会导致数据类型不匹配。实际上,您应该更改代码以使用参数化查询,并为每个参数传递正确的数据类型。
例如,如果DateToday字段需要日期时间,则传递日期时间,而不是字符串
cmdinsert.CommandText = "INSERT INTO Orders ([InvoiceNumber], [ClientNo], [FirstName], " & _
"[LastName], [ProductName], [ProductCode], [Unit], [Quantity], [Price], [TotalPrice], " & _
"[DateToday], [DateToDeliver], [Address], [ContactNo]) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
cmdInsert.Parameters.AddWithValue("@p1", InvoiceNumberTextBox.Text)
......
cmdInsert.Parameters.AddWithValue("@p11", Convert.ToDateTime(Label1.Text))
.....
cmdinsert.Connection = cnn
cmdinsert.ExecuteNonQuery()
在上面的示例中,第11个参数对应于DateToday字段。如您所见,我强制它为日期时间,因此将正确的值传递给数据库。同样的事情应该用于数字(整数/十进制),但也应该用于字符串,因为参数避免了文本框值中单引号的问题。