Search code examples
c#wcfcomboboxpivotviewer

How to get Combobox.SelectedItem Return string value of what is selected instead of Service Reference Class?


I currently have the following code for a button. The message box shows SilverlightApplication2.ServiceReference2.Employee instead of the text string selected by the user. The combobox items are being populated by a WCF service. As a result I am unable to pass it to the Async call. How do I get the string of what user selected?

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
    object selectedItem = comobo1.SelectedItem.ToString();
    MessageBox.Show(selectedItem.ToString());
    var proxy = new Service1Client();
    proxy.GetAllEmployeesCompleted += proxy_GetAllEmployeesCompleted;
    proxy.GetAllEmployeesAsync(selectedItem.ToString());
}

My Service Reference looks like

public class dropdown {

    [OperationContract]
    public ObservableCollection<Employee> GetAllBrands()
    {   

        var empstwo = new ObservableCollection<Employee>();
        string connect = ConfigurationManager.ConnectionStrings["yoyo"].ToString();
        using (var con = new OdbcConnection(connect))

        {  
            //now you can try
            //wait. To accept a param from main page, u need to create a method to accept that param first.
            //I think you should put this in service1.svc.cs

            string query = "Select distinct(brand) FROM pivottable";
            var cmd = new OdbcCommand(query, con);
            con.Open();
            using (var dr = cmd.ExecuteReader())
            {


                while (dr.Read())
                {
                    var emp = new Employee();

                    emp.ComboData = dr.GetStringOrNull(0);

                    empstwo.Add(emp);

                }


            }



        }

        return empstwo;
    }

}

and this is the employee class. In this string ComboData holds the list of brands which populates my dropdown list

public class Employee
{
    public int EmployeeID { get; set; }
    public string FirstName { get; set; }
    public Uri ImageURI { get; set; }
    public string ComboData { get; set; }


}

Solution

  • You need to cast the selecteditem to the type of object you are binding.Something like this,

    var selected = (Employee)comobo1.SelectedItem;
    MessageBox.Show(selected.ComboData.ToString());