Question

i have read many posts but can not find my answer.my question is a little specific.in my silverlight project i want to get weather data like temp,status and date from yahoo weather and save it to my database countiniously by changing from its rss.so iused webclient and its DownloadStringAsync and DownloadStringCompleted for getting data.also i created a presentation model in my server in models folder(beacause i wanted to use it in my service) so in my DownloadStringCompleted event handler i have done something like this:

  void xmlclient_DownloadStringCompleted(object sender,DownloadStringCompletedEventArgs e)
    {
        XNamespace yweather = "http://xml.weather.yahoo.com/ns/rss/1.0";
        if (e.Error == null)
        {
            XElement x = XElement.Parse(e.Result);
            weatherquery1 =
               from items in x.Descendants("item")

               select new BusinessApplication1.Web.Models.WeatherConditionModel
               {

               PubDate = items.Element(yweather +"condition").Attribute("date").Value,
               Status = items.Element(yweather + "condition").Attribute("text").Value
               };
             }

           }

this is in my viewmodel and i tested it all works.i can get data and also can see the result in datagrid or listbox. inow i want to save data in my database.i want it to be done automatically and not by a button or a command.i want it to read data always and save it to database in every 5 minutes for example.so i created my service and i create a custom insert which i can shape it myself:

 private void MapwcModel(WeatherConditionTable wctable, WeatherConditionModel wcPM)
     {
        wctable.Status = wcPM.Status;
        wctable.PubDate = wcPM.PubDate;
        wctable.WeatherConditionID = wcPM.WeatherConditionID;

     }

    [Insert]
    [Invoke]
    public void InsertWeatherConditionData(WeatherConditionModel WeatherConditionData)
    {
        WeatherConditionTable wc = WeatherConditionContext.WeatherConditionTables.CreateObject();
        MapwcModel(wc, WeatherConditionData);
        wc.Status = WeatherConditionData.Status;
        wc.PubDate = WeatherConditionData.PubDate;
        WeatherConditionContext.WeatherConditionTables.AddObject(wc);
        WeatherConditionContext.SaveChanges();


    }

and my get data:

        public IQueryable<WeatherConditionModel> GetWeatherConditionData()
           {
         return from p in this.WeatherConditionContext.WeatherConditionTables
               select new WeatherConditionModel
               {
                   WeatherConditionID = p.WeatherConditionID,
                   Status = p.Status,
                   PubDate = p.PubDate,

               };
           }

now i do not know how to force it to save data.i wrote this in my iewmodel but did not work:

    foreach (BusinessApplication1.Web.Models.WeatherConditionModel el in weatherquery1)

                {
                WeatherConditionDomainContext context = new WeatherConditionDomainContext();
                EntityQuery<BusinessApplication1.Web.Models.WeatherConditionModel> weatherLoadQuery = context.GetWeatherConditionDataQuery();
                context.Load<BusinessApplication1.Web.Models.WeatherConditionModel>(weatherLoadQuery);
                context.SubmitChanges(delegate(SubmitOperation operation)
                {
                    if (operation.HasError)
                    {
                        operation.MarkErrorAsHandled();
                    }
                }, null);
                }

i do not know how to force insert method to work.someone please tell me in where i am wrong?i know there is somewhere.show me the way. best regards

Was it helpful?

Solution 2

I found it .Many thanks to book: Silverlight 4 Unleased- chapter 13- from the great : Laurent Bugnion .

first,there is no need to use the WeatherConditionModel as a presentation model (presentation model id used when you need to save data in multiple tables ).it is just a class used as holder of my query output. second,not need to change Insert method in the service at all(because here i want to save data in just one table) so , just create your service upon your entity model and build it.after build it in this way, you can call your table in your view model( which i could not,because i changed my service methods(i changed WeatherConditionTable to WeatherConditionModel(the class!!) manually as you saw). third,in my foreach loop,i can save my data to my database. .i have a combobox and a listbox and a button,i select my city from combobox,and hit button that uses a command to my GetRss and it does the job nicely now.it shows data and saves it to database. it is my view model code( described part):

      internal void GetRssFeed()
          {
            Feed selectedFeed = (Feed)FeedModel.FeedList[FeedModel.SelectedIndex];
            FeedModel.SelectedFeedName = selectedFeed.FeedName;
            WebClient xmlclient = new WebClient();
            xmlclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlclient_DownloadStringCompleted);
        xmlclient.DownloadStringAsync(new Uri(selectedFeed.FeedUrl));

    }

    WeatherConditionDomainContext context = new WeatherConditionDomainContext();
    WeatherConditionTable wct = new WeatherConditionTable();
    void xmlclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        XNamespace yweather = "http://xml.weather.yahoo.com/ns/rss/1.0";
        if (e.Error == null)
        {

            XElement x = XElement.Parse(e.Result);
            weatherquery1 =
               from items in x.Descendants("channel")
               let item=items.Element("item")
               select new WeatherConditionModel
               {
                   Temp = Convert.ToInt32(item.Element(yweather + "condition").Attribute("temp").Value),
                   PubDate = item.Element(yweather + "condition").Attribute("date").Value,
                   Status = item.Element(yweather + "condition").Attribute("text").Value,
                   Humidity=Convert.ToInt32(items.Element(yweather + "atmosphere").Attribute("humidity").Value)
               };

            foreach (WeatherConditionModel wc in weatherquery1)
           {

                   wct.Temp = wc.Temp;
                   wct.Status = wc.Status;
                   wct.PubDate = DateTime.Now.ToShortTimeString();
                   wct.Humidity = wc.Humidity;
                   context.WeatherConditionTables.Add(wct);
                   context.SubmitChanges();


           }
        }
        else
        {
            MessageBox.Show(e.Error.Message);
        }
    }

thanks for all attention.i hope it helped someone.tell me if anyone have better ideas. please mark as answer if it helped anyone.

OTHER TIPS

I am having a hard time understanding what you are trying to do, but the pattern for the last step you show should be more like:

// Create data context
WeatherConditionDomainContext context = new WeatherConditionDomainContext();

// Load existing entities
EntityQuery<BusinessApplication1.Web.Models.WeatherConditionModel> weatherLoadQuery = context.GetWeatherConditionDataQuery();
context.Load<BusinessApplication1.Web.Models.WeatherConditionModel>(weatherLoadQuery);

// Update or insert new entries
foreach (BusinessApplication1.Web.Models.WeatherConditionModel el in weatherquery1)
{
    // Update existing entries

    // Or, add new entries if they did not exists
}

// Submit all changes (updates & inserts)
context.SubmitChanges(delegate(SubmitOperation operation)
    {
        if (operation.HasError)
        {
            operation.MarkErrorAsHandled();
        }
    }, null);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top