Silverlight应用程序的WCF服务中的转换问题 [英] Convertion problem in with WCF service for silverlight app

查看:99
本文介绍了Silverlight应用程序的WCF服务中的转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个Silverlight应用程序,其中使用了WCF服务来显示数据库中的记录.
数据从WCF传递到List中的Silvelight.在类变量中有存储.
但是当我将结果与silverlight的datagrid绑定时出现错误:-

错误1无法将类型"AdWorldSilverLightApp.ServiceReference1.fetchUserRecordResponseFetchUserRecordResult"隐式转换为"System.Collections.IEnumerable".存在显式转换(是否缺少强制转换?)F:\ balwant \ TestApp \ AdWorldSilverLightApp \ AdWorldSilverLightApp \ ShowUser.xaml.cs 43 36 AdWorldSilverLightApp


发生.
silverlight xaml.cs代码如下:-

Hi,
I have a silverlight app in which i consume WCF service for showing record from database.
data is passed from WCF to Silvelight in List<> having store in class variable.
but when i am binding the result with datagrid of silverlight then an error:-

Error 1 Cannot implicitly convert type ''AdWorldSilverLightApp.ServiceReference1.fetchUserRecordResponseFetchUserRecordResult'' to ''System.Collections.IEnumerable''. An explicit conversion exists (are you missing a cast?) F:\balwant\TestApp\AdWorldSilverLightApp\AdWorldSilverLightApp\ShowUser.xaml.cs 43 36 AdWorldSilverLightApp


occurs.
silverlight xaml.cs code is as:-

public ShowUser()
       {
           InitializeComponent();
           ServiceReference1.MyServiceClient newclient = new ServiceReference1.MyServiceClient();
           newclient.fetchUserRecordCompleted += new EventHandler<ServiceReference1.fetchUserRecordCompletedEventArgs>(newclient_fetchUserRecordCompleted);
           newclient.fetchUserRecordAsync();
       }
       void newclient_fetchUserRecordCompleted(object sender, ServiceReference1.fetchUserRecordCompletedEventArgs e)
       {

           ShowGrid.ItemsSource = e.Result;
       }




WCF服务代码为:-




and WCF service code is as:-

public class UserMaster
       {
           public string UserName { get; set; }
           public string Password { get; set; }
           public string FirstName { get; set; }
           public string LastName { get; set; }
           public string Email { get; set; }
           public string Address { get; set; }
           public string MatrialStatus { get; set; }
           public string Moblie { get; set; }
       }
 public List<UserMaster> fetchUserRecord()
        {
            var Record = new List<UserMaster>();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            cmd.CommandText = "select * from UserMaster";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i <= dt.Rows.Count; i++)
                {
                    var user = new UserMaster
                    {
                        UserName = dt.Rows[i]["UserName"].ToString(),
                        Password=dt.Rows[i]["Password"].ToString(),
                        FirstName = dt.Rows[i]["FirstName"].ToString(),
                        LastName = dt.Rows[i]["LastName"].ToString(),
                        Email = dt.Rows[i]["Email"].ToString(),
                        Address = dt.Rows[i]["Address"].ToString(),
                        MatrialStatus = dt.Rows[i]["MatrialStatus"].ToString(),
                        Moblie = dt.Rows[i]["Moblie"].ToString()
                    };
                    Record.Add(user);


                }
                
            }
          
            return Record;
            // return dt;
        }


请帮帮我.
感谢


Please help me.
thanks

推荐答案

您不能仅将itemsource设置为结果-ShowGrid.ItemsSource = e.Result;.

您需要删除将结果中返回的值放入集合(一个IEnumerable 对象)中,并将其分配给itemssource.
检查您在e.Result中得到了什么-它是IEnumerable 类型吗?
You cannot just set the itemsource to the result - ShowGrid.ItemsSource = e.Result;.

You need to remove put the values returned in the result into a collection (an IEnumerable object) and the assign to the itemssource.
Check what you are getting in e.Result - is it an IEnumerable type?


这篇关于Silverlight应用程序的WCF服务中的转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