ExecScalar() in OracleClient

  • Thread starter Thread starter AlexB
  • Start date Start date
A

AlexB

The .ExecScalar method of the OracleClient seems to have a
bug. If I call it with a query that returns a number it
executes fine. However, it I attempt to return a string
if returns "0" and generates an unusual error message:
invalid cast from string to integer (see code below).

Is this a bug or is there a trick to making this work?

Anyone?


The following function works fine with "Select Count(*)
From table"; fails with "Select TextField From Table"

Function ExecScaler(psSQL as String, psConnect as String)
as Object
Dim oCmd As New OracleCommand()
Dim oConn As New OracleConnection()
oConn.ConnectionString = psConnect
With oCmd
.Connection = oConn
oConn.Open()
.CommandText = psSQL
ExecScalar = .ExecuteScalar()
End With
End Function
 
Hi AlexB,

I tried, but I cannot reproduce the problem on my side.

I am using the following table:

CREATE TABLE "SYSTEM"."TEST"("NAME" VARCHAR2(50) NOT NULL)

In the talbe, I have the following rows:

NAME
--------------------------------------------------
1
2
3
4
5
0

6 rows selected.

The following is my code:

Function ExecScalar(ByVal psSQL As String, ByVal psConnect As String)
As Object
Dim oCmd As New OracleCommand()
Dim oConn As New OracleConnection()
oConn.ConnectionString = psConnect
With oCmd
.Connection = oConn
oConn.Open()
.CommandText = psSQL
ExecScalar = .ExecuteScalar()
End With
End Function


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(ExecScalar("select count(*) from test",
"Server=chinaora.orcl;Uid=system;Pwd=manager"))
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
MsgBox(ExecScalar("select name from test where name='0'",
"Server=chinaora.orcl;Uid=system;Pwd=manager"))
End Sub

Everything works as expected.

Please try the above your sides. Any Qs, please reply to this post.
 
Back
Top