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 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : 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 November 2nd, 2010, 11:04 AM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
Default The name 'ErrorMessage' does not exist in the current context

Three times in the last two weeks, I have encounbtered the error message:
The name 'ErrorMessage' does not exist in the current context

The error sometimes appears and sometimes it doesn't.
I couldn't find anything wrong with the code but after a bit of a rummage I found this thread in the asp.net forums.

http://forums.asp.net/t/1006588.aspx

The first solution worked for me. Viz:
From the VWD menu, choose "Website> Start Options>Build."
Then in the top dropdown box choose "No Build".

Imar, do you have a more elegant explanation and fix?
PS - This happened most recently in step 3 on page 593

Ken
Reply With Quote
  #2 (permalink)  
Old November 2nd, 2010, 11:36 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi ken,

This option may seem to work, but it doesn't really. All it does is prevent the site from being compiled when you run it, hiding potential errors from you. Not a good idea.

Instead, you need to find the problem, and fix it. Search your site for all occurrences of ErrorMessage and look if any one of them is of order.

Cheers,

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
  #3 (permalink)  
Old November 2nd, 2010, 05:23 PM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
Default

Well I tried that and found lots of instances of "ErrorMessage" but I couln't see anything wrong with them.
The only clue that I have is that the Error List points to line 40 in Genres.aspx.cs which I have highlighted in red in the following code.

Can you please explain what the error message means.
For example what is "the current context" ?

Thanks
Ken
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Management_Genres : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    switch (e.Row.RowType)
    { 
      case DataControlRowType.DataRow:
        DataRowView myDataRowView = (DataRowView)e.Row.DataItem;
        if (Convert.ToInt32(myDataRowView["NumberOfReviews"]) > 0 )
        {
        LinkButton deleteLink = e.Row.FindControl("DeleteLink") as LinkButton;
          if (deleteLink != null)
          {
  //          deleteLink.Enabled = false;  commented out for exercise on page 553
          }
        }
        break;
    }
  }
  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;
    }
  }
  }
}
Reply With Quote
  #4 (permalink)  
Old November 2nd, 2010, 05:32 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

It means it's not defined anywhere; not as a variable in the Code Behind class, not as a member in the base class (BasePage for example) and not in the markup of the page. Does the page Management/Genres.aspx contain a defition for a control called ErrorMessage? And is it in the page directly? (E.g. not hidden in an ItemTemplate of a Repeater for example?)

Cheers,

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
  #5 (permalink)  
Old November 2nd, 2010, 05:38 PM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
Default

Sorry, I don't know what you mean by "in the page directly".
Line 5 of Management/Genres has an <asp:Label ID="ErrorMessage"...
Is that "directly" ?
Ken

Code:
<%@ Page Title="Planet Wrox - Management - Genres" Language="C#" MasterPageFile="~/MasterPages/Management.master" AutoEventWireup="true" CodeFile="Genres.aspx.cs" Inherits="Management_Genres" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" runat="Server">
  <asp:Label ID="ErrorMessage" runat="server" cssClass="ErrorMessage" EnableViewState="False"></asp:Label>
  <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    AllowSorting="True" AutoGenerateColumns="False" 
    DataKeyNames="Id" DataSourceID="SqlDataSource1" 
    EmptyDataText="There are no data records to display."  
   CellPadding="4" ForeColor="#333333" GridLines="None" 
    onrowdatabound="GridView1_RowDataBound">

    <Columns>
      <asp:TemplateField ShowHeader="False">
        <EditItemTemplate>
          <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
            CommandName="Update" Text="Update"></asp:LinkButton>
          &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
            CommandName="Cancel" Text="Cancel"></asp:LinkButton>
        </EditItemTemplate>
        <ItemTemplate>
          <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
            CommandName="Edit" Text="Edit"></asp:LinkButton>
          &nbsp;<asp:LinkButton ID="DeleteLink" runat="server" CausesValidation="False" 
            CommandName="Delete" Text="Delete"></asp:LinkButton>
        </ItemTemplate>
        <ItemStyle Width="100px" />
      </asp:TemplateField>
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" 
        ItemStyle-Width="200px">
<ItemStyle Width="200px"></ItemStyle>
      </asp:BoundField>
      <asp:BoundField DataField="SortOrder" HeaderText="Sort Order" SortExpression="SortOrder" />
    </Columns>
   
  </asp:GridView>
  <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" DefaultMode="Insert" Height="50px" Width="125px">
    <Fields>
      <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
      <asp:BoundField DataField="SortOrder" HeaderText="SortOrder" SortExpression="SortOrder" />
      <asp:CommandField ShowInsertButton="True" />
    </Fields>
  </asp:DetailsView>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:PlanetWroxConnectionString1 %>" 
    DeleteCommand="DELETE FROM [Genre] WHERE [Id] = @Id" 
    InsertCommand="INSERT INTO [Genre] ([Name], [SortOrder]) VALUES (@Name, @SortOrder)" 
    ProviderName="<%$ ConnectionStrings:PlanetWroxConnectionString1.ProviderName %>" 
    SelectCommand="SELECT Genre.Id, Genre.Name, Genre.SortOrder, Count(Review.Id) AS NumberOfReviews FROM Genre LEFT OUTER JOIN Review ON Genre.Id = Review.GenreId GROUP BY Genre.Id, Genre.Name, Genre.SortOrder" 
    UpdateCommand="UPDATE [Genre] SET [Name] = @Name, [SortOrder] = @SortOrder WHERE [Id] = @Id" 
    ondeleted="SqlDataSource1_Deleted">
    <DeleteParameters>
      <asp:Parameter Name="Id" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
      <asp:Parameter Name="Name" Type="String" />
      <asp:Parameter Name="SortOrder" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
      <asp:Parameter Name="Name" Type="String" />
      <asp:Parameter Name="SortOrder" Type="Int32" />
      <asp:Parameter Name="Id" Type="Int32" />
    </UpdateParameters>
  </asp:SqlDataSource>
  <script type="text/javascript">
    $(function () {
      $('.GridView tr:odd:not(.GridViewPagerStyle)').
            addClass('GridViewAlternatingRowStyle');
    });
  </script>
