Rohit Acharya
Rohit Acharya

Reputation: 47

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; }


}

Upvotes: 1

Views: 2787

Answers (3)

jithin
jithin

Reputation: 101

Try This Code to get Value,Text of an Combobox SelectedItem Programatically

ComboboxItem cmb = new ComboboxItem();

cmb = (ComboboxItem)cmb_designation.SelectedItem;

staffreg.Designation=int.Parse(cmb.Value.ToString());

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222582

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());

Upvotes: 0

Yuliam Chandra
Yuliam Chandra

Reputation: 14640

You can use Text property.

string selectedText = comobo1.Text;

From the documentation.

Gets or sets the text of the currently selected item.

Upvotes: 0

Related Questions