I'm currently doing the try it out on p. 575 - "Handling Errors when Deleting Rows".
The exercise involves writing a handler method that fires when a user tries to delete a row from the Genres table that has reviews associated with it. A Label is added above the GridView that prints an error message if the user tries to do this (in Genres.aspx)-
<asp:Label ID="ErrorMessage" runat="server" CssClass="ErrorMessage" EnableViewState="False"></asp:Label>
In the code behind file the handler method catches the exception and sets the text property of the label-
protected void SqlDataSource1_Deleted(object sender, SqlDataSourceStatusEventArgs e)
if (e.Exception != null && e.Exception is SqlException)
SqlException myException = (SqlException)e.Exception;
if (myException.Number == 547)
ErrorMessage.Text = @"Sorry, you can't delete this genre because it has associated reviews that you need to delete first.";
e.ExceptionHandled = true;
However, when I attempt to build the site to view the page I get the following error-
C:\BegASPNET\Site\Management\Genres.aspx.cs(42,9): error CS0103: The name 'ErrorMessage' does not exist in the current context
I've even tried removing my own files and substituting them with the ones from the Chapter 15 source code download, but still get the same error.
Any ideas on what might be happening?
All help greatly appreciated,