数据类型不匹配Vb.net访问

时间:2015-10-31 23:03:17

标签: database vb.net

enter image description here我正在尝试使用vb将数据插入到我正在构建的应用程序的Access数据库中。试图让数据进入数据库,我一直在试着看不见的墙。下面是代码,下面我会解释一下。

 Private Sub btnSubmitExpense_Click(sender As Object, e As EventArgs) Handles btnSubmitExpense.Click
    provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    dataFile = "C:\PFADatabase.accdb"
    connString = provider & dataFile
    myConnection.ConnectionString = connString
    myConnection.Open()
    Dim str As String
    str = "Insert into Expenses ([ExpenseTypeID], [DateOfExpense],[AmountOfExpense]) Values(?,?,?)"
    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
    cmd.Parameters.Add(New OleDbParameter("ExpenseTypeID", CType(cboCategoryOfExpense.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("DateOfExpense", CType(dateOfExpense.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("AmountOfExpense", CType(txtAmountOfExpense.Text, String)))

    Try
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        myConnection.Close()
        'clear the form below
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

[ExpenseTypeID]实际上是一个查找。该对象是vb中的一个组合框,我在此处发布的代码上编程了。 [DateOfExpense]也是VB中的Date对象。我只是把它拖了过来。我希望它与该表中的所有内容相关联。 再次将[AmountOfExpense]绑定到数据库中。

用户基本上使用工具输入日期,然后选择费用类别,然后输入金额。这是一个表,但expenseType实际上是在另一个表的Access中查找,所以我们可以有下拉列表。一切顺利,直到我点击保存按钮。然后,每当我这样做,我得到异常类型不匹配。我该如何解决这个问题?我认为文本字符串是错误的,但我已经尝试了其他的东西,所以我认为它比这更多,这就是为什么我来到这里给你的专业人士获取输入。

谢谢。

编辑:错误发生在

 cmd.ExecuteNonQuery()

并说“标准表达式中的数据类型不匹配。”

'START THE POPULATE THE COMBO BOXES

    'Gets the database and makes the connection

    provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="

    dataFile = "C:\PFADatabase.accdb"

    connString = provider & dataFile
    myConnection.ConnectionString = connString


    'Generates a query command
    Dim cmd As OleDbCommand = New OleDbCommand("SELECT [ExpenseType] FROM [ExpenseType]", myConnection)
    myConnection.Open() 'Opens connection
    Dim dr As OleDbDataReader = cmd.ExecuteReader


    While dr.Read
        cboCategoryOfExpense.Items.Add(dr.Item(0))
    End While
    myConnection.Close()


    'END THE POPULATE OF COMBO BOXES

正如您在评论中看到的那样,它是如何填充组合框的。

编辑:在费用下的图片是主表。带圆圈的值是FK,它导致ExpenseType的PK。我们从下拉的数据是ExpenseType下的ExpenseType。根据我的理解,你们说,我同意,它写的是ExpenseType的PK,而不是数据。我想要它做的就是保持数据分类为我们将使用的图形。

1 个答案:

答案 0 :(得分:1)

根据列的要求将输入转换为适当的数据类型。如果你没有指定参数的数据类型并且总是传递字符串,那么你很容易成为这种错误的目标

   ...
   Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)

   Dim expID As Integer
   if Not Int32.TryParse(cboCategoryOfExpense.Text, expID) Then
        MessageBox.Show("Invalid integer")
        return 
   End If
   cmd.Parameters.Add("ExpenseTypeID", OleDbType.Integer).Value = expID

   Dim dtExp as DateTime
   if Not DateTime.TryParse(dateOfExpense.Text, dtExp) Then 
        MessageBox.Show("Invalid Date")
        return 
   End If
   cmd.Parameters.Add("DateOfExpense", OleDbType.Date).Value = dtExp

   Dim amount As Decimal
   if Not Decimal.TryParse(txtAmountOfExpense.Text, amount) Then 
        MessageBox.Show("Invalid Decimal")
        return 
   End If
   cmd.Parameters.Add("AmountOfExpense", OleDbType.Currency).Value = amount
   ....

查看您的编辑和评论,也许这就是您需要的

' Be sure to have a combobox with DropDownList style to avoid
' user entering an unexpected value then....

'Load the combobox from the table ExpenseType 
 Dim cmd As OleDbCommand = New OleDbCommand("SELECT [ExpenseTypeID], " & _
                           "[ExpenseType] FROM [ExpenseType]", myConnection)
myConnection.Open() 'Opens connection
Dim dr As OleDbDataReader = cmd.ExecuteReader
Dim dt = new DataTable()
dt.Load(dr)

cboCategoryOfExpense.DataSource = dt

' The visible part of the combo contains value from ExpenseType field
cboCategoryOfExpense.DisplayMember = "ExpenseType"

' The hidden part refers to the ExpenseTypeID field
cboCategoryOfExpense.ValueMember = "ExpenseTypeID"
...

现在转换组合框值时,你可以写

   Dim expID As Integer

   ' Sanity check in case the user want to insert but has not 
   ' selected anything from the combobox 
   if cboCategoryOfExpense.SelectedValue Is Nothing Then
        MessageBox.Show("Select an Expense")
        return 
   End if
   cmd.Parameters.Add("ExpenseTypeID", OleDbType.Integer).Value = Convert.ToInt32(cboCategoryOfExpense.SelectedValue)
   ....