473,472 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

OleDb ADO.Net ExecuteScalar()

When I use the OleDb to connect a Oracle db, and use the
ExecutScalar() method to get the count of a table, the
return value can't be cast to a data type, say int, while
this works fine for a SQL server db using SqlCommand.

Following is the code example:

string strCnt = "SELECT count (*) from B_PlanSchemeItems";
string strSource="Provider=MSDAORA;Data
Source=avocet1;User ID=dbo; Password=mmsmms;";
OleDbConnection conn = new OleDbConnection(strSource);
conn.Open();

OleDbCommand cmdCount = new OleDbCommand(strCnt,conn);
object objInt;
objInt = cmdCount.ExecuteScalar();
intNumRecs = (int) (objInt);

The run time error occurs with message:
"Specified cast is not valid"

Following code works fine for a SQL server db:

string strCnt = "SELECT count (*) from B_PlanSchemeItems";
string
strSource="server=planserv1;uid=sa;pwd=sysadmin;da tabase=a
vocet1";
SqlConnection myConn = new SqlConnection(strSource);
myConn.Open();

SqlCommand cmdCount = new SqlCommand(strCnt, myConn);
intNumRecs = (int) cmdCount.ExecuteScalar();

Can anyone help me on solving the cast problem in OleDb
case? Thanks.
Nov 15 '05 #1
2 12984
Hi ,

OleDbCommand.ExecuteScalar return an object , not in integer so maybe what
you are getting is a Decimal and therefore the cast is not valid, this is
what I would do.
Assign it to a object variable as you do:
objInt = cmdCount.ExecuteScalar();
place a breakpoint on this line and see what is the correct type, just to
know ;)
I would use Convert.ToInt32() instead of cast:
intNumRecs =Convert.ToInt32( objInt);

this should work fine.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"WeiminZhang" <wz****@stssystems.com> wrote in message
news:01****************************@phx.gbl...
When I use the OleDb to connect a Oracle db, and use the
ExecutScalar() method to get the count of a table, the
return value can't be cast to a data type, say int, while
this works fine for a SQL server db using SqlCommand.

Following is the code example:

string strCnt = "SELECT count (*) from B_PlanSchemeItems";
string strSource="Provider=MSDAORA;Data
Source=avocet1;User ID=dbo; Password=mmsmms;";
OleDbConnection conn = new OleDbConnection(strSource);
conn.Open();

OleDbCommand cmdCount = new OleDbCommand(strCnt,conn);
object objInt;
objInt = cmdCount.ExecuteScalar();
intNumRecs = (int) (objInt);

The run time error occurs with message:
"Specified cast is not valid"

Following code works fine for a SQL server db:

string strCnt = "SELECT count (*) from B_PlanSchemeItems";
string
strSource="server=planserv1;uid=sa;pwd=sysadmin;da tabase=a
vocet1";
SqlConnection myConn = new SqlConnection(strSource);
myConn.Open();

SqlCommand cmdCount = new SqlCommand(strCnt, myConn);
intNumRecs = (int) cmdCount.ExecuteScalar();

Can anyone help me on solving the cast problem in OleDb
case? Thanks.

Nov 15 '05 #2
Thanks, Ignacio Machin!

It works fine now. Looks like the SqlClient object is not
exactly same as OleDb in the way it returns the object.

Anyway, thanks again.
-----Original Message-----
Hi ,

OleDbCommand.ExecuteScalar return an object , not in integer so maybe whatyou are getting is a Decimal and therefore the cast is not valid, this iswhat I would do.
Assign it to a object variable as you do:
objInt = cmdCount.ExecuteScalar();
place a breakpoint on this line and see what is the correct type, just toknow ;)
I would use Convert.ToInt32() instead of cast:
intNumRecs =Convert.ToInt32( objInt);

this should work fine.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"WeiminZhang" <wz****@stssystems.com> wrote in message
news:01****************************@phx.gbl...
When I use the OleDb to connect a Oracle db, and use the ExecutScalar() method to get the count of a table, the
return value can't be cast to a data type, say int, while this works fine for a SQL server db using SqlCommand.

Following is the code example:

string strCnt = "SELECT count (*) from B_PlanSchemeItems"; string strSource="Provider=MSDAORA;Data
Source=avocet1;User ID=dbo; Password=mmsmms;";
OleDbConnection conn = new OleDbConnection(strSource);
conn.Open();

OleDbCommand cmdCount = new OleDbCommand(strCnt,conn);
object objInt;
objInt = cmdCount.ExecuteScalar();
intNumRecs = (int) (objInt);

The run time error occurs with message:
"Specified cast is not valid"

Following code works fine for a SQL server db:

string strCnt = "SELECT count (*) from B_PlanSchemeItems"; string
strSource="server=planserv1;uid=sa;pwd=sysadmin;da tabase=a vocet1";
SqlConnection myConn = new SqlConnection(strSource);
myConn.Open();

SqlCommand cmdCount = new SqlCommand(strCnt, myConn);
intNumRecs = (int) cmdCount.ExecuteScalar();

Can anyone help me on solving the cast problem in OleDb
case? Thanks.

.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cmo63126 | last post by:
Why am I not able to retrieve the value of ID (which does exist) using ExecuteScalar()? Do i simply have to use ExecuteReader() instead? My problem snippet: string mySQL = "SELECT ID FROM...
2
by: Dinesh | last post by:
Hi, I have one stored procedure in SQL server in which i have written one insert statement. Now in my cs file i pass the parameters require to execute that stored procedure and finaly by mistaken...
6
by: Max | last post by:
Anyone know why I'm always getting 0 returned? My stored procedure returns -1. Dim iErrorCode As Int32 iErrorCode = Convert.ToInt32(SqlHelper.ExecuteScalar(AppVars.strConn, _ "gpUpdateMember",...
7
by: Neven Klofutar | last post by:
Hi, I have a problem with SqlHelper.ExecuteScalar ... When I try to execute SqlHelper.ExecuteScalar I get this message: "System.InvalidCastException: Object must implement IConvertible.". ...
2
by: Martin | last post by:
Hi, I currently have an application that connects to an MS ACCESS database. This application uses an OLEDB connection string for MS ACCESS. Now, I'd like to upsize the application so I converted...
5
by: bienwell | last post by:
Hi all, I have a problem with using myCommand.ExecuteScalar(). My question is : If the Web setup is incorrect, does it make command ExecuteScalar() work improperly ?? In my program, I was...
3
by: charliewest | last post by:
Hi - I need to detect when the ExecuteScalar() method of the cmd object returns NULL. I have tried the below code, however, it always returns false (this is to say, that ExecuteScalar never...
8
by: erin.sebastian | last post by:
Hi all, I have a really silly problem that i can't find the answer too. I am working with VB.NET and i am trying to insert a new record into my access database (pretty easy right?) well i am...
3
by: =?Utf-8?B?TWlrZVM=?= | last post by:
Below is the code for 2 separate functions. The first one gets data from an Access database and works fine. The second one is almost a duplicate except it gets data from an SQL database. It...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.