Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 September 8th, 2010, 03:33 PM
Authorized User
 
Join Date: Aug 2010
Posts: 19
Thanks: 6
Thanked 0 Times in 0 Posts
Default Chapter 13 Page 436 Single operator

Hi Imar,

In attempting to reproduce your example of Single and SingleOrDefault operators on page 436, I get the following error message: "Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource." My assumption is that it is attempting to return a collection of results, but fails because I'm telling it to return a single result. I'm hoping to understand what needs to be done to make this work.

My code looks identical to what you have on the page:

Code:
Partial Class All
  Inherits BasePage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Using myDataContext As New PlanetWroxDataContext()
            Dim review22 = (From r In myDataContext.Reviews _
            Where r.Id = 22 _
            Select r).Single()

            GridView1.DataSource = review22
            GridView1.DataBind()

        End Using
    End Sub
End Class
Thanks,

-Chris
 
Old September 9th, 2010, 03:11 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Chris,
Quote:
My assumption is that it is attempting to return a collection of results, but fails because I'm telling it to return a single result.
Nope, your LINQ query is fine. You're telling it to return a single instance, and that's exactly what it does.

The problem is what you do with the returned instance:

Quote:
GridView1.DataSource = review22
The GridView is designed to work with a collection, not with a single instance. So the error you get is from the GridView, not from the query.

Your code would work if you used the Review to populate, say a TextBox:

TextBox1.Text = review22.Title

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
caiman (September 9th, 2010)
 
Old September 9th, 2010, 09:52 AM
Authorized User
 
Join Date: Aug 2010
Posts: 19
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Yes, that helps!

Thanks as always.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in chapter 13 page 451 Try it out morteza BOOK: Beginning ASP.NET 4 : in C# and VB 3 June 6th, 2010 08:11 AM
"Single and SingleOrDefault" (p. 436) roman BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 July 3rd, 2008 07:48 PM
Chapter 13 page 444 DRAYKKO Beginning VB 6 1 April 19th, 2006 07:10 PM





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