Wrox Programmer Forums
| 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
 
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

 
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;
  }
}




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





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