ExecuteOracleScalar error??

D

daiyuan

Hi:

When I run this project,I miss a error:

it's say: convert is falied!

I don't konw why.

Please advice.

Thanks!!!

This is my codes:


////////////////////////////////////////////////////////////////////////////
///

using System.Data.OracleClient;

....

private int SetVirtualItemCount()
{
int result = 0;

// database connection
String strConn = "Password=;User ID=sa;Data Source=HBMIS";
OracleConnection conn = new OracleConnection(strConn);

// ¼ÆËã¼Í¼×ÜÊý
String strCmd = "SELECT COUNT(*) FROM Employees";
OracleCommand cmd = new OracleCommand(strCmd,conn);

// Ö´ÐÐ
conn.Open();
int count = (int)cmd.ExecuteOracleScalar(); //error
line
conn.Close();

//result = (int)count;

return result;
}
////////////////////////////////////////////////////////////////////////
 
S

Sushil Chordia

DaiYuan,
ExecuteOracleScalar() returns Oracle specific DataType.Since you are
expecting an integer, the equivalent Oracle type is OracleNumber. Hence your
code should look like this:
Code Snippet:
oraclecommand1.CommandText = "select Count(*) from Employees";
OracleNumber oraclenumber1 =
(OracleNumber)oraclecommand1.ExecuteOracleScalar();

If all you want is to get an integer value, use ExecuteScalar method instead
Code Snippet:
oraclecommand1.CommandText = "select Count(*) from Employees";
int count = (int)oraclecommand1.ExecuteScalar();

Hope That Helps,
Sushil.
 

zam

Joined
Sep 12, 2012
Messages
1
Reaction score
0
Hi:

When I run this project,I miss a error:

it's say: convert is falied!

I don't konw why.

Please advice.

Thanks!!!

This is my codes:


////////////////////////////////////////////////////////////////////////////
///

using System.Data.OracleClient;

....

private int SetVirtualItemCount()
{
int result = 0;

// database connection
String strConn = "Password=;User ID=sa;Data Source=HBMIS";
OracleConnection conn = new OracleConnection(strConn);

// ¼ÆËã¼Í¼×ÜÊý
String strCmd = "SELECT COUNT(*) FROM Employees";
OracleCommand cmd = new OracleCommand(strCmd,conn);

// Ö´ÐÐ
conn.Open();
int count = (int)cmd.ExecuteOracleScalar(); //error
line
conn.Close();

//result = (int)count;

return result;
}
////////////////////////////////////////////////////////////////////////

zam
*********************************
int count = (int)cmd.ExecuteOracleScalar(); //error

answer

Int32 count = Convert.ToInt32(cmd.ExecuteOracleScalar());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top