【问题标题】:VB.NET System.Data.EvaluateException: 'Cannot perform '=' operation on System.Int32 and System.String.'VB.NET System.Data.EvaluateException:'无法对 System.Int32 和 System.String 执行'='操作。'
【发布时间】:2018-09-16 21:11:21
【问题描述】:

我有一个连接到我的项目的 Access 数据库,我在这里要做的是在连接到 datagridview 上的字段的组合框中显示一些值。

这是我得到的错误

System.Data.EvaluateException: 'Cannot perform '=' operation on System.Int32 and System.String.'

这里是代码

   Private Sub form_inventory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'InventoryManager2DataSet.Sub_Category' table. You can move, or remove it, as needed.
        Me.Sub_CategoryTableAdapter.Fill(Me.InventoryManager2DataSet.Sub_Category)

        'TODO: This line of code loads data into the 'InventoryManager2DataSet.Sub_Category' table. You can move, or remove it, as needed.
        Me.Sub_CategoryTableAdapter.Fill(Me.InventoryManager2DataSet.Sub_Category)
        'TODO: This line of code loads data into the 'InventoryManager2DataSet.Inventory' table. You can move, or remove it, as needed.
        Me.InventoryTableAdapter.Fill(Me.InventoryManager2DataSet.Inventory)
        Me.DataGridViewTextBoxColumn8.DisplayMember = "Category"
        Me.DataGridViewTextBoxColumn8.ValueMember = "ID"
        Me.DataGridViewTextBoxColumn8.DataSource = Sub_CategoryBindingSource


        With Sub_CategoryComboBox
            .DataSource = Sub_CategoryBindingSource
            .DisplayMember = "Category"
            .ValueMember = "Id"
        End With

    End Sub

    Private Sub Sub_CategoryComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Sub_CategoryComboBox.SelectedIndexChanged
        If Me.Sub_CategoryComboBox.SelectedIndex = -1 Then
            InventoryBindingSource.Filter = Nothing
        Else
            InventoryBindingSource.Filter = $"convert[Sub Category] = '{Me.Sub_CategoryComboBox.SelectedValue}'"
        End If
    End Sub

这部分代码触发了错误

 InventoryBindingSource.Filter = $"convert[Sub Category] = '{Me.Sub_CategoryComboBox.SelectedValue}'"

我可以让它在没有连接的情况下工作一个干净的组合框,但是当它连接到字段时我无法让它工作。

  • Sub_Category 是一个表
  • 子类别是长整数
  • 类别是短文本

子类别是从包含类别字段的子类别表中查找的字段。

这里是可视化的:

【问题讨论】:

    标签: database vb.net ms-access type-conversion


    【解决方案1】:

    您说组合框的值将是Id 属性

    With Sub_CategoryComboBox
        .DataSource = Sub_CategoryBindingSource
        .DisplayMember = "Category"
        .ValueMember = "Id" ' This line!
    End With
    

    然后您尝试将组合框的值(Id)与文本字段进行比较。

    我现在不知道你的业务逻辑应该是什么,但我怀疑这可能是你需要的:

    With Sub_CategoryComboBox
        .DataSource = Sub_CategoryBindingSource
        .DisplayMember = "Category"
        .ValueMember = "Category" ' Changed!
    End With
    

    【讨论】:

    • 这不起作用我已经尝试过了,也忽略了 InventoryBindingSource.Filter = $"convert[Sub Category] ​​= '{Me.Sub_CategoryComboBox.SelectedValue}'" 中的转换我忘了删除它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    相关资源
    最近更新 更多