Update dB field based on HttpWebResponse in MVC controller

I have a list of URLs in a SQL dB table and an MVC project that acts upon these.

I want to iterate through the list and check the HttpWebResponse.

If ok, move on, if not update a field in the dB.

I am doing this in an MVC Controller and here is where I have so far with failed results.

ECmodel db = new ECmodel();
        // GET: Clean
        public ActionResult Index()
        {


            // db items = ECmodel.GetAllRows();
            //return View(items);

            var church = from m in db.ECMains
                         where m.Auth == "Yes" && m.Live == "Y"
                         select m;


            foreach (var item in church)
            {
                HttpWebRequest webRequest = HttpWebRequest.Create(@item.URL) as HttpWebRequest;
                webRequest.Method = WebRequestMethods.Http.Get;
                webRequest.ContentType = "application/x-www-form-urlencoded";
                HttpWebResponse response = null;
                try
                {
                    response = webRequest.GetResponse() as HttpWebResponse;
                    if (response.StatusCode != HttpStatusCode.OK)
                    {

                        //Need to update db

                        int ID = @item.ID;

                        db.ECMains.Where(u => u.ID == ID).Single(u => u.Live == "N");
                        db.SaveChanges();

                        //var Live = (from p in db.
                        //               where p.ID == ID
                        //               select p).Single();

                        //Live.Live = "N";

                        //db.SubmitChanges();


                        //Update("john", user => user.LastModifiedDate = DateTime.Now);


                    }
                }
                catch (WebException ex) { }
                finally
                {
                    if (response != null)
                    { response.Dispose(); }
                }

            }

            return View();
        }

I verified the ID is being returned and tried this.

int ID = @item.ID;
var live = db.ECMains.Find(ID);
live.Live = "N";
db.SaveChanges();

That didn’t work and being fresh to MVC I am having a bit of trouble stepping through this.

If i remove the foreach loop and simply return the church.ToList() the view lists all id’s correctly.

How would I check the loop to see where this is failing?

Thank you for taking a look

-Doug

dotnet714

foreach (var item in church)
            {
                HttpWebRequest webRequest = HttpWebRequest.Create(@item.URL)

I doubt this code compiles on C# class. What is the @ before @item ?

dotnet714

That didn’t work

Some error? What does not work?

dotnet714

That didn’t work and being fresh to MVC

Here does not matter MVC. You can reproduce this on a console project – or windows forms.

ignatandrei

foreach (var item in church)
            {
                HttpWebRequest webRequest = HttpWebRequest.Create(@item.URL)

I doubt this code compiles on C# class. What is the @ before @item ?

This represents the value of the corresponding field in the foreach loop and is compiling and returning values correctly. My goal is to loop through the results of the query, check for URL for a response and update the dB based on that result.

ignatandrei

dotnet714

That didn’t work

Some error? What does not work?

No error, the browser timed out and no field was updated.

Leave a Reply