1

I have created a method in C#.net 4.0 which returns a dynamic type:

public dynamic  GetEntertainmentDetails(int entertainmentId)
    {
        dynamic result = from PE in entities.ProductEntertainments
                     join PM in entities.ProductModels on PE.ProductModelID equals PM.ProductModelID
                     join PMA in entities.ProductMasters on PM.ProductUID equals PMA.ProductUID
                     join PMF in entities.ProductManufactorers on PMA.ManufactorerID equals PMF.ManufactorerID
                     where PE.EntertainmentID == entertainmentId
                     select new { PE.EntertainmentID, PMF.ManufactorerID, PMA.ProductUID, PM.ProductModelID, PE.CDPlayer, PE.CDChanger, PE.DVDPlayer, PE.Radio, PE.AudioSystemRemoteControl, PE.SpeakersFront, PE.SpeakersRear };
        return result;
    }

I want to use the results in a method which goes like this:

private void DisplayRecord()
    {
        dynamic item = dbContext.GetEntertainmentDetails(entertainmentId);
        this.cmbManufacturer.SelectedValue = item.ManufactorerID;
        this.cmbProducts.SelectedValue = item.ProductUID;
        this.cmbVariant.SelectedValue = item.ProductModelID;
        if (item.CDPlayer == true)
            this.cdPlayerYes.IsChecked = true;
        else
            this.cdPlayerNo.IsChecked = true;

    }

But value returned is not being set in cmbManufactorer or any other control. I tested that dyanmic item has values but not sure why are they not being set.

Is it the right way to handle it or am I doing wrong here ?

Update: I am using this code in a WPF application. SelectedValue property can be get/set there for dropdowns.

Rakesh Singh
  • 143
  • 2
  • 11

3 Answers3

1

You cannot set the value in dropdown like that Since SelectedValue is a read only property
Try like This :-

// Assuming item.ManufactorerID is returning the index of selected item:- 

  cmbManufacturer.SelectedIndex  = item.ManufactorerID;

Or else if you have value of Dropdown then :-

 cmbManufacturer.Items.FindByText("PassedValue").Selected = true;
Pranav
  • 8,563
  • 4
  • 26
  • 42
  • @Pranav.. Selected value can be set there – Rakesh Singh Jun 20 '13 at 05:18
  • if the item.ManufactorerID is returning something which is not in Dropdown then value will not be selected.(or default value 0). for testing just check with cmbManufacturer.SelectedIndex=2; – Pranav Jun 20 '13 at 05:21
  • Yes I am 100% sure that returning value is in dropdown list. Here is another example: VendData.GetProductColorsByProductColorID_Result colorObject = dbContext.GetProductColorsByID(productColorId); this.cmbManufacturer.SelectedValue = colorObject.ManufactorerID; Here value for cmbManufacturer is being set and displayed. – Rakesh Singh Jun 20 '13 at 05:24
  • I mean definitely it is working for strong types but not for dynamic – Rakesh Singh Jun 20 '13 at 05:25
  • I am not sure because i have not tried with dynamic...but i guess unless `item.ManufactorerID` is returning an INT ..dropdownList should not have any problem ... However Try to cast item.ManufactorerID as Int and check for my sake.. – Pranav Jun 20 '13 at 05:30
  • It is able not item.ManufactorerID to int, but ResultView of the item is showing that there are values in it. – Rakesh Singh Jun 20 '13 at 05:51
  • on checking this link difference-between-selecteditem-selectedvalue-and-selectedvaluepath [link](http://stackoverflow.com/questions/4902039/difference-between-selecteditem-selectedvalue-and-selectedvaluepath). It seems you might need to use SelectedValue with SelectedValuePath , just for trial you could see what happens when you use SelectedItem – Rameez Ahmed Sayad Jun 20 '13 at 05:54
  • Let me make myself clear here. It's all not about SelectedValue or SelectedItem. Definitely it works when we have a valid value. It's about how a dynamic object is wrapping the returning results in it. When we set it as 'dynamic s = "1"; cmbABCD.selectedValue=s;' it definitely works, but here scenario is bit different. I am getting a complex result set and wrapping it in a dynamic object and not able to read the returned values. – Rakesh Singh Jun 20 '13 at 06:05
0

I am not sure about your item.ManufactorerID what kind of value is coming in that.

However dynamic should work if the types are compatable.

For ex:

    dynamic s = "Item 3";
    drop1.SelectedValue  = s;

Or

    dynamic s = 2;
    drop1.SelectedIndex   = s;

both works.

mmssaann
  • 1,507
  • 6
  • 27
  • 55
0

Alright, I got the reason of why was it not working. It's because the GetEntertainmentDetails method was returning a complex result set with an array of items, thus not able to find out the ManufacturerID or any other item. I changed the code as:

 public dynamic  GetEntertainmentDetails(int entertainmentId)
    {
        dynamic result = (from PE in entities.ProductEntertainments
                     join PM in entities.ProductModels on PE.ProductModelID equals PM.ProductModelID
                     join PMA in entities.ProductMasters on PM.ProductUID equals PMA.ProductUID
                     join PMF in entities.ProductManufactorers on PMA.ManufactorerID equals PMF.ManufactorerID
                     where PE.EntertainmentID == entertainmentId
                     select new 
                     { 
                         PE.EntertainmentID, 
                         PMF.ManufactorerID, 
                         PMA.ProductUID, 
                         PM.ProductModelID, 
                         PE.CDPlayer, 
                         PE.CDChanger, 
                         PE.DVDPlayer, 
                         PE.Radio, 
                         PE.AudioSystemRemoteControl, 
                         PE.SpeakersFront, 
                         PE.SpeakersRear 
                     }).SingleOrDefault();
        return result;
    }

And now I am able to bind every control.

Rakesh Singh
  • 143
  • 2
  • 11