Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2004

Tip: Looking for answers? Try searching our database.

ExecuteScalar() Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Beringer - 20 Sep 2004 06:10 GMT
Can someone tell me why I get a run time exception of "Invalid Cast" on the
following code sample on the second line?:

SqlCommand identity = new SqlCommand("SELECT @@Identity", conn);
int temp = (int)(identity.ExecuteScalar());

I Looked in the sample code that somes with VS and this cast method is used
in an example provided by MS (page title "Executing a Data Command that
Returns a Single Value").  If temp is an object and not int,  the code works
and you can see the integer value in the debugger.

BTW:  the Select command is supposed to return the identity value of the
last inserted record.  And I found this at the "CodeGuru" in an article
written by Karl Moore
(www.developer.com/net/net/article.php/11087_2201321_2).

Thank you in advance,

Eric
Jakob Christensen - 20 Sep 2004 08:01 GMT
The object returned by your call to ExecuteScalar is of type Decimal.  You
can use Convert.ToInt32 instead of just casting, or you can change the type
of your temp variable to Decimal.

Regards, Jakob.

> Can someone tell me why I get a run time exception of "Invalid Cast" on the
> following code sample on the second line?:
[quoted text clipped - 15 lines]
>
> Eric

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.