</asp:Content>
Reply With Quote
  #6 (permalink)  
Old November 2nd, 2010, 06:01 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

What I mean with in the "page directly" was a top level control. E.g. one that is inside a Content placeholder when using a Master Page, or somewhere between the <form> tags when not using a Master Page. This means the control is directly accessible from the Code Behind. If the control were located in a Repeater or a ListView or some other data bound control, it's not directly addressable.

You posted the markup of the Master Page, but a Code Behind of the Genres page. Does Genres.aspx contain an ErrorMessage control as well? If not, the error makes sense as you can't access controls from a page defined in a Master Page like this directly. The fix is to add a control called ErrorMessage to Genres.aspx or to remove the code that tries to set the ErrorMessage from its Code Behind.

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
  #7 (permalink)  
Old November 2nd, 2010, 08:28 PM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
Default

"You posted the markup of the Master Page, but a Code Behind of the Genres page"

Hmmm, maybe I misunderstand something here but that's not the way it looks to me. As far as I can see, I posted the Genres.aspx page. (It is the one in the ~/Management folder)

And the ErrorMessage is in a label control that is immediately after the opening <asp: Content 2... code.

Ken

FYI: here is the Management.master
Code:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Management.master.cs" Inherits="MasterPages_Management" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
  <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
  <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <div style="width: 200px; float: left;">
      <ul>
        <li><a href="~/Management/Default.aspx" runat="server">Management Home</a></li>
        <li><a href="~/Management/Genres.aspx" runat="server">Manage Genres</a></li>
        <li><a href="~/Management/Reviews.aspx" runat="server">Manage Reviews</a></li>
      </ul>
    </div>
    <div style="width: 750px; float: left;">
      <asp:ContentPlaceHolder ID="cpMainContent" runat="server">
      </asp:ContentPlaceHolder>
    </div>
  </div>
  </form>
</body>
</html>
And here is the code behind of the Management.Master
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPages_Management : System.Web.UI.MasterPage
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
}

Last edited by ken evans; November 2nd, 2010 at 08:29 PM.. Reason: typo
Reply With Quote
  #8 (permalink)  
Old November 3rd, 2010, 03:10 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
Hmmm, maybe I misunderstand something here but that's not the way it looks to me. As far as I can see, I posted the Genres.aspx page.
You're absolutely right. My bad, sorry. I looked at the code you posted, saw MasterPage and thought you posted the code for the master page (overlooked the fact that it was "MasterPageFile" which is of course perfectly fine for a content page such as Genres.

Anyway, I can't see anything wrong with that page; I copied it into my Planet Wrox web site and it works fine. ErrorMessage is perfectly "in context".

The only reason for this error I can think of is that maybe something else in your site is not OK. Try rebuilding the site by right-clicking iit in the Solution Explorer and choosing Build Web Site. Then look at the Error List window. Does it list other errors?

If that doesn't help, I don't know the reason for this. Could be a bug in Visual Studio, or a local machine issue, or anything else ;-)

Cheers,

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
The Following User Says Thank You to Imar For This Useful Post:
ken evans (November 3rd, 2010)
  #9 (permalink)  
Old November 3rd, 2010, 06:13 AM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by Imar View Post
Try rebuilding the site by right-clicking iit in the Solution Explorer and choosing Build Web Site. Then look at the Error List window. Does it list other errors?
I did this and :
1: The error appeared after the build.
2: After about 1 second, the error disappeared and the site launched and appears to be behaving normally.

Thanks Imar.

Ken

Last edited by ken evans; November 3rd, 2010 at 06:14 AM.. Reason: typo
Reply With Quote
  #10 (permalink)  
Old November 3rd, 2010, 06:48 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

The Error List is not always 100% in sync with the actual code. Visual Studio compiles the site in the background, so you may see erorrs that are not really errors when compilation is done. If the error disappears automatically, there's no problem and everything should run fine.

Cheers,

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
The name 'Request' does not exist in the current context janilane BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 July 13th, 2010 05:46 PM
P552: The name 'Roles' does not exist in the current context eddiemcham BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 3 May 1st, 2009 05:03 PM
The name 'partOneStep' does not exist in the current context pallone ASP.NET 3.5 Basics 11 March 4th, 2009 05:30 AM
The name 'txtOperande1' does not exist in the current context stevemcd999 BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 3 February 25th, 2009 11:58 PM
Position of a node outside current context QuickSilver002 XSLT 2 April 19th, 2007 02:07 PM



All times are GMT -4. The time now is 05:39 PM.


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