Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 10th, 2015, 07:04 PM
Authorized User
 
Join Date: Aug 2011
Posts: 44
Thanks: 14
Thanked 0 Times in 0 Posts
Default Chapter 15 - Hand-coding data access pages

Hello Imar,

in chapter 15 you create a hand-coded version of AddEditReview page - based on the Entity Framework and not on a SqlDataSourceControl.
However there is no handling of errors that may occur in the DB access.
For instance, if I try to update a review which, in the meantime, was deleted by another user of the applicaton I get a System.InvalidOperationException.
I've chosen to manage the exception in a try/catch and Response.Redirect the user to an error page, but I would like to know if there is a more elegant way to manage the DB exception within Entity Framework
like you did on page 562 where you caught a SqlException 547 from a SqlDataSource control.

Below my code.
Thanks
Antonius

Code:
    protected void SaveButton_Click(object sender, EventArgs e)
    {
      try
      {
        using (PlanetWroxEntities istanzaDb = new PlanetWroxEntities())
        {
          Review myReview = null;
          if (_id == -1) // Insert new item
          {
            myReview = new Review();
            myReview.CreateDateTime = DateTime.Now;
            myReview.UpdateDateTime = myReview.CreateDateTime;
            istanzaDb.Reviews.Add(myReview);
          }
          else // update existing item
          {
            var counter = (from record in istanzaDb.Reviews
                              where record.Id == _id
                              select record).Count();
            if (counter != 1)
            {
              ErrorMessage.Text = "The review was not found in the DB any more";
              return;             
             }
            else
            {
              myReview = (from record in istanzaDb.Reviews
                          where record.Id == _id
                          select record).Single();
              myReview.UpdateDateTime = DateTime.Now;
            } // end else 
          }  // end else 
          myReview.Title = TitleText.Text;
          myReview.Summary = SummaryText.Text;
          myReview.Body = BodyText.Text;
          myReview.GenreId = Convert.ToInt32(GenreList.SelectedValue);
          myReview.Authorized = Authorized.Checked;
          istanzaDb.SaveChanges();
          Response.Redirect("Reviews.aspx");
        } // end using
      } // end try
      catch (System.InvalidOperationException)
      {
        Response.Redirect("~/Errors/ErrorDbGeneric.aspx");
      }
    }

Last edited by Antonius Block; August 11th, 2015 at 03:14 AM..
 
Old August 19th, 2015, 03:08 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

In general your approach is good: you use try/catch to catch any exceptions.

You can check if an item exists in the database without raising an exception though. On page 498 you can see that Single throws an exception when the item doesn't exist, but SingleOrDefault does not. By replacing your call to Single with SingleOrDefault, you get back null when the item doesn't exist. You can then check for null instead.

You can use the same try/catch principle as on page 562 to handle other (database) exceptions here.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 15: Try It Out - Hand-Coding Data Access Pages marie05 BOOK: Beginning ASP.NET 4.5 : in C# and VB 1 October 21st, 2014 03:45 AM
Chapter 15, Try It Out Hand-Coding Data Access Pages WebDev123 BOOK: Beginning ASP.NET 4.5 : in C# and VB 9 May 29th, 2014 07:57 AM
Hand coding data access code Wilfredo Rosado BOOK: Beginning ASP.NET 4 : in C# and VB 10 January 10th, 2014 04:29 PM
ch. 15 - Hand Coding Data Acess Code Tulsi BOOK: Beginning ASP.NET 4 : in C# and VB 6 January 11th, 2013 03:45 AM
Hand Coding VS Data Wizards Bulldog BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 May 22nd, 2007 04:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.