Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 4th, 2011, 04:00 PM
Authorized User
Points: 224, Level: 4
Points: 224, Level: 4 Points: 224, Level: 4 Points: 224, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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?
Reply With Quote
  #2 (permalink)  
Old October 4th, 2011, 04:07 PM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #3 (permalink)  
Old October 4th, 2011, 05:16 PM
Authorized User
Points: 224, Level: 4
Points: 224, Level: 4 Points: 224, Level: 4 Points: 224, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #4 (permalink)  
Old October 5th, 2011, 03:24 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #5 (permalink)  
Old October 5th, 2011, 05:44 AM
Friend of Wrox
Points: 547, Level: 8
Points: 547, Level: 8 Points: 547, Level: 8 Points: 547, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #6 (permalink)  
Old October 5th, 2011, 09:58 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.