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

Posted on 2011-05-09
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

    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

    Just what I was looking for ... thanks very much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now