Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am consuming web service result into my list box is on windows phone app
my first two pages works fine i.e. there i bind result in textbox
but I unable to bind with listbox below is my code
C#
namespace Medical_Representative_Project2
{
    public partial class Antocids : PhoneApplicationPage
    {
        ObservableCollection<Class1> p = new ObservableCollection<Class1>();

        //string Data;
        public Antocids()
        {
            InitializeComponent();
            listBox1.DataContext = p;
            ProductsClient client = new ProductsClient();
            client.getProdDetailsCompleted += new EventHandler<getProdDetailsCompletedEventArgs>(client_getProdDetailsCompleted);
            client.getProdDetailsAsync();
        }
        private void client_getProdDetailsCompleted(object sender, getProdDetailsCompletedEventArgs e)
        {

           // assuming you want to clear the data each time you get a new result
            foreach (var result in e.Result)
                // p.Add(result); 
                p.Add(result);//here i getting the error


        }




    }
}

And this is my Class
C#
namespace Medical_Representative_Project2
{
    public class Class1
    {
        public string name { get; set; }
    }
}



my requirement is display web-service result in listbox
Posted
Updated 20-Mar-12 20:09pm
v2

Is there a particular reason you're using ObservableCollection? Instead you could do something like this:

C#
namespace Medical_Representative_Project2
{
    public partial class Antocids : PhoneApplicationPage
    {
        public Antocids()
        {
            InitializeComponent();
            ProductsClient client = new ProductsClient();
            client.getProdDetailsCompleted += new EventHandler<getProdDetailsCompletedEventArgs>(client_getProdDetailsCompleted);
            client.getProdDetailsAsync();
        }

        private void client_getProdDetailsCompleted(object sender, getProdDetailsCompletedEventArgs e)
        {
            // Always check to see if there's an error before examining e.Result!
            if (e.Error == null)
            {
                listBox1.ItemsSource = e.Result;
            }
            else
            {
                MessageBox.Show(e.Error.Message.ToString());
            }
        }
    }
}


The above code works (at least in WPF, but should also be fine in WP7) for me. If the above does not work for you then we'll need more information in order to help. What is the error you're getting?

I agree with Oleksandr that you are best to move the web service code to the OnLoaded, although I don't think that alone would make the difference in it working or not.

Edited as I made a slip in the error check. Changed != to ==.
 
Share this answer
 
v2
Well , as far as i know , it would be better to move initialization and requesting data in event handler on PageLoaded event.
So as a result:
C#
namespace Medical_Representative_Project2
{
    public partial class Antocids : PhoneApplicationPage
    {
        ObservableCollection<Class1> p = new ObservableCollection<Class1>();

        //string Data;
        public Antocids()
        {
            InitializeComponent();
            this.Loaded+=OnLoaded;
        }
   
        void OnLoaded(object send,RoutedEventArgs e)
       {
        listBox1.DataContext = p;
        ProductsClient client = new ProductsClient();
        client.getProdDetailsCompleted += new EventHandler<getProdDetailsCompletedEventArgs>(client_getProdDetailsCompleted);
            client.getProdDetailsAsync();
       }

        private void client_getProdDetailsCompleted(object sender, getProdDetailsCompletedEventArgs e)
        {
           if(e.Error!=null)
              return;

           // assuming you want to clear the data each time you get a new result
            foreach (var result in e.Result)
                // p.Add(result);
                p.Add(result);//here i getting the error
        }

    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900