以下是代码:
Private m_cn As New SqlConnection
Private m_DA As SqlDataAdapter
Private m_CB As SqlCommandBuilder
Private m_DataTable As New DataTable
Private m_intRowPosition As Integer = 0
Private Sub InsertDatabaseItem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
m_cn.ConnectionString = "Data Source=TREVOR-PC\SQLSERVEREXPRESS;Initial Catalog=Milk Convience Products;Integrated Security=True"
m_cn.Open()
m_DA = New SqlDataAdapter("Select * From ProductIndex", m_cn)
m_CB = New SqlCommandBuilder(m_DA)
txtBarcode.Focus()
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Dim cmd As New SqlCommand(("INSERT INTO ProductIndex VALUES(" &
"@ID," &
"@Name," &
"@Price," &
"@Desc)" &
"@Barcode)"), m_cn)
cmd.Parameters.Add("@ID", SqlDbType.Int)
cmd.Parameters("@ID").Value = txtID.Text
cmd.Parameters.Add("@Name", SqlDbType.VarChar)
cmd.Parameters("@Name").Value = txtName.Text
cmd.Parameters.Add("@Price", SqlDbType.Money)
cmd.Parameters("@Price").Value = txtPrice.Text
cmd.Parameters.Add("@Desc", SqlDbType.VarChar)
cmd.Parameters("@Desc").Value = txtDesc.Text
cmd.Parameters.Add("@Barcode", SqlDbType.BigInt)
cmd.Parameters("@Barcode").Value = txtBarcode.Text
cmd.ExecuteNonQuery()
MsgBox("Success!", MsgBoxStyle.Information, "SUCCESS")
Me.Hide()
txtID.Clear()
txtName.Clear()
txtPrice.Clear()
txtDesc.Clear()
txtBarcode.Clear()
m_cn.Close()
m_cn.Dispose()
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Hide()
End Sub
单击btnOK以输入新数据库项后,会发生异常并显示:无法将参数值从String转换为Int32。
我做了一些调试,发现错误发生在“cmd.Parameters(”@ Barcode“)之后.Value = txtBarcode.Text”代码行
答案 0 :(得分:1)
cmd.Parameters.Add("@Barcode", SqlDbType.BigInt)
cmd.Parameters("@Barcode").Value = txtBarcode.Text
您正在尝试将txtBarcode.Text强制转换为大整数。这应该通过显式转换值来完成。您的测试用例可能是空字符串或其他无法转换的字符串值。
注意重新阅读您的代码这也可能是Id和Price参数的问题。