Search the web
Sign In
New User? Sign Up
StrongTypes · Free .NET Generics Help
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Generic SqlDataReader to List   Message List  
Reply | Forward Message #21 of 44 |
RE: [StrongTypes] Generic SqlDataReader to List

Since GetValue returns an object, you'd have to cast it to T.

public T RStoList<T>(SqlDataReader rdr, int index)
{
List<T> list = new List<T>();

while (rdr.Read())
{
list.Add((T)rdr.GetValue(index));
}

return list;
}

HTH,
Ryan


Thank you,
Ryan Olshan
Website - http://www.StrongTypes.com <http://www.strongtypes.com/>
Group - http://groups.yahoo.com/group/StrongTypes
Blog - http://blogs.strongcoders.com/blogs/ryan/


_____

From: StrongTypes@yahoogroups.com [mailto:StrongTypes@yahoogroups.com] On
Behalf Of sureshotnj
Sent: Friday, May 12, 2006 1:02 PM
To: StrongTypes@yahoogroups.com
Subject: [StrongTypes] Generic SqlDataReader to List


I'm trying to loop through a reader and put all the values into a
generic list.

public T RStoList<T>(SqlDataReader rdr, int index)
{
List<T> list = new List<T>();

while (rdr.Read())
{
list.Add(rdr.GetValue(index));
}

return list;
}

It's not letting me add the object to the list though. I need to
somehow convert the value from the reader into the right type T.

Tried this without success:

list.Add(Convert.ChangeType(rdr.GetValue(index),typeof(T)));

Ideas?










SPONSORED LINKS
Object
<http://groups.yahoo.com/gads?t=ms&k=Object+oriented&w1=Object+oriented&w2=O
bject+oriented+design&w3=Object+oriented+language&w4=Object+oriented+trainin
g&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6&s=172&.sig=
uLqg2pfGRplGmMsp6kJtVQ> oriented Object
<http://groups.yahoo.com/gads?t=ms&k=Object+oriented+design&w1=Object+orient
ed&w2=Object+oriented+design&w3=Object+oriented+language&w4=Object+oriented+
training&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6&s=17
2&.sig=pGWYLp0sQpJs3JoBMlqbdA> oriented design Object
<http://groups.yahoo.com/gads?t=ms&k=Object+oriented+language&w1=Object+orie
nted&w2=Object+oriented+design&w3=Object+oriented+language&w4=Object+oriente
d+training&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6&s=
172&.sig=oMxENFA3eiv-nKTwqk3z6Q> oriented language
Object
<http://groups.yahoo.com/gads?t=ms&k=Object+oriented+training&w1=Object+orie
nted&w2=Object+oriented+design&w3=Object+oriented+language&w4=Object+oriente
d+training&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6&s=
172&.sig=WW87rn12Wb695fEwKFWRNg> oriented training Object
<http://groups.yahoo.com/gads?t=ms&k=Object+oriented+methodology&w1=Object+o
riented&w2=Object+oriented+design&w3=Object+oriented+language&w4=Object+orie
nted+training&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6
&s=172&.sig=ZfydFONc8Llu8PBIVAyRuQ> oriented methodology Object
<http://groups.yahoo.com/gads?t=ms&k=Object+oriented+tutorial&w1=Object+orie
nted&w2=Object+oriented+design&w3=Object+oriented+language&w4=Object+oriente
d+training&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6&s=
172&.sig=5RlQ7dXcE52DzP8Rq1Dbdg> oriented tutorial

_____

YAHOO! GROUPS LINKS



* Visit your group "StrongTypes
<http://groups.yahoo.com/group/StrongTypes> " on the web.


* To unsubscribe from this group, send an email to:
StrongTypes-unsubscribe@yahoogroups.com
<mailto:StrongTypes-unsubscribe@yahoogroups.com?subject=Unsubscribe>


* Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
<http://docs.yahoo.com/info/terms/> .


_____




[Non-text portions of this message have been removed]




Mon May 15, 2006 2:01 am

teranetlists
Offline Offline
Send Email Send Email

Forward
Message #21 of 44 |
Expand Messages Author Sort by Date

I'm trying to loop through a reader and put all the values into a generic list. public T RStoList<T>(SqlDataReader rdr, int index) { List<T> list = new...
sureshotnj
Offline Send Email
May 15, 2006
1:58 am

Since GetValue returns an object, you'd have to cast it to T. public T RStoList<T>(SqlDataReader rdr, int index) { List<T> list = new List<T>(); while...
Ryan Olshan
teranetlists
Offline Send Email
May 15, 2006
2:01 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help