Solved

MVC - combining two field in a drop down from joint table

Posted on 2014-11-14
1
137 Views
Last Modified: 2015-03-06
I have a table that has 2 foreign key. I would like to create in drop down that contain the value of these two foreign key. How
can I do that?


Here is my table definition:

Master Table
Id INT Prinary key
PhoneNumber VARCHAR(3)
IDLocation Foreign key to location table
IDDepartment Foreign Key to Department table

Master Table with data exammpe:
1|383-383-33838|1|1


Location Table
ID Primary key but this is related to the IDLocation Foreign key in Master table
Location Varchar

Location Table with example.
1|Chicago


Department Table
ID Primary key but this is relate to the ID Department foreng Key in Master table
Department varchar
1|Clothes

So in my drop down I would like  something like this.

Please choose
Chicago - Clothes
or "Value"  - "value"
0
Comment
Question by:yguyon28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40444095
Set the following properties as shown below:

cmbBox.DataTextField = "Diaplay"           // for "Chicago - Clothes"
cmbBox.DataValueField = "BothFKs"       // for "1|1"

You could concatenate them in your Select statement:
Select 
   m.IDLocation + '|' + m.IDDepartment As BothFKs
   , l.Location + '-' + d.Department As Diaplay
   From [Master Table] m
   Inner Join [Location Table] l 
   on m.IDLocation = l.IDLocation
   Inner Join [Department Table] d 
   on m.IDDepartment = d.IDDepartment 

Open in new window


This will produce:

1|1 and  Chicago - Clothes
^-- value           ^--display

Most likely, you need to use CAST() like, apply it if it errors:

Cast(m.IDLocation as varchar(5)) + '|' + Cast(m.IDDepartment as varchar(5))  As BothFKs

cmbBox.SelectedItem.Text -->  "Chicago - Clothes"
cmbBox.SelectedItem.Value --> "1|1"
cmbBox.SelectedIndex --> 0 ,1, 2, ...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

705 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