从 LINQ 结果类型到类型 'Integer' 的转换无效



我创建了一个带有ComboBox进行搜索的表单和4个文本框以查看字段值。我使用Form_load事件填充了ComboBox。

这是我的代码:

Imports System.Data.SqlClient
Imports System.Data.Linq
Public Class FRM_Product
    Dim connection As New SqlConnection(connection string)
    Dim db As New ProductDataContext(connection)
    Private Sub FRM_ Product_Load(sender As Object, e As EventArgs) Handles 
        MyBase.Load
        Dim V_Query = db.Product_Write(Nothing, Nothing, Nothing, Nothing).ToList
        If V_Query.Count <> 0 Then
            CMB_Search.DataSource = V_Query
            CMB_Search.ValueMember = "Product_ID"
            CMB_Search.DisplayMember = "Product_A"
        End If
    End Sub
    Private Sub CMB_Search_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CMB_Search.SelectedIndexChanged
        Dim ID As Integer = CMB_Search.SelectedValue
        Dim V_Query = db.Product_Write(Nothing, Nothing, Nothing, Nothing).ToList
        TXT_Product_ID.Text = V_Query(0). Product_ID
        TXT_ Product_A.Text = V_Query(0). Product_A
        TXT_ Product_E.Text = V_Query(0). Product_E
        TXT_ Product_NOTE.Text = V_Query(0). Product_Note
    End Sub

这是Product_Write存储过程:

CREATE PROCEDURE Product_Write 
    @ Product_ID   INT = NULL , 
    @ Product_A    nvarchar(50)= NULL , 
    @ Product_E    nvarchar(50)= NULL , 
    @ Product_Note nvarchar(255)= NULL 
AS 
    BEGIN 
        SET NOCOUNT ON; 
        BEGIN 
            SELECT * 
            FROM [dbo].[ Product] 
            WHERE [Product_ID] = ISNULL( @Product_ID,[Product_ID]) 
              AND [Product_A] = ISNULL( @Product_A,[Product_A]) 
              AND [Product_E] = ISNULL( @Product_E, [Product_E]) 
            ORDER BY [Product_A] 
        END 
    END

线

Dim ID As Integer = CMB_Search.SelectedValue

引起此错误:

从类型的" product_writeresult"转换为键入'整数'是无效的。'

如何解决此错误?

您可以尝试以下方法:

Dim myDataRowView As DataRowView = DirectCast(CMB_Search.SelectedValue, DataRowView)
Dim ID as Integer = CInt(myDataRowView("Product_ID"))

最新更新