Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5.1 > BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 10th, 2015, 07:04 PM
Authorized User
Points: 233, Level: 4
Points: 233, Level: 4 Points: 233, Level: 4 Points: 233, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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..
Reply With Quote
  #2 (permalink)  
Old August 19th, 2015, 03:08 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:12 PM.


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