Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 20th, 2008, 12:22 AM
Authorized User
 
Join Date: Jul 2007
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loading Entity /w No Data

It seems that in most of the examples in the book, the queries issued through the DAL return at least one or more records. In my case, I have a method which queries the database for 'Players' based on first name and last name. Sometimes, no players are returned, and when this happens I'm getting an exception when trying to convert the database data to en entity object as outlined in the book.

Specifically, I'm getting an exception in the GetPlayerFromReader method as I try to build the entity object:

Code:
    public override PlayerDetails GetPlayerByFirstNameLastName(string firstName, string lastName)
    {
      using (SqlConnection cn = new SqlConnection(this.ConnectionString))
      {
        SqlCommand cmd = new SqlCommand("Sheets_GetPlayerByFirstNameLastName", cn);
        cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = firstName;
        cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = lastName;
        cmd.CommandType = CommandType.StoredProcedure;
        cn.Open();
        return GetPlayerFromReader(ExecuteReader(cmd));
      }
    }


    protected virtual PlayerDetails GetPlayerFromReader(IDataReader reader)
    {
      PlayerDetails player = new PlayerDetails(
          (int)reader["PlayerID"],
          reader["PositionCode"].ToString(),
          reader["FirstName"].ToString(),
          reader["LastName"].ToString(),
          reader["MiddleName"].ToString(),
          reader["TeamCode"].ToString(),
          (short)reader["Number"],
          (DateTime)reader["FirstYear"]);
      return player;
    }
The exception I get says: "Invalid attempt to read when no data is present." because no records are returned.

Should I be checking the 'depth' property of the Reader object here to see if there is any data before creating the Player entity object?

Thanks

  #2 (permalink)  
Old January 20th, 2008, 02:27 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

As you've already discovered, the GetPlayerFromReader method will throw an exception is there is no data.

What you should do is to use the Read() method on the DataReader BEFORE passing it to the GetPlayerFromReader method. If there is no data, the Read() method will return false. In this case, you can just return null from your GetPlayerByFirstName method, indicating there is no such player with the given firstName/lastName combo.

public override PlayerDetails GetPlayerByFirstNameLastName(string firstName, string lastName)
{
  using (SqlConnection cn = new SqlConnection(this.ConnectionString))
  {
    SqlCommand cmd = new SqlCommand("Sheets_GetPlayerByFirstNameLastName", cn);
    cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = firstName;
    cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = lastName;
    cmd.CommandType = CommandType.StoredProcedure;
    cn.Open();
    IDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
    if (reader.Read())
        return GetPlayerFromReader(reader);
    else
        return null;
  }
}
 


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
confusing in ADO.NET Entity Data Model,Linq to sql angelboy ADO.NET 5 November 21st, 2008 05:01 PM
Loading Lots of Data bmains C# 2005 1 January 28th, 2008 12:57 PM
Ch 3 - incremental data loading Johnslg BOOK: Expert SQL Server 2005 Integration Services ISBN: 978-0-470-13411-5 0 July 9th, 2007 01:08 PM
Loading data from SQL levinll Pro VB Databases 0 January 25th, 2007 04:09 PM
Difference between Entity and Entity type arshad mahmood C++ Programming 0 May 8th, 2004 12:34 AM



All times are GMT -4. The time now is 11:43 PM.


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