ASP.NET: Retrieve a lookup value to display in a gridview column

Posted on 2011-05-09
Medium Priority
Last Modified: 2012-06-27
I am building my first web application, using ASP.NET 4 with VB. I'm also using LINQ-to-Entities.

The page I'm working on now will be a listing of Contacts, a gridview based on the "Person" entity, and will display the following columns:


The problem I'm running into is that while FirstName, LastName, and BusName are retrieved directly from the People table (via "Person" entity data source), the City is stored as CityID and Region as RegionID. These ID numbers are linked to an "AttributeName" table, which stores each user's list of cities, regions, phone types, etc. This gridview should not display the CityID and RegionID integers, but should instead display the AttributeName (string) that corresponds to these ID values.

I have set up a template field in the gridview to display the City name, but I can't figure out how to get the name. I have tried both a function and a stored procedure to return the name when provided the ID. I tried calling these from the template field, but it blows up when the CityID is null (which it frequently is).

What would be the best approach to populating a gridview column with the result of a lookup function (or SP), which would also handle nulls? Is there a way to integrate it into the Entity Data Source, and if so, is that a better approach? Or should it be handled in the aspx / vb code, and if so, how can it be configured to work if the CityID is null (in which case it should just display an empty string)?

Thanks very much for any direction you can provide!
Question by:JMS1965
LVL 14

Accepted Solution

robasta earned 2000 total points
ID: 35727348
Using the Entity Datasource is the easiest. Here's how:

1. In your datasource, add the Attribute  [ Include="City" ] - where 'City' is the table that contains the City Name
2. In the gridview, you can now bind like this: [ DataField="City.Name" ]

Im sure this also handles nulls.

Author Closing Comment

ID: 35747209
Just what I was looking for ... thanks very much!

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question