Procedure returns error: Cannot cast System.Int32 to System.Int32[]
Procedure returns error: Cannot cast System.Int32 to System.Int32[]
我写了一个程序,应该 return 我的数据库中的一个值。要 returned 的值是一个整数。这是我的代码:
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[]
不知道为什么,该字段是一个整数字段,而我尝试 return 的值是一个整数。我做错了什么?
我发现我的错误:
Dim catId = Convert.ToInt32(command.ExecuteScalar())
我写了一个程序,应该 return 我的数据库中的一个值。要 returned 的值是一个整数。这是我的代码:
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[]
不知道为什么,该字段是一个整数字段,而我尝试 return 的值是一个整数。我做错了什么?
我发现我的错误:
Dim catId = Convert.ToInt32(command.ExecuteScalar())