Binding Combobox from WCF

5 posts, 0 answers
  1. Vuyiswa Maseko
    Vuyiswa Maseko avatar
    187 posts
    Member since:
    Jan 2010

    Posted 11 Feb 2011 Link to this post

    Good Day Guys , i am using RadSilverlight controls and the implementation should be the same with the normal Combobox. But now the List that i get is not what i want or the data that i need. i have a WCF DataLayer Service that has a function that is defined like this 

    public IList<AccountsModel> GetAccounts()
            {
                con = new SqlConnection(strConccounting);
     
                cmdselect = new SqlCommand("usp_Select_Account");
     
                cmdselect.CommandType = CommandType.StoredProcedure;
     
                da = new SqlDataAdapter();
     
                cmdselect.Connection = con;
     
                da.SelectCommand = cmdselect;
     
                DataTable dt = new DataTable();
     
                List<AccountsModel> m_Accounts = new List<AccountsModel>();
                try
                {
                    con.Open();
     
                    da.Fill(dt);
     
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            AccountsModel m = new AccountsModel();
                            m.iAccountID = Convert.ToInt32(dt.RowsIdea[0]);
                            m.sName = dt.RowsIdea[1].ToString();
                            m_Accounts.Add(m);
                        }
                    }
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    con.Close();
                }
                return m_Accounts;
     
            }

    and now in my Presentation Layer, my Silverlight App , i want to Bind it to the Combobox. Now normally in asp.net . you would have e.g

    ID , that will be Primary key for the record and the "Description" and that will be the Text that will be displayed in the the Dropdownlist. So my setup is the same and i have amodel that is sitting on the Data layer that i forgot to share with you

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel;
    using System.ServiceModel;
    using System.Runtime.Serialization;
     
    namespace DAL
    {
        public class AccountsModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
     
            private int _iAccountID;
            private string _sName;
     
            /// When the Property is changed from the PL this gets Fired
            /// </summary>
            /// <param name="property"></param>
            private void OnPropertyChanged(String property)
            {
     
                // DAL objConverter = new DAL();
     
                List<AccountsModel> Model = new List<AccountsModel>();
     
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(property));
                    //objConverter.Updatedata();               
                }
            }
     
     
            public int iAccountID
            {
     
                get
                {
                    return _iAccountID;
                }
                set
                {
                    _iAccountID = value;
     
                    OnPropertyChanged("iAccountID");
     
                }
            }
     
     
            public string sName
            {
     
                get
                {
                    return _sName;
                }
                set
                {
                    _sName = value;
     
                    OnPropertyChanged("sName");
     
                }
            }
        }
    }

    ok now lets Continue. Now on the Silverlight side , i had a Telerik Combobox that is defined like this in the xaml

    <telerik:RadComboBox HorizontalAlignment="Left"  Text="{Binding sName}" Margin="222,80,0,0" Name="ddlaccount" VerticalAlignment="Top" Width="186" Height="26">
            </telerik:RadComboBox>

    and the server side i am creating the BLL Service object and accessing the funtions like this

    public Posting()
            {
                InitializeComponent();
     
               Loaded += new RoutedEventHandler(Posting_Loaded);
            }
     
            void Posting_Loaded(object sender, RoutedEventArgs e)
            {
     
                BLL.BLLServiceClient objBLL = new BLL.BLLServiceClient();
     
                
                objBLL.GetAccountsCompleted += new EventHandler<BLL.GetAccountsCompletedEventArgs>(objBLL_GetAccountsCompleted);
                objBLL.GetAccountsAsync();
            }
     
            void objBLL_GetAccountsCompleted(object sender, BLL.GetAccountsCompletedEventArgs e)
            {
                if (e.Error == null)
                {
               
                    ddlaccount.ItemsSource = e.Result;
                 
                }
            }

    and at the same time , i am binding the Combobox. but the Results i get are not the Values but this

    Thanks

  2. Dan Andrews
    Dan Andrews avatar
    28 posts
    Member since:
    Jul 2009

    Posted 14 Feb 2011 Link to this post

    <telerik:RadComboBox Name="ddlaccount"
                   t:TextSearch.TextPath="sName"
                   SelectedValuePath="sName"
                   DisplayMemberPath="sName"
                   SelectedValue="{Binding Path=myName, Mode=TwoWay, NotifyOnValidationError=True}"
                   IsEditable="True" />

    You'll then need to set the datacontext of this to an object that has myName (of type Account).  Generally you don't bind directly to the WCF object, but instead make a Model which exposes and/or extends properties of the WCF object.  Your ViewModel will have a collection of your Model.  It may appear to be duplicate work, but you need the model to do validation which a WCF object does not have.

    public class ModelBaseClass : INotifyPropertyChanged, INotifyDataErrorInfo
    {
              ....
    }
  3. DevCraft banner
  4. Dan Andrews
    Dan Andrews avatar
    28 posts
    Member since:
    Jul 2009

    Posted 14 Feb 2011 Link to this post

    Your View, Model, ViewModel all belong in the Silverlight application.  What you're calling your Model (on the WCF side) is basically a Data transfer object (DTO), formerly known as value objects or VO.
  5. JULIO
    JULIO avatar
    3 posts
    Member since:
    Dec 2010

    Posted 17 Jun 2011 Link to this post

    If a fill with static info., I can see the items, , but, databinding makes this:
  6. JULIO
    JULIO avatar
    3 posts
    Member since:
    Dec 2010

    Posted 22 Jun 2011 Link to this post

    Im changin the style, but nothing, still can't see anything, but if I click, all this items are overthere.

    ¿¿¿¿ Is this a BUG ????
Back to Top
DevCraft banner