Question

I have problem, i don't know how to update ObservableCollection when i call the same URL in windows phone.

The problem is:- I call the URL for the first time then i add this to my listbox then after one minute i call the same URL and the result have new data (New data Added or Old Data removed), i don't how to search the old data to check if all new data is exist and if not how add the new row received with out duplication.

Please Advise me :(

this how i call the URL and how i add the data:-

 private void GetOpentPos (Object sender, EventArgs e)
    {


        var request = HttpWebRequest.Create(new Uri("http://74.54.46.178/vertexweb10/webservice.svc/GetOpenPositions?AccountId=1122336675")) as HttpWebRequest;

        request.Method = "GET";

        if (request.Headers == null)
        {
            request.Headers = new WebHeaderCollection();
        }
        request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();
        request.CookieContainer = cookieJar2;
        request.BeginGetResponse(ar =>
        {
            HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState;
            using (var response = (HttpWebResponse)req2.EndGetResponse(ar))
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        var outerRoot4 = JsonConvert.DeserializeObject<OuterRootObject4>(reader.ReadToEnd());
                        JArray jsonArray = JArray.Parse(outerRoot4.d);
                        JToken jsonArray_Item = jsonArray.First;

                        DispatchInvoke(() =>
                        {


                            while (jsonArray_Item != null)
                            {


                                string SymbolNameTra = jsonArray_Item.Value<string>("SymbolName");
                                string TypeTra = jsonArray_Item.Value<string>("BuySell");
                                double AmountTra = jsonArray_Item.Value<double>("Amount");
                                double ProfitLossTra = jsonArray_Item.Value<double>("ProfitLoss");
                                int PosID = jsonArray_Item.Value<int>("ID");



                                DataReceivedCollectionTr.Add(new DataTr() { SymbolNameTr = SymbolNameTra, TypeTr = TypeTra, AmountTr = AmountTra, ProfitLossTr = ProfitLossTra,PosID = PosID });

                                jsonArray_Item = jsonArray_Item.Next;
                            }

                        }
);


                    }
                }

            }

        }, request);



    }
Was it helpful?

Solution

Check by some unique field and if list does not containt such item add it

if (DataReceivedCollectionTr.FirstOrDefault(i => i.SymbolNameTr == SymbolNameTra) == null)
{
    DataReceivedCollectionTr.Add(new DataTr() { SymbolNameTr = SymbolNameTra, TypeTr = TypeTra, AmountTr = AmountTra, ProfitLossTr = ProfitLossTra,PosID = PosID });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top