Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0 DataBinding , +
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.
Posted 25-Feb-13 7:35am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

Now, to have data for a given customer, a SQL query would be like:
SELECT 
   CompanyName, CompanyID 
FROM 
   CompanyTable 
INNER JOIN 
   CustomerTable ON CompanyTable.CompanyID = CustomerTable.CompanyID 
WHERE   
   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.
  Permalink  
Comments
Jack R. Schaible at 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 at 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 at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 368
1 Richard MacCutchan 240
2 OriginalGriff 230
3 Jared Sanow 145
4 Vinay Mistry 133
0 Sergey Alexandrovich Kryukov 5,955
1 OriginalGriff 4,805
2 CPallini 2,473
3 Abhinav S 1,507
4 Richard MacCutchan 1,437


Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100