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]