Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a detailsview that is databound to a Customer data entity object. The customer is connected to a Company object via a foreign key. I need to populate a dropdownlist with a list of Companies, and have the selected item be the one that is connected with that customer. I'm not too sure how to go about this.

1 solution

I will explain you the logic via data-structure. Assumption: Data model (presenting as if in DB) would be something like
CustomerID CustomerName CompanyID

CompanyID CompanyName

Now, to have data for a given customer, a SQL query would be like:
   CompanyName, CompanyID 
   CustomerTable ON CompanyTable.CompanyID = CustomerTable.CompanyID 
   CustomerTable.CustomerID = @customerid

Thus, you get the client associated with the given Customer which you can use and set the company dropdown with.

Try out based on above logic.
Share this answer
Jack R. Schaible 25-Feb-13 14:30pm    
It's not really a database issue, though. Assume that I can't change the database, I still just need to pull a list of records from the company table, bind that to the dropdownlist in the detailsview, and select one of them based off of data in the detailsview.
Sandeep Mewara 25-Feb-13 14:46pm    
I am not asking you to change. I shared the expected database structure for the kind of relation you shared. If the above structure is not so then you should share how they are connected.

Overall, you have to find a way to get CompanyID using a CustomerID. Once you have it, use that and set in company dropdown.
Jack R. Schaible 25-Feb-13 15:01pm    
No no, you got the structure right...and as for getting the Company from the Customer, I have database queries that will do that (GetCompanyByCustomerID). My problem is with displaying that information in a detailsview.
Sandeep Mewara 25-Feb-13 23:55pm    
So for detailsview the datasource should have the info about the companyID for a Customer. I assume you are showing all the customers in the detail view with edit functionality of Company for them.

If so, the dropdown value expects a valid CompanyID, define the dropdown with all companies and make sure the dataset/datasource has valid companyid for the customer such that when you assign/set dropdown to that value all works good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900