Question

I have been assigned to create a Contacts page using ASP.NET. I have a GridView that displays all Contacts with a DropDownList ItemTemplate in the 'Phone Number(s)' column. A Customer can have multiple Phone Numbers, so I have two tables in my DB: Customer and Phone (with the FK Customer). I am currently using the SqlDataSource control to populate my GridView, and another SqlDataSource for the Phone numbers. As of now, the DDL will populate ALL phone numbers in the Phone table, but I only want it to display the numbers for the correct Contact. Is there an easy way to do this?

Was it helpful?

Solution

I suppose you could do it two ways.

1) You can write a single SQL query which will return something like:

CONTACT NAME     |     PHONE NUMBER
-----------------------------------
JOHN             |     555-555-5551
JOHN             |     555-555-5552
JANE             |     555-555-5553

And then your business logic can parse the results into the correct DataSet, DataTable, Enumerable, or whatever you're binding to your GridView.

2) You can write one SQL query to get all contacts, bind that to your GridView, loop through all the contacts, individually query for that contact's phone numbers, and then bind those results to the drop down contained within the record for that contact.

I prefer the first, but either should be OK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top