过程返回错误:无法将System.Int32强制转换为System.Int32 []

时间:2015-01-04 04:31:44

标签: vb.net sql-server-ce

我编写了一个应该从我的数据库返回值的过程。要返回的值是整数。这是我的代码:

    Private Sub btnSelectProducts_Click(sender As Object, e As EventArgs) Handles btnSelectProducts.Click

    Try

    'Connection Variables
    Dim cnn As SqlCeConnection
    Dim connectionString As String

    'Connect to database
    connectionString = "Data Source=|DataDirectory|\Database\ContactsAndInventory.sdf"
    cnn = New SqlCeConnection(connectionString)

    Dim Stream As New MemoryStream()
    cnn.Open()

    Dim command As New SqlCeCommand("SELECT CATEGORY_ID FROM PRODUCT_CATEGORY WHERE (CATEGORY_NAME = @CATEGORY_NAME)", cnn)
    command.Parameters.AddWithValue("@CATEGORY_NAME", txtCategoryName.Text)

        Dim catId As Integer() = CType(command.ExecuteScalar(), Integer())'Error Here

    MsgBox(catId)

        cnn.Close()

    Catch ex As Exception

        MsgBox(ex.ToString)

    End Try


End Sub

我收到错误:

System.InvalidCastException: Unable to cast objct to type System.Int32 to type System.Int32[]

在无法找出原因的情况下,该字段是一个Integer字段,我想要返回的值是一个整数。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我弄清楚我的错误:

  Dim catId = Convert.ToInt32(command.ExecuteScalar())