0

I have a combobox which has a list of all brands, I need to pass this selected value of the combo box to the wcf service named service1.svc. I am not able to understand how I can pass the selected item as parameter to the service. Could you guys please help me out. Thanks

My service reference is as follows

namespace SilverlightApplication2.Web {

public class Service1
{

    [OperationContract]
    public ObservableCollection<Employee> GetAllEmployees(string brandID)  
    {
        var emps = new ObservableCollection<Employee>();
        string connect = ConfigurationManager.ConnectionStrings["yoyo"].ToString();

        using(var con = new OdbcConnection(connect))
        {


            string query = "Select new,brand,imagelink FROM pivottable WHERE brand='"+brandID+"'";


            var cmd = new OdbcCommand(query, con);


            con.Open();



            using (var dr = cmd.ExecuteReader())
            {
                while(dr.Read())
                {
                    var emp = new Employee();
                    emp.EmployeeID = dr.GetInt32(0);
                    emp.FirstName = dr.GetString(1);
                    emp.ImageURI = new Uri(dr.GetString(2));
                    emps.Add(emp);

                }


            }



        }

        return emps; 
    }


}

}

My mainpage.xaml.cs is as follows

namespace SilverlightApplication2 {

   public partial class MainPage : UserControl
{

   public MainPage()
    {
        InitializeComponent();
        object selectedItem = Combobox.SelectedItem;

        var proxy = new Service1Client(selectedItem.ToString());
      //  var proxy = new Service1Client();
        proxy.GetAllEmployeesCompleted += proxy_GetAllEmployeesCompleted;
       proxy.GetAllEmployeesAsync();

       var proxytwo = new Service1Client();
       proxytwo.GetAllBrandsCompleted += proxytwo_GetAllBrandsCompleted;
       proxytwo.GetAllBrandsAsync();
    }

    void proxytwo_GetAllBrandsCompleted(object sender, GetAllBrandsCompletedEventArgs f)
    {
        Combobox.ItemsSource = f.Result;
    }


    void proxy_GetAllEmployeesCompleted(object sender,GetAllEmployeesCompletedEventArgs e)
    {


        Pivot.ItemsSource = e.Result;


    }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        object selectedItem = Combobox.SelectedItem;
        MessageBox.Show("Selected item is" +selectedItem.ToString());

    }



}

}

I get Object reference not set to an instance of an object. at var proxy = new Service1Client(selectedItem.ToString());

Rohit Acharya
  • 47
  • 1
  • 7
  • What did you try so far? Do you have a combo box? Can you get the selected item and display it somewhere? Can you send it to a service? If not, why not, what questions remain after you read a basic service tutorial? – nvoigt Aug 18 '14 at 11:16
  • Hi nvoigt , thankyou for your reply, I have a Combobox.ItemSource = e.result which gives me distinct brands from the table through a service, when the user selects a brand I want to pass this to string brandID above. I currently have a Async call as var proxy = new Service1Client(); proxy.GetAllEmployeesCompleted += proxy_GetAllEmployeesCompleted; proxy.GetAllEmployeesAsync(); – Rohit Acharya Aug 18 '14 at 14:57
  • Why does your service call on the proxy not take parameters? Is the proxy older than your parameter in the function? Maybe you need to update your proxy? – nvoigt Aug 18 '14 at 15:06
  • I have object selectedItem = Combobox.SelectedItem; How do I pass this selected item in proxy? – Rohit Acharya Aug 18 '14 at 15:16
  • You should be able to pass a string to your proxy's method, for example `proxy.GetAllEmployeesAsync(selectedItem.ToString());`. If not, maybe your proxy is outdated. – nvoigt Aug 18 '14 at 15:21
  • Object reference not set to an instance of an object. (This is actualy NullReferenceException) this is the error it throws when I passed through proxy. I also hardcoded as string selectedItem = "EZRA" but then it says could not find endpoint elements with name EZRA and contract 'ServiceReference1.Service1' – Rohit Acharya Aug 18 '14 at 15:39
  • Please post the code we are talking about in your question and mark any errors you get including the exact error message. – nvoigt Aug 18 '14 at 15:47
  • Hey nvoigt my apologies I have updated the question with codes, Is it clear for you now? – Rohit Acharya Aug 18 '14 at 15:53

1 Answers1

0
var proxy = new Service1Client();

proxy.GetAllEmployeesCompleted += proxy_GetAllEmployeesCompleted;
proxy.GetAllEmployeesAsync(selectedItem.ToString());

If you get compiler errors on the last line, you need to update your service proxy. Right-click your service reference and select what's closest to "Update" in your IDEs language.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Thanks a lot nvoigt, its working. It throws up an error of nullreferenceexception was unhandled by code because the selected item would be set to null before the user clicks – Rohit Acharya Aug 19 '14 at 02:18
  • 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()); } The Message box shows Service reference class instead of the string selected by the user, How do i get the string value of what the user selects? – Rohit Acharya Aug 19 '14 at 03:22