我收到以下错误'条件表达式中的数据类型不匹配'添加数据时

时间:2014-04-09 13:13:45

标签: sql vb.net

我有以下错误但我不确定为什么我收到此错误。当我尝试向数据库添加数据时,我收到此错误。

条件表达式中的数据类型不匹配。

我正在处理的代码如下。有人可以帮忙吗?谢谢

 Private Sub RefreshData()
    If Not cnn.State = ConnectionState.Open Then
        'open connection
        cnn.Open()
    End If

    Dim da As New OleDb.OleDbDataAdapter("SELECT Item ID as [Item ID], " & _
                                         "Item Name as [Item Name], " & _
                                         "Item Type as [Item Type], " & _
                                         "Quantity as [Quantity], " & _
                                         "Min Shelf Stock as [Min Shelf Stock], " & _
                                         "Purchase Price as [Purchase Price], Note " & _
                                         " From Product ORDER BY Item ID", cnn)
    Dim dt As New DataTable
    'fill data to datatable 
    da.Fill(dt)

    'offer data in data table into datagridview
    Me.dgvData.DataSource = dt

    'Close the connection
    cnn.Close()
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim cmd As New OleDb.OleDbCommand
        If Not cnn.State = ConnectionState.Open Then
            'The line of code below opens the connection to the database if it isnt open
        cnn.Open()
        End If

    cmd.Connection = cnn
    'Check whether to add new or update
    If Me.txtItemID.Tag & "" = "" Then
        'Add new 
        'The line of coding below adds data to table
        cmd.CommandText = "INSERT INTO Product ([Item Id], [Item Name], [Item Type], [Quantity], [Min Shelf Stock], [Purchase Price], [Note]) VALUES (@id, @name, @iType, @quantity, @minshelfstock, @price, @note)"
        cmd.Parameters.AddWithValue("@id", txtItemID.Text)
        cmd.Parameters.AddWithValue("@name", txtItemName.Text)
        cmd.Parameters.AddWithValue("@iType", cboItemType.Text)
        cmd.Parameters.AddWithValue("@quantity", txtQuantity.Text)
        cmd.Parameters.AddWithValue("@minshelfstock", txtMinShelfStock.Text)
        cmd.Parameters.AddWithValue("@price", txtPurchasePrice.Text)
        cmd.Parameters.AddWithValue("@note", txtNote.Text)
        cmd.ExecuteNonQuery()
    Else
        'Update data in the table 
        cmd.CommandText = "UPDATE Product " & _
                    " SET [Item ID]=" & Me.txtItemID.Text & _
                    ", [Item Name]='" & Me.txtItemName.Text & "'" & _
                    ", [Item Type]='" & Me.cboItemType.Text & "'" & _
                    ", [Quantity]=" & Me.txtQuantity.Text & "" & _
                    ", [Min Shelf Stock]=" & Me.txtMinShelfStock.Text & "" & _
                    ", [Purchase Price]=" & Me.txtPurchasePrice.Text & "" & _
                    ", [Note]='" & Me.txtNote.Text & "'" & _
                    " WHERE [Item ID]=" & Me.txtItemID.Tag
        cmd.ExecuteNonQuery()
    End If
    'Refresh data in list
    RefreshData()
    'Clear the form
    Me.btnClear.PerformClick()

    'The code below closes the connection to the database
    cnn.Close()
End Sub

Private Sub Product_Load(sender As Object, e As EventArgs) Handles Me.Load
    cnn = New OleDb.OleDbConnection
    cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\inventorysystem.mdb"
    '
    'Get the data into a list
    Me.RefreshData()
End Sub

Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
    'check for the selected item in list
    If Me.dgvData.Rows.Count > 0 Then
        If Me.dgvData.SelectedRows.Count > 0 Then
            Dim intItemID As Integer = Me.dgvData.SelectedRows(0).Cells("Item ID").Value
            'Get the data from database followed Item ID
            'Open the connection
            If Not cnn.State = ConnectionState.Open Then
                cnn.Open()
            End If
            'Get the data into the datatable
            Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Product " & _
                                                  " WHERE Item ID=" & intItemID, cnn)
            Dim dt As New DataTable
            da.Fill(dt)

            Me.txtItemID.Text = intItemID
            Me.txtItemName.Text = dt.Rows(0).Item("Item Name")
            Me.cboItemType.Text = dt.Rows(0).Item("Item Type")
            Me.txtQuantity.Text = dt.Rows(0).Item("Quantity")
            Me.txtMinShelfStock.Text = dt.Rows(0).Item("Min Shelf Stock")
            Me.txtPurchasePrice.Text = dt.Rows(0).Item("Purchase Price")
            Me.txtNote.Text = dt.Rows(0).Item("Note")
            '
            'Hide the ID to be edited in TAG od txtItemID in case ID is changed 
            Me.txtItemID.Tag = intItemID
            'Change the add button to update 
            Me.btnAdd.Text = "Update"
            'Disable the Edit button
            Me.btnEdit.Enabled = False
            'Close the connection
            cnn.Close()
        End If
    End If
End Sub

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    'Check for the selected item in the list
    If Me.dgvData.Rows.Count > 0 Then
        If Me.dgvData.SelectedRows.Count > 0 Then
            Dim intItemID As Integer = Me.dgvData.SelectedRows(0).Cells("Item ID").Value
            'Open the connection 
            If Not cnn.State = ConnectionState.Open Then
                cnn.Open()
            End If

            'Delete data 
            Dim cmd As New OleDb.OleDbCommand
            cmd.Connection = cnn
            cmd.CommandText = "DELETE FROM Product WHERE Item ID" & intItemID
            cmd.ExecuteNonQuery()
            'Refresh the data 
            Me.RefreshData()

            'Close the connection
            cnn.Close()
        End If
    End If
End Sub

结束班

2 个答案:

答案 0 :(得分:1)

问题是AddWithValue方法。此方法创建参数及其数据类型。但是,为了这样做,它会查看作为第二个参数传递的值的数据类型。如果此值是一个字符串(就像您总是传递文本框的Text属性一样),那么它会构建一个类型为OleDbType.LongVarWChar或类似的参数。当然,这不是您的数据库对您要更新的一个或多个字段的期望。

您需要检查字段的数据类型并传递类似数据类型的参数,并将转换应用于文本框的字符串值。

例如,如果price字段的类型为numeric decimal,则需要编写

cmd.Parameters.AddWithValue("@price", Convert.ToDecimal(txtPurchasePrice.Text))

答案 1 :(得分:0)

可能是由于数据数据类型。

尝试this

cmd.Parameters.Add("@id",  SqlDbType.Int).Value = txtItemId.Text;
cmd.Parameters.Add("@name",   SqlDbType.VarChar, 20).Value = txtItemName.Text;
cmd.Parameters.Add("@iType",   SqlDbType.VarChar, 20).Value = cboItemType.Text;
cmd.Parameters.Add("@quantity",  SqlDbType.Int).Value = txtQuantity.Text;
cmd.Parameters.Add("@minshelfstock",  SqlDbType.Int).Value = txtMinShelfStock.Text;
cmd.Parameters.Add("@price",  SqlDbType.Int).Value = txtPurchasePrice.Text;
cmd.Parameters.Add("@note", SqlDbType.VarChar, 20).Value = txtNote.Text;

不确定它是否是您想要实现的目标。