简体   繁体   English

将组合框选定的值传递给WCF,以从Silverlight 5 Pivot Viewer客户端获取数据

[英]Passing combobox selected value to WCF to get data from a silverlight 5 Pivot Viewer Client

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. 我有一个包含所有品牌列表的组合框,我需要将此组合框的选定值传递给名为service1.svc的wcf服务。 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 { 命名空间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 我的mainpage.xaml.cs如下

namespace SilverlightApplication2 { 命名空间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()); 在var proxy = new Service1Client(selectedItem.ToString());

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. 右键单击您的服务参考,然后选择您的IDE语言中最接近“更新”的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM