Link to home
Start Free TrialLog in
Avatar of NaumLitvin
NaumLitvin

asked on

How to resolve "Name does not exist in the current context" error in ASP.NET 2.0 web project?

When I run any web page from VS2005 I get the same 5 errors (see "error messages" pic). confuding part is that when I hit F5 to run a page and click YES on "build error dialog", most pages run just fine, including "offending" AdminGradeIDs (code is attached).

Some pages, however, do not run and I am getting message "can not load type" - see picture
(I will post a separate question on that)

Question in this post is "How to resolve `Name does not exist in the current context` error ?"

TIA
(code is located locally in the C:\nl\A_TEMPLETON\A_TAdmin\A_TAdminProj folder)
============  file  AdminGradeIDs.aspx  =================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdminGradeIDs.aspx.cs" Inherits="A_TAdminProj.AdminGradeIDs" %>
 
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
 
        <asp:Label ID="Label1" runat="server" Font-Size="XX-Large" Style="position: relative"
            Text="Admin Grades Table" Width="432px" Font-Bold="True"></asp:Label>
        <br />
        <br />
 
        <asp:Label ID="Label2" runat="server" Font-Bold="True" Style="left: 18px; position: relative;
            top: -1px" Text="Regions"></asp:Label>
        <asp:Label ID="Label3" runat="server" Font-Bold="True" Style="left: 48px; position: relative;
            top: -1px" Text="Category"></asp:Label>
        <asp:Label ID="Label4" runat="server" Font-Bold="True" Style="left: 81px; position: relative;
            top: -2px" Text="Tier"></asp:Label>
        <asp:Label ID="Label5" runat="server" Font-Bold="True" Style="left: 135px; position: relative;
            top: -1px" Text="Manufacturer"></asp:Label><br />
 
        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ds_ddl_Regions"
            DataTextField="reg" DataValueField="reg" Style="position: relative">
        </asp:DropDownList>
 
        <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="ds_ddl_Categories"
            DataTextField="Cat" DataValueField="Cat" Style="position: relative">
        </asp:DropDownList>
 
        <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="ds_ddl_Tiers" DataTextField="Tier"
            DataValueField="Tier" Style="position: relative">
        </asp:DropDownList>
 
        <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="ds_ddl_Manuf" DataTextField="Manuf"
            DataValueField="Manuf" Style="position: relative">
        </asp:DropDownList>
        
        <asp:SqlDataSource ID="ds_ddl_Manuf" runat="server" ConnectionString="<%$ ConnectionStrings:SPTDConnectionString %>"
                        SelectCommand="aspSelectDDLManuf" SelectCommandType="StoredProcedure">
        </asp:SqlDataSource>
 
        <asp:SqlDataSource ID="ds_ddl_Tiers" runat="server" ConnectionString="<%$ ConnectionStrings:SPTDConnectionString %>"
            SelectCommand="aspSelectDDLTier" SelectCommandType="StoredProcedure">
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="ds_ddl_Categories" runat="server" ConnectionString="<%$ ConnectionStrings:SPTDConnectionString %>"
            SelectCommand="aspSelectDDLCategories" SelectCommandType="StoredProcedure">
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="ds_ddl_Regions" runat="server" ConnectionString="<%$ ConnectionStrings:SPTDConnectionString %>"
            SelectCommand="aspSelectDDLRegions" SelectCommandType="StoredProcedure">
        </asp:SqlDataSource>
        <br />
 
 
 
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="GradeID"
            DataSourceID="ds_dv1" Height="50px" Style="left: 4px; position: relative; top: 28px"
            Width="225px" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" 
            OnPageIndexChanging="DetailsView1_PageIndexChanging" OnItemDeleted="DetailsView1_ItemDeleted" OnItemInserted="DetailsView1_ItemInserted" OnItemUpdated="DetailsView1_ItemUpdated" OnItemDeleting="DeleteView1_ItemDeleting"
             >
            <Fields>
                <asp:BoundField DataField="GradeID" HeaderText="GradeID" InsertVisible="False" ReadOnly="True"
                    SortExpression="GradeID" />
                <asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
                <asp:BoundField DataField="RegionID" HeaderText="RegionID" SortExpression="RegionID" />
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
                <asp:BoundField DataField="TierID" HeaderText="TierID" SortExpression="TierID" />
                <asp:BoundField DataField="ManufID" HeaderText="ManufID" SortExpression="ManufID" />
                <asp:BoundField DataField="Active" HeaderText="Active" SortExpression="Active" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
            <FooterStyle BackColor="Tan" />
            <EditRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <AlternatingRowStyle BackColor="PaleGoldenrod" />
        </asp:DetailsView>
 
        <asp:GridView ID="GridView11" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display."
            OnSelectedIndexChanged="GridView11_SelectedIndexChanged" 
            style="left: 248px; position: relative; top: -161px" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" DataKeyNames="gradeid" ForeColor="Black" GridLines="None" PageSize="20" >  
            <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast"
                NextPageText="Next" PageButtonCount="5" Position="TopAndBottom" PreviousPageText="Prev" />
            <FooterStyle BackColor="Tan" />
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="gradeid" HeaderText="Grade Id" InsertVisible="False" ReadOnly="True"
                    SortExpression="gradeid" />
                <asp:BoundField DataField="grade" HeaderText="Grade" SortExpression="grade" />
                <asp:BoundField DataField="manufacturer" HeaderText="Manufacturer" SortExpression="manufacturer" />
                <asp:BoundField DataField="regionname" HeaderText="Region Name" SortExpression="regionname" />
                <asp:BoundField DataField="tiername" HeaderText="Tier Name" SortExpression="tiername" />
                <asp:BoundField DataField="categoryname" HeaderText="Category Name" SortExpression="categoryname" />
                <asp:BoundField DataField="active" HeaderText="Active" SortExpression="active" />
            </Columns>
            <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Left" />
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <AlternatingRowStyle BackColor="PaleGoldenrod" />
        </asp:GridView>
 
            <%-- from DELETE: AND [Grade] = @original_Grade AND [RegionID] = @original_RegionID AND [CategoryID] = @original_CategoryID AND [TierID] = @original_TierID AND [ManufID] = @original_ManufID AND [Active] = @original_Active"
            
            from UPDATE:             AND [Grade] = @original_Grade AND [RegionID] = @original_RegionID AND [CategoryID] = @original_CategoryID AND [TierID] = @original_TierID AND [ManufID] = @original_ManufID AND [Active] = @original_Active"
            
            --%>
        
        <asp:SqlDataSource ID="ds_dv1" runat="server" ConflictDetection="CompareAllValues"
            ConnectionString="<%$ ConnectionStrings:SPTDConnectionString %>" 
            
            DeleteCommand="DELETE FROM [Grades] WHERE [GradeID] = @original_GradeID"
 
            InsertCommand="INSERT INTO [Grades] ([Grade], [RegionID], [CategoryID], [TierID], [ManufID], [Active]) VALUES (@Grade, @RegionID, @CategoryID, @TierID, @ManufID, @Active)"
            
            OldValuesParameterFormatString="original_{0}" 
            
            SelectCommand="SELECT [GradeID], [Grade], [RegionID], [CategoryID], [TierID], [ManufID], [Active] FROM [Grades] WHERE ([GradeID] = @GradeID) ORDER BY [Grade]"
            
            UpdateCommand="UPDATE [Grades] SET [Grade] = @Grade, [RegionID] = @RegionID, [CategoryID] = @CategoryID, [TierID] = @TierID, [ManufID] = @ManufID, [Active] = @Active WHERE [GradeID] = @original_GradeID" 
            >
            
            <DeleteParameters>
                <asp:Parameter Name="original_GradeID" Type="Int32" />
                <asp:Parameter Name="original_Grade" Type="String" />
                <asp:Parameter Name="original_RegionID" Type="Byte" />
                <asp:Parameter Name="original_CategoryID" Type="Byte" />
                <asp:Parameter Name="original_TierID" Type="Byte" />
                <asp:Parameter Name="original_ManufID" Type="Int32" />
                <asp:Parameter Name="original_Active" Type="String" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Grade" Type="String" />
                <asp:Parameter Name="RegionID" Type="Byte" />
                <asp:Parameter Name="CategoryID" Type="Byte" />
                <asp:Parameter Name="TierID" Type="Byte" />
                <asp:Parameter Name="ManufID" Type="Int32" />
                <asp:Parameter Name="Active" Type="String" />
                <asp:Parameter Name="original_GradeID" Type="Int32" />
                <asp:Parameter Name="original_Grade" Type="String" />
                <asp:Parameter Name="original_RegionID" Type="Byte" />
                <asp:Parameter Name="original_CategoryID" Type="Byte" />
                <asp:Parameter Name="original_TierID" Type="Byte" />
                <asp:Parameter Name="original_ManufID" Type="Int32" />
                <asp:Parameter Name="original_Active" Type="String" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="Grade" Type="String" />
                <asp:Parameter Name="RegionID" Type="Byte" />
                <asp:Parameter Name="CategoryID" Type="Byte" />
                <asp:Parameter Name="TierID" Type="Byte" />
                <asp:Parameter Name="ManufID" Type="Int32" />
                <asp:Parameter Name="Active" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView11" DefaultValue="0" Name="GradeID" PropertyName="SelectedValue"
                    Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
 
 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
            ConnectionString="<%$ ConnectionStrings:SPTDConnectionString %>" DeleteCommand="asp_null"
            DeleteCommandType="StoredProcedure" InsertCommand="asp_null" InsertCommandType="StoredProcedure"
            OldValuesParameterFormatString="original_{0}" ProviderName="<%$ ConnectionStrings:SPTDConnectionString.ProviderName %>"
            SelectCommand="aspSelectGrid1" SelectCommandType="StoredProcedure" UpdateCommand="asp_null"
            UpdateCommandType="StoredProcedure"></asp:SqlDataSource>
 
    </form>
</body>
</html>
========   file AdminGradeIDs.aspx.cs   ===================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
namespace A_TAdminProj
{
    public partial class AdminGradeIDs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
        protected void GridView11_SelectedIndexChanged(object sender, EventArgs e)
        {
            DetailsView1.DataBind();
        }
        protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
        {
            GridView11.DataBind();
        }
        protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            GridView11.DataBind();
        }
        protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
        {
            GridView11.DataBind();
        }
        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            GridView11.DataBind();
        }
        protected void DeleteView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            // if (MessageBox.Show("Really delete?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // a 'DialogResult.Yes' value was returned from the MessageBox
                // proceed with your deletion
            }
        }
    }
}

Open in new window

err9219a.jpg
err9219b.jpg
err9219c.jpg
Avatar of Craig Wardman
Craig Wardman
Flag of United Kingdom of Great Britain and Northern Ireland image

when u click 'yes' to that error message, the website runs as the last known working build...

Code infront changes will have happend, as these dont rely on changes in the compiled code. However any new classes (including the code behind for new pages) will not exist.

In your your page code infront, you are trying to inherit from a class that doesnt exist in the current build.

Those other errors you are seeing are probably caused by a problem in the .designer.cs file, maybe you are referencing a server control that hasnt been compiled yet, or maybe the designer file is out of sync.

Try adding another ASP.NET control to the page and MSVS will generate a new designer file. If that doesnt fix it, try looking at the .designer.cs file and see if there are any obvious problems.

Also, make sure code behind matches the namespacing from your code infront (i.e. check you havent declared a different namespace for this page in code behind, without updating the code infront)
Avatar of NaumLitvin
NaumLitvin

ASKER

1) I added a new control and it did not help.

2)I guess namespacing gets generated when I have a solution. When I have only a project, no namespacing is generated and (other) code works just fine without namespacing. So I tried to remove namespasing info from both files - did not help either. Do you see any problems with the code? Both DetailsView1 and GridView11 seem to be properly declared in the code infront...

3) I do not see designer.cs file for AdminGradeIDs at all, even after I added a new control. Is there an option somewhere I need to check? I do not remember seeing designer files before,  However now, when I add a new webform to my project (which exists in a solution) designer file is generated... When and why are they needed?

thanks

in the 'solution explorer' toolbox there are five icons accross the top.. The second icon from the left should 'show all files' - with this selected all of the your .aspx files should have a little + sign by their name so you can expand the view to show the codebehind and designer file.

Alternatively, you can right click on an aspx file in solution and from the context menu select, 'view code gen file' (not sure if this was present in vs2005, im using 2008 here)

Basically, your code infront and code behind are all partial classes that when compiled will form your GUI and event handling code. When you have a 'runat=server' control on your GUI, .NET needs to have representation of that control in the event handling code. It does this by automatically creating decalarations in a seperate code behind file, the designer file.

It keeps that file seperate from your code behind, so that your code isnt overwrriten with the generated code whenever you something, but because they are all marked as partial, they are all the same class.

.NET generally gets this right, but if you decide to use another namespace in your code behind, you must update your code infront to reflect this and .NET will generate a new designer file reflecting those updates.

If you havent messed with your namespace then this shouldnt be an issue. Sometimes .NET gets out of sync between the codeinfront and designer file, so this is usually fixed by adding another runat=server control and saving, to make it regenate the file.

If this does not fix it still, try doing a rebuild all.
I usually work with all files visible (see pic). as you can see some pages do not have designer file... I do not see it in most of my other working solutions so still not sure when it is needed and when not... but your explanation certainly helps, thanks!

when I select 'view code gen file' nothing happens. I refresh solution explorer, switched to another solution and back - no designer file was created...

when I try to build the project from the Build menu I am getting errors which are what this questions is about... do you see any problems with name space in the code?

any other ideas?



pic9220a.jpg
ASKER CERTIFIED SOLUTION
Avatar of Craig Wardman
Craig Wardman
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, whatever that does, did the trick! It built these designer files and the error disappeared, I can build the project and it seems to work fine.

thanks!!
it would be great to know why this "convert to web application" does to (maybe) avoid such situations in the future... but hey, it works, now!