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 July 26th, 2007, 01:16 PM
Registered User
 
Join Date: Jul 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting ahead of IDataReader?

I am using this book and TBH model to produce a new site. In my site, I have 'Tasks' instead of 'Articles'. I've implemented the DAL and BLL with my own business rules and data structure, but using the Articles model.

In my UI I would like a Master/Child GridView/DetailsView of 'Task' objects. The dv will be getting its data key from the SelectedIndexChanged event of the gv, as usual. I am using object data sources for both dv and gv. Normally, using the DataSet designer and TableAdapters, a dv in ReadOnly mode will show the EmptyDataText for no data returned from it's ObjectDataSource (a null object?). The problem I am running into is this: using this DAL/BLL implementation, when I hit the dv on this page, I get

 'Invalid attempt to read when no data is present.'

The rest of the error is below.

OK, so what am I missing? Should I be able to get a null TaskDetails value back at some point in the BLL so I can tell the dv that there isn't any data yet? Where can this be implemented? Or am I just missing something obvious?

TIA,
Tara


------------------------------

Line 26: protected virtual TaskDetails GetTaskFromReader(IDataReader reader)
Line 27: {
Line 28: TaskDetails task = new TaskDetails(
Line 29: (int)reader["TaskId"],
Line 30: (string)reader["TaskInfo"],


Source File: h:\Inetpub\wwwroot\SEM5\App_Code\DAL\TaskProvider. cs Line: 28

Stack Trace:

[InvalidOperationException: Invalid attempt to read when no data is present.]
   System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) +137
   System.Data.SqlClient.SqlDataReader.get_Item(Strin g name) +24
   DAL.TaskProvider.GetTaskFromReader(IDataReader reader) in h:\Inetpub\wwwroot\SEM5\App_Code\DAL\TaskProvider. cs:28
   DAL.SqlClient.SqlTasksProvider.GetTaskById(Int32 taskId) in h:\Inetpub\wwwroot\SEM5\App_Code\DAL\SqlClient\Sql TasksProvider.cs:54
   BLL.Tasks.Task.GetTaskById(Int32 taskId) in h:\Inetpub\wwwroot\SEM5\App_Code\BLL\Tasks\Task.cs :242

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle._InvokeMethodFast(Objec t target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +0
   System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +72
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +358
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29
   System.Web.UI.WebControls.ObjectDataSourceView.Inv okeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +482
   System.Web.UI.WebControls.ObjectDataSourceView.Exe cuteSelect(DataSourceSelectArguments arguments) +2040
   System.Web.UI.DataSourceView.Select(DataSourceSele ctArguments arguments, DataSourceViewSelectCallback callback) +17
   System.Web.UI.WebControls.DataBoundControl.Perform Select() +149
   System.Web.UI.WebControls.BaseDataBoundControl.Dat aBind() +70
   System.Web.UI.WebControls.DetailsView.DataBind() +4
   Members_ManageTasks.getTaskDetail() in h:\Inetpub\wwwroot\SEM5\Members\ManageTasks.aspx.c s:40
   Members_ManageTasks.gvTasks_SelectedIndexChanged(O bject sender, EventArgs e) in h:\Inetpub\wwwroot\SEM5\Members\ManageTasks.aspx.c s:34
   System.Web.UI.WebControls.GridView.OnSelectedIndex Changed(EventArgs e) +105
   System.Web.UI.WebControls.GridView.HandleSelect(In t32 rowIndex) +89
   System.Web.UI.WebControls.GridView.HandleEvent(Eve ntArgs e, Boolean causesValidation, String validationGroup) +221
   System.Web.UI.WebControls.GridView.OnBubbleEvent(O bject source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.GridViewRow.OnBubbleEven t(Object source, EventArgs e) +117
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.ImageButton.OnCommand(Co mmandEventArgs e) +115
   System.Web.UI.WebControls.ImageButton.RaisePostBac kEvent(String eventArgument) +171
   System.Web.UI.WebControls.ImageButton.System.Web.U I.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102


--
Tara G Deschenes
Technimedia LLC
 
Old July 26th, 2007, 02:00 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Tara,

Not sure about your code that calls TaskDetails, but normally you need to call Read before you can access data in the reader. E.g.:

if (reader.Read())
{}

By calling Read, you start pointing to the first record. Each subsequent call to Read advances the record pointer one record.

Hope this helps,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old July 26th, 2007, 02:14 PM
Registered User
 
Join Date: Jul 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar, thanks for the response. I appreciate it.

In TBH code you have the DAL.ArticleProvider.cs class. In this class is a method GetArticleFromReader(IDataReader, reader). In the book this is what finally sends the reader through the record and assigns the data to the ArticleDetails object, and this method does not use 'if (reader.Read())'... It specifically only returns the ArticleDetails object.

So, to solve this, where should I add the 'if (reader.Read())' in terms of TBH Articles class? Where is the best spot to check for this? In BLL.Articles.Article?

Thanks again for your input.

Tara

--
Tara G Deschenes
Technimedia LLC
 
Old July 26th, 2007, 02:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I guess it depends on your design and preferences.

IMO, GetTaskFromReader would be a good place. It allows you to see if you have any data and then return something else if you don't, like null or a new, empty TaskDetails instance instead.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old July 26th, 2007, 03:43 PM
Registered User
 
Join Date: Jul 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, I agree. And when I try that - send a new empty instance of 'TaskDetails' (or ArticleDetails) - at that point in the logic, I receive an error: Value cannot be null. Parameter name: container. So I have been trying to pick back through the DAL and BLL layers to find the appropriate spot in the class for the reader check, and in each I am not getting a straightforward result. (One way I set it up and received no null or empty container errors, but then my gridview has a row of blank data, which isn't right either.) This is why I think I am missing something here.

In other words, will TBH site never return a null Article instance? If this is the case, this error should also occur in TBH, right? But that scenario never happens because TBH does not have a Master/Child dv/gv page?

(I guess I am just looking for a yes or no. lol :D I'm ready to bang my head on the desk.)

Thanks again!
Tara

--
Tara G Deschenes
Technimedia LLC
 
Old July 26th, 2007, 03:52 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I don't know the inner workings of TBH, so I can't say anything useful about that.

Maybe it only calls for items in knows (or assumes) exist? E.g. call individual items based on IDs that comes from a list that comes from the database? Just a guess.....

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old July 26th, 2007, 04:08 PM
Registered User
 
Join Date: Jul 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Imar
 I don't know the inner workings of TBH, so I can't say anything useful about that.

Maybe it only calls for items in knows (or assumes) exist? E.g. call individual items based on IDs that comes from a list that comes from the database? Just a guess.....
Right! But I think this is the exact scenario I have with the GridView and DetailsView - the grid only has the IDs that exist in the database, and when I select one, it should return the data associated with it. But instead when I select I get the 'Invalid attempt to read...'. So why does the DataReader not have any data to read at that point? It should. I debug and I can see the ID being passed and the SP called. The data is in the DB. But there is a disconnect here somewhere. The TBH code doesn't specifically tell the IDataReader to read, it just creates an instance and grabs the data from the reader, no loop.

protected virtual TaskDetails GetTaskFromReader(IDataReader reader)
        {
            TaskDetails task = new TaskDetails(
            (int)reader["TaskId"],
            (string)reader["TaskInfo"],
            (string)reader["TaskType"],
            (int)reader["TaskStatus"],
            (string)reader["Community"],
            (string)reader["Apartment"],
            (DateTime)reader["EventDate"],
            (string)reader["ResponseTime"],
            (string)reader["CallVolume"],
            (string)reader["AssignedTo"],
            (string)reader["AssignedToEmail"],
            (DateTime)reader["AssignedDate"],
            (string)reader["Message"],
            (string)reader["IconImageUrl"]
            );
            return task;
        }

if I add a while(reader.Read()) or if (reader.Read()) in there, not all code paths return a value unless I send an empty instance out, then I get the null container issue again.

Of course, all of this is centric to TBH code. Does anyone here have experience with the code that can shed some light?

Imar, again, thanks for helping me talk this out! :)

Tara

--
Tara G Deschenes
Technimedia LLC
 
Old July 26th, 2007, 04:08 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Oh, BTW: this
Quote:
quote:One way I set it up and received no null or empty container errors, but then my gridview has a row of blank data, which isn't right either.)
seems to make sense. When your method returns a new, empty instance of your class, it makes sense that is shows up as em empty row in the GridView....

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old July 26th, 2007, 04:14 PM
Registered User
 
Join Date: Jul 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Imar
 Oh, BTW: this
Quote:
quote:One way I set it up and received no null or empty container errors, but then my gridview has a row of blank data, which isn't right either.)
seems to make sense. When your method returns a new, empty instance of your class, it makes sense that is shows up as em empty row in the GridView....


Yes! Again, it seems to be correct - the instance should be there because I made it and sent it - but I don't want empty data rows in my gridview, right? How would I weed those out? And this won't happen if I use the TableAdapter/DataSet/ObjectDataSource method and have .net write the code - the hooked up detailsview or gridview knows there is no data. What ever is sent back is the right kind of 'nothing', I guess.

:D

--
Tara G Deschenes
Technimedia LLC
 
Old July 26th, 2007, 04:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If you think that an empty object isn't acceptable; that is, you only want to return valid instances from the method, you could throw an error instead:

E.g.

if (reader.Read())
{
TaskDetails task = new TaskDetails(
  (int)reader["TaskId"],
  (string)reader["TaskInfo"],
  ....
  return task;
}
throw new Exception("Invalid attempt to read item");

That solves the "Not all code paths return a value" error.

But it seem that GetTaskFromReader is called with an empty reader. Are you absolutely sure the code that accesses the database returns a record?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
The IDataReader....a suggestion. Tremmorkeep BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 February 22nd, 2008 11:37 AM
Type Ahead Drop Down List kdkcchoco ASP.NET 1.0 and 1.1 Professional 43 February 20th, 2007 03:56 PM





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