Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 October 4th, 2011, 04:00 PM
Authorized User
 
Join Date: Sep 2011
Posts: 41
Thanks: 3
Thanked 4 Times in 3 Posts
Default Assigning data to a label

Hello,

PROBLEM: How to bind data to a label, without using a view control.

CONTEXT: Chp 14, on the "Manage Photo Album" page, I would like to place the Album title at the top of the page.

WHAT I DID: I use a panel and place a label inside so I can easily center the label. I use a LinQ query to get the Name of the Album. But I cannot bind the name to the label.
Code:
using (PlanetWroxEntities myAlbum = new PlanetWroxEntities())
        {
            var albumName = from m in myAlbum.PhotoAlbums
                            where m.Id == albumId       // albumId retrieved from querystring
                            select new { m.Name };
THINGS I TRIED: I've tried using the Eval in the Markup View on the Label control and all sorts of scenarios. But I don't know if I'm assigning the datasource correctly, etc. I can get the Album name to display if I use view control (e.g., formview) at the top of the page and place a label inside. But I figured, there's probably just a simple way to bind data to a label without having to use a view control. Or must I use a view control of some sort?

Does anyone have an answer?
 
Old October 4th, 2011, 04:07 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What about simply assigning it to its Text property?

Code:
 
var albumName = (from m in myAlbum.PhotoAlbums
                            where m.Id == albumId       // albumId retrieved from querystring
                            select m.Name).FirstOrDefault();
myLabel.Text = albumName;
Cheers,

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!
 
Old October 4th, 2011, 05:16 PM
Authorized User
 
Join Date: Sep 2011
Posts: 41
Thanks: 3
Thanked 4 Times in 3 Posts
Default

Gracias,

That is pretty much what I was looking for. Actually, I did try to assign it to Label.Text ... but I didn't use the FirstorDefault() and well, it forced me to convert albumName.toString() ... obviously, the displayed text was not what I wanted ...

Thank you sir.
 
Old October 5th, 2011, 03:24 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome. The first quesry returns a collection of string (even though there's only one item in it) which you cannot assign to a Label. By using FirstOrDefault, you only get a single item, or null when the item could not be found.

Cheers,

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!
 
Old October 5th, 2011, 05:44 AM
Friend of Wrox
 
Join Date: Apr 2010
Posts: 125
Thanks: 20
Thanked 3 Times in 3 Posts
Default

Hi,
Is there difference between
Code:
select new { m.Name }
and
Code:
select m.Name
?
__________________
Please excuse me for poor and bad English.
 
Old October 5th, 2011, 09:58 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yeah, there's a difference.

Code:
 
select new { m.Name }
results in an anonymous object with a property called Name that holds the name. If you output this directly, you get something like { Name = "The name" }

Code:
 
select m.Name
returns just a string holding the name you selected.

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Putting Data Label in Chart amitabha.chakraborty C# 2008 aka C# 3.0 0 May 8th, 2009 01:51 AM
How to bind data from a db to a label in c# 3.5 bex ASP.NET 3.5 Basics 23 March 4th, 2009 03:37 PM
Assigning null values in typed data sets Utsav.Kedia General .NET 0 January 16th, 2006 03:02 PM





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