Wrox Programmer Forums
|
BOOK: Professional .NET 2.0 Generics
This is the forum to discuss the Wrox book Professional .NET 2.0 Generics by Tod Golding; ISBN: 9780764559884
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional .NET 2.0 Generics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 21st, 2006, 09:52 AM
Registered User
 
Join Date: Jun 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Casting

Public Function GetItems(Of T)(ByVal sql As String) As Collection(Of T)
        Dim selectCmd As New OleDbCommand(sql, dbConn)
        Dim retVal As New Collection(Of T)
        Dim dataReader As OleDbDataReader = selectCmd.ExecuteReader()
        While (dataReader.Read() = True)
            retVal.Add(DirectCast(dataReader(0), T))
        End While

        Return retVal
    End Function

I keep on getting an error:

Message="Unable to cast object of type 'System.Int32' to type 'Architecture.Employees'." What am i doing wrong?

 
Old June 21st, 2006, 10:50 AM
Registered User
 
Join Date: Jun 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To make my statement clearer above please give an example of passing a Person class to the function and casting a reader object to the class. Thanks.

 
Old November 13th, 2007, 04:55 PM
Registered User
 
Join Date: Nov 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am having a similar error.
I am using an OracleDataReader.


FREDYCOREA
 
Old November 19th, 2007, 04:49 PM
Registered User
 
Join Date: Nov 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Did anybody find out why this code do not work.
I tried it several ways to no avail, still the same casting error message.


FREDYCOREA





Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting - Can you help koco C# 1 September 24th, 2007 08:41 AM
Casting Problem liorlankri ASP.NET 1.x and 2.0 Application Design 2 January 2nd, 2007 03:31 AM
casting ravibodani Access 1 April 4th, 2006 01:19 PM
Casting question shantawn J2EE 4 August 2nd, 2005 02:40 AM
Dynamic Casting pareshsoft C# 2 December 1st, 2004 12:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.