Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 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
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 11th, 2009, 09:18 AM
Authorized User
 
Join Date: Mar 2007
Location: , , Denmark.
Posts: 52
Thanks: 6
Thanked 2 Times in 2 Posts
Default Returning a List<> using a DataReader, skips first item.

I'm building a search module, using the TheBeerHouse architecture. I've noticed something odd.


Code:
public static List<ProfilSearch> GetProfiles(string _search)
        {
            using (SqlConnection conn = new SqlConnection(_connString))
            {

                SqlCommand cmd = new SqlCommand(_search, conn);
                cmd.CommandType = CommandType.Text;
                conn.Open();

                IDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                    return GetProfilesCollectionFromReader(reader);
                else
                    return null;
            }
        }

        public static ProfilSearch GetProfileFromReader(IDataReader reader)
        {
            ProfilSearch _Profil = new ProfilSearch(
                (int)reader["profil_id"],
                reader["profil_type"].ToString(),
                (reader["profil_active"] == DBNull.Value ? 0 : (int)reader["profil_active"]),
                reader["profil_email"].ToString(),
            reader["profil_firstname"].ToString(),
            reader["profil_lastname"].ToString(),
            reader["profil_gender"].ToString(),
            (reader["profil_areaid"] == DBNull.Value ? 0 : (int)reader["profil_areaid"]),
            (reader["profil_lastupdate"] == DBNull.Value ? DateTime.Now : (DateTime)reader["profil_lastupdate"]),
            reader["profil_imageUrl"].ToString());
            return _Profil;
        }

        public static List GetProfilesCollectionFromReader(IDataReader reader)
        {
            List _profiles = new List();
            while (reader.Read())
                _profiles.Add(GetProfileFromReader(reader));
            return _profiles;
        }
The GetProfiles method is called from the codebehind, just like we would in TheBeerHouse.

Code:
List<ProfilSearch> _profiles = null;

_profiles = ProfilSearchManager.GetProfiles(search);

search is the query string which is built in codebehind, depending on the values picked by the user.
If I run that query directly in SQL Server,it retrieves, say 200 rows, and if I write out _profiles.Count, the number is 199 items (rows).

I just looked through my entire application, and in all the places I

return a List using a DataReader, the first item is missing.

Have any of you come across this in your work on TheBeerHouse, or anywhere else?
If so, do you have a solution for it?

Thnx

Last edited by philthy; June 11th, 2009 at 09:23 AM..
  #2 (permalink)  
Old June 11th, 2009, 02:20 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Philthy,
You are calling reader.Read() in GetProfiles(), which will set the pointer to the first item.
Then in GetProfilesCollectionFromReader, you are using while(reader.Read()), so the first time this is run, the pointer will be moved to the second item.
To fix this, you could simply change the while to a do..while:

Code:
do {
    _profiles.Add(GetProfileFromReader(reader));
} while (reader.Read());
Phil
The Following User Says Thank You to philip_cole For This Useful Post:
philthy (June 11th, 2009)
  #3 (permalink)  
Old June 11th, 2009, 03:38 PM
Authorized User
 
Join Date: Mar 2007
Location: , , Denmark.
Posts: 52
Thanks: 6
Thanked 2 Times in 2 Posts
Default

Thanks man, that works
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading data into List<T> rvincent C# 2005 8 June 14th, 2007 11:27 AM
generic List<T> Sort() shadowcodes C# 2005 2 February 15th, 2007 01:30 PM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
<marquee><b>About CHAT App. in PHP4</b></marquee> Ramkrishna PHP How-To 1 September 11th, 2004 07:01 AM



All times are GMT -4. The time now is 04:26 PM.


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