Wrox Programmer Forums
|
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 December 29th, 2006, 11:59 AM
Authorized User
 
Join Date: Mar 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default System.IndexOutOfRangeException

I have modified some of the code from the News article section of this book for my own situation. However I have the following problem. I have a stored procedure which returns a number of field values from the database. (eg, field1, field2, field3) but not field4 or field5 as they are not required. However this throws an error in the ArticleProvide class in the method GetArticleFromReader(IDataReader reader, bool readBody). The error line is that of the field not being returned. So for example if field5 is not being returned, the following error is thrown System.IndexOutOfRangeException: field5

If i return all the fields from the database table there is no error.

Any ideas why this is so? And to remind you that I have altered some of the code so I feel the source of the problem may be in that.

 
Old January 1st, 2007, 04:13 PM
Authorized User
 
Join Date: Mar 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anyone have any ideas for me or want more information or something explained?

 
Old January 5th, 2007, 06:54 AM
Authorized User
 
Join Date: Mar 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Please Help!

 
Old January 5th, 2007, 04:46 PM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to vantoko
Default

you could solve this by giving those properties a default (empty) value ?
if your code expecs a value, you should provide a value.
wat you do is
(reader["field5"] == DBNull.Value ? 0 : (int)reader["field5"])

koen

 
Old January 5th, 2007, 05:22 PM
Authorized User
 
Join Date: Mar 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much for your help koen, much appreciated.

However unfortunately it did not change the situation, still have the same error.

Any other ideas?

 
Old January 8th, 2007, 03:10 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to vantoko
Default

could you post the SP you have and the GetArticleFromReader code ?

 
Old January 8th, 2007, 08:05 AM
Authorized User
 
Join Date: Mar 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The following is the GetArticleFromReader code.

 protected virtual ArticleDetails GetArticleFromReader(IDataReader reader)
      {
         return GetArticleFromReader(reader, true);
      }
      protected virtual ArticleDetails GetArticleFromReader(IDataReader reader, bool readBody)
      {
            ArticleDetails article = new ArticleDetails(
            (int)reader["ArticleID"],
            (DateTime)reader["AddedDate"],
            reader["AddedBy"].ToString(),
            (int)reader["CategoryID"],
            reader["CategoryTitle"].ToString(),
            reader["Title"].ToString(),
            reader["Abstract"].ToString(),
            null,
            reader["Country"].ToString(),
            reader["State"].ToString(),
            reader["City"].ToString(),
            (reader["ReleaseDate"] == DBNull.Value ? DateTime.Now : (DateTime)reader["ReleaseDate"]),
            (reader["ExpireDate"] == DBNull.Value ? DateTime.MaxValue : (DateTime)reader["ExpireDate"]),
            (bool)reader["Approved"],
            (bool)reader["Listed"],
            (bool)reader["CommentsEnabled"],
            (bool)reader["OnlyForMembers"],
            (int)reader["ViewCount"],
            (int)reader["Votes"],
            (int)reader["TotalRating"]);

         if (readBody)
            article.Body = reader["Body"].ToString();

         return article;
        }

        protected virtual List<ArticleDetails> GetArticleCollectionFromReader(IDataReader reader)
      {
         return GetArticleCollectionFromReader(reader, true);
      }
      protected virtual List<ArticleDetails> GetArticleCollectionFromReader(IDataReader reader, bool readBody)
      {
         List<ArticleDetails> articles = new List<ArticleDetails>();
         while (reader.Read())
            articles.Add(GetArticleFromReader(reader, readBody));
         return articles;
      }

And the following is sample of a stored procedure

ALTER PROCEDURE dbo.tbh_Articles_GetArticleByID
(
   @ArticleID int
)
AS
SET NOCOUNT ON

SELECT tbh_Articles.ArticleID, tbh_Articles.AddedDate, tbh_Articles.AddedBy, tbh_Articles.CategoryID, tbh_Articles.Title, tbh_Articles.Abstract, tbh_Articles.Body,
         tbh_Articles.Country, tbh_Articles.State, tbh_Articles.City, tbh_Articles.ReleaseDate, tbh_Articles.ExpireDate, tbh_Articles.Approved,
         tbh_Articles.Listed, tbh_Articles.CommentsEnabled, tbh_Articles.OnlyForMembers, tbh_Articles.ViewCount, tbh_Articles.Votes, tbh_Articles.TotalRating,
         tbh_Categories.Title AS CategoryTitle
FROM tbh_Articles INNER JOIN
      tbh_Categories ON tbh_Articles.CategoryID = tbh_Categories.CategoryID
   WHERE ArticleID = @ArticleID

Please help!

 
Old January 8th, 2007, 02:25 PM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to vantoko
Default

is the release date or expiredate failing, or is it another field ?

 
Old January 8th, 2007, 06:47 PM
Authorized User
 
Join Date: Mar 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If i leave out any field i get the SystemIndexOutOfRangeExcepetion for that field. So if I used Select * from tbh_Articles INNER JOIN
      tbh_Categories ON tbh_Articles.CategoryID = tbh_Categories.CategoryID
   WHERE ArticleID = @ArticleID

then there would be no problem but If i selected them all bar tbh_Articles.Title for example then I would get a SystemIndexOutOfRange for reader["Title"].ToString().

 
Old January 10th, 2007, 09:59 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to vantoko
Default

this architecture expects a reference from the stored procedure for all field described in the class. this is an interface contract between the layers






Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IndexOutOfRangeException dhoward VB.NET 2002/2003 Basics 4 February 20th, 2008 04:29 PM
Insert System date and System Time -Form _TextBox cnkumar74 VB How-To 14 February 14th, 2007 10:52 AM
System.IndexOutOfRangeException: - Please Help!!! olud ASP.NET 1.0 and 1.1 Basics 1 January 30th, 2007 02:56 PM
IndexOutOfRangeException - Chp 13, Page 446 aleahy BOOK: Beginning ASP.NET 1.0 1 November 5th, 2003 08:40 AM





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