4 Replies - 14720 Views - Last Post: 13 August 2011 - 06:18 AM

#1 monashahid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-December 10

combo box not returning string value?

Posted 11 August 2011 - 01:27 AM

hi everyone!
need urgent help....plz

i m working with silverlight 4 and wcf ria services...
i have used a combobox which is binded with database value say firstname.

myapp.Web.domainservice context = new myapp.Web.domainservice();
            LoadOperation<employee_profile> userQueryOperation = context.Load(context.GetEmployee_profileQuery());
            comboBox1.ItemsSource = userQueryOperation.Entities;
            comboBox1.DisplayMemberPath = "fname";



that is fine but when i prints the value of selected item it gives the int no rather than string value.

string id2 = comboBox1.SelectedItem.ToString();



returns table name with selected value index like this;

employee_profile : 2

Is This A Good Question/Topic? 0
  • +

Replies To: combo box not returning string value?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,061
  • Joined: 02-June 10

Re: combo box not returning string value?

Posted 12 August 2011 - 02:08 PM

What is the object type populating the ComboBox.Items collection?

What I mean is that .Items is really an object collection. It can contain strings, ints, widgets, or even Forms. You can put any kind of object into it. Maybe the type you are putting in has the .ToString overriden to supply a numeric value.


Put a breakpoint on this line:

string id2 = comboBox1.SelectedItem.ToString();

When it stops there, hover over the .SelectedItem and you will get a tooltip-like menu allowing you to look at the actual item.

Attached Image

Expand out the properties so you can see the important properties. If that doesn't give you a big clue as to why it isn't working, take a screenshot showing these values and post it, so we can all see it and maybe someone can help determine why.

I'm going to guess that the issue is here:
comboBox1.ItemsSource = userQueryOperation.Entities;
comboBox1.DisplayMemberPath = "fname";



The display member is different than the actual ItemsSource. But then you are trying to get a string out of the SelectedItem. Which probably doesn't contain a pretty looking firstname string or you wouldn't have to have done this displaymember technique to begin with. So the Item as supplied by ItemsSource only knows to return '2' as a string, probably from its .ToString() method being called automatically to provide a string as requested.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: combo box not returning string value?

Posted 12 August 2011 - 03:05 PM

Moved to Silverlight forum
Was This Post Helpful? 0
  • +
  • -

#4 monashahid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-December 10

Re: combo box not returning string value?

Posted 12 August 2011 - 05:06 PM

thanks for the response,

i did as you say but still cant understand what is wrong with it so posting the snap shot

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: combo box not returning string value?

Posted 13 August 2011 - 06:18 AM

Can you post the code for the employee_profile class? Are you overriding the ToString method for the class?

When you set the ItemsSource of a control to a collection of objects, the SelectedItem property will be the actual object, not the DisplayMember. So calling ToString is going to call the ToString method for the object.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1