<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx</link><description>By Nannette Thacker There are several ways to retrieve values from a DataReader. The first method is by using the index and looping through the fields in order as retrieved within the Select statement. Starting with the 0 index, you may use the GetValue</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx#6712404</link><pubDate>Thu, 30 Oct 2008 16:44:57 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6712404</guid><dc:creator>JamesNT</dc:creator><author>JamesNT</author><description>&lt;p&gt;While I have found this post to be most informative, it may be worth mentioning that lines such as:&lt;/p&gt;
&lt;p&gt;lastname = reader.item(&amp;quot;lastname&amp;quot;)&lt;/p&gt;
&lt;p&gt;will generate an error if OPTION STRICT is on.&lt;/p&gt;
&lt;p&gt;You will either have to turn off OPTION STRICT (not recommended) or have code like:&lt;/p&gt;
&lt;p&gt;with reader&lt;/p&gt;
&lt;p&gt; &amp;nbsp; if not .item(&amp;quot;birthdate&amp;quot;) is dbnull.value then&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; birthdate = .getdatetime(.getordinal(&amp;quot;birthdate&amp;quot;))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; end if&lt;/p&gt;
&lt;p&gt;end with&lt;/p&gt;
&lt;p&gt;You may also need to put in additional checks for the datatype of the fields in the datareader if you are NOT gauranteed to know what they are. &amp;nbsp;We are, of course, assuming that the database the reader got its information from has the right datatypes in the right fields (i.e. that the birthdate field is of type datetime).&lt;/p&gt;
&lt;p&gt;Please correct me if I&amp;#39;m wrong.&lt;/p&gt;
&lt;p&gt;JamesNT&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6712404" width="1" height="1"&gt;</description></item><item><title>re: Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx#6649607</link><pubDate>Wed, 01 Oct 2008 04:54:49 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6649607</guid><dc:creator>Dee</dc:creator><author>Dee</author><description>&lt;p&gt;When using &lt;/p&gt;
&lt;p&gt;lastname = GetValueFromReader(reader,&amp;quot;lastname&amp;quot;);&lt;/p&gt;
&lt;p&gt;I get an error:&lt;/p&gt;
&lt;p&gt;Compiler Error Message: CS0103: The name &amp;#39;GetValueFromReader&amp;#39; does not exist in the current context&lt;/p&gt;
&lt;p&gt;When looking up &amp;quot;GetValueFromReader&amp;quot; on Microsoft (&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader_methods.aspx"&gt;msdn.microsoft.com/.../system.data.oledb.oledbdatareader_methods.aspx&lt;/a&gt;) it does not list this as a method of the datareader class.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6649607" width="1" height="1"&gt;</description></item><item><title>re: Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx#6491031</link><pubDate>Wed, 06 Aug 2008 15:28:04 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6491031</guid><dc:creator>Axel</dc:creator><author>Axel</author><description>&lt;p&gt;You also forgot about the possibility to do it like this:&lt;/p&gt;
&lt;p&gt;SqlDataReader dr = cmd.ExecuteReader();&lt;/p&gt;
&lt;p&gt;while (dr.Read())&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;lastName = dr.GetString(dr.GetOrdinal(&amp;quot;LastName&amp;quot;));&lt;/p&gt;
&lt;p&gt; &amp;nbsp;firstName = dr.GetString(dr.GetOrdinal(&amp;quot;FirstName&amp;quot;));&lt;/p&gt;
&lt;p&gt; &amp;nbsp;age = dr.GetInt32(dr.GetOrdinal(&amp;quot;Age&amp;quot;));&lt;/p&gt;
&lt;p&gt; &amp;nbsp;amountOwed = dr.GetDouble(dr.GetOrdinal(&amp;quot;AmountOwed&amp;quot;));&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;etc. etc.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6491031" width="1" height="1"&gt;</description></item><item><title>re: Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx#6449252</link><pubDate>Sun, 27 Jul 2008 09:55:47 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6449252</guid><dc:creator>fdf</dc:creator><author>fdf</author><description>&lt;p&gt;PhoneNumber += rdr.GetValue(rdr.GetString(PhoneNumber))+&amp;quot;\n&amp;quot;;&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6449252" width="1" height="1"&gt;</description></item><item><title>re: Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx#5815630</link><pubDate>Mon, 18 Feb 2008 19:50:52 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5815630</guid><dc:creator>nannette</dc:creator><author>nannette</author><description>&lt;p&gt;Great suggestion Travis! Thanks! I have also added that to my post.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5815630" width="1" height="1"&gt;</description></item><item><title>re: Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx#5795225</link><pubDate>Fri, 15 Feb 2008 18:24:58 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5795225</guid><dc:creator>Travis</dc:creator><author>Travis</author><description>&lt;p&gt;if (reader[&amp;quot;lastname&amp;quot;] != DBNull.Value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; lastname = reader[&amp;quot;lastname&amp;quot;].ToString();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Why all the code? &amp;nbsp;Just do this, it handles nulls nicely unlike .ToString():&lt;/p&gt;
&lt;p&gt;lastname = Convert.ToString(reader[&amp;quot;lastname&amp;quot;]);&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5795225" width="1" height="1"&gt;</description></item><item><title>re: Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx#5789161</link><pubDate>Thu, 14 Feb 2008 20:51:27 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5789161</guid><dc:creator>nannette</dc:creator><author>nannette</author><description>&lt;p&gt;Thanks everyone for your feedback and valuable input. I have included your comments at the end of my article so they won't be missed.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5789161" width="1" height="1"&gt;</description></item><item><title>re: Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx#5788968</link><pubDate>Thu, 14 Feb 2008 20:34:24 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5788968</guid><dc:creator>nannette</dc:creator><author>nannette</author><description>&lt;p&gt;AndrewSeven,&lt;/p&gt;
&lt;p&gt;Good points! Thanks!&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5788968" width="1" height="1"&gt;</description></item><item><title>re: Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx#5788769</link><pubDate>Thu, 14 Feb 2008 20:16:39 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5788769</guid><dc:creator>AndrewSeven</dc:creator><author>AndrewSeven</author><description>&lt;p&gt;Isn't there a small cost because you are hitting the reader twice for each field?&lt;/p&gt;
&lt;p&gt;if (reader[&amp;quot;lastname&amp;quot;] != DBNull.Value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;lastname = reader[&amp;quot;lastname&amp;quot;].ToString();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;vs.&lt;/p&gt;
&lt;p&gt;object val = reader[&amp;quot;lastname&amp;quot;];&lt;/p&gt;
&lt;p&gt;if (val != DBNull.Value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;lastname = val;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;If your concern is readability, you should probably encapsulate the null test so you can write: lastname = GetValueFromReader(reader,&amp;quot;lastname&amp;quot;);&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5788769" width="1" height="1"&gt;</description></item><item><title>re: Retrieving Values from a DataReader</title><link>http://weblogs.asp.net/nannettethacker/archive/2008/02/14/retrieving-values-from-a-datareader.aspx#5788644</link><pubDate>Thu, 14 Feb 2008 20:02:43 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5788644</guid><dc:creator>nannette</dc:creator><author>nannette</author><description>&lt;p&gt;Marneus,&lt;/p&gt;
&lt;p&gt;You are correct. Readers may refer to this document on the GetOrdinal()&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getordinal.aspx" target="_new" rel="nofollow"&gt;msdn2.microsoft.com/.../system.data.sqlclient.sqldatareader.getordinal.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Please note this comment on efficiency:&lt;/p&gt;
&lt;p&gt;&amp;quot;Because ordinal-based lookups are more efficient than named lookups, it is inefficient to call GetOrdinal within a loop. Save time by calling GetOrdinal once and assigning the results to an integer variable for use within the loop.&amp;quot;&lt;/p&gt;
&lt;p&gt;From the article, here is the usage:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;&amp;#39; Call GetOrdinal and assign value to variable.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;Dim&lt;/span&gt; customerID &lt;span style="COLOR:blue;"&gt;As&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;Integer&lt;/span&gt; = reader.GetOrdinal(&lt;span style="COLOR:maroon;"&gt;&amp;quot;CustomerID&amp;quot;&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;&amp;#39; Use variable with GetString inside of loop.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;While&lt;/span&gt; reader.Read()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span style="COLOR:maroon;"&gt;&amp;quot;CustomerID={0}&amp;quot;&lt;/span&gt;, reader.GetString(customerID))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;End&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;While&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5788644" width="1" height="1"&gt;</description></item></channel></rss>