从LINQ Result类型到“Integer”类型的转换无效

时间:2017-09-24 20:22:18

标签: vb.net linq

我创建了一个带有ComboBox的表单来搜索,并创建了4个TextBox来查看字段值。我使用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”类型转换为“Integer”类型无效。'

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

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