I have 2 tables in the database carrier and vendor. vendor has a foreign key of Carrier_Id. When the user want add a vendor, the user will select a carrier from a combobox where the valve from carrier table.
The problem I facing now is that,I don’t know how to get the valve from combobox so that I can insert it into the database.
I use this code to get valve from carrier table to show in the combobox.
MyinvoiceDataDataContext contect = new MyinvoiceDataDataContext();
var st = from s in contect.Carriers
select new { s.CarrierID, s.CarrierName};
comVendorCarrier.ItemsSource = st;
comVendorCarrier.DisplayMemberPath = "CarrierName";
comVendorCarrier.SelectedValuePath = "CarrierID";
To get the value of this combobox (i.e. the selected carrier), all you need is just use the two properties
SelectedValueto get theCarrierIdorSelectedTextto get the value (i.e.CarrierName).For example, you could get the id of the selected carrier by using:
Or if you need the
CarrierNameyou should use:Edit: Assuming that your tables have the following structre:
Carrier:
CarrierId.CarrierName.Vendor:
VendorId.CarrierId: a foreign key references Carriers(CarrierId).VendorName.These two tables should be mapped in your .dbml file to two entities
VendorandCarrier, in which theVendorclass has a property of typeCarrierthat represents the foreign keyCarrierID.Then in order to view
CarrierNamein the vendors list, you can do this: