C# .NET - Error: The name 'Profile' does not exist in current context

Asked By Elvin on 08-Oct-11 02:44 PM
I got the error on various pages, but just concentrating it on the 
CartControl.ascx.cs code behind page.
I am trying to build a shopping cart website and this error occured. Please assist. Errors are highlighted.
Note: I am using Visual Studio 2008.
------------------------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace zephyrAir2
{
    public partial class CartControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             if (Profile.SCart == null)
        {
            Profile.SCart = new zephyrAir2.Cart();
        }
        if (!Page.IsPostBack)
        {
            ReBindGrid();
        }
        if(Profile.SCart.Items == null)
        {
            TotalLabel.Visible = false;
        }
    }
    protected void grdCart_RowEditing
                 (object sender, GridViewEditEventArgs e)
    {
        grdCart.EditIndex = e.NewEditIndex;
        ReBindGrid();
    }
    protected void grdCart_RowUpdating
               (object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtQuantity = (TextBox)
        grdCart.Rows[e.RowIndex].Cells[2].Controls[0];
        int Quantity = Convert.ToInt32(txtQuantity.Text);
        if (Quantity == 0)
        {
            Profile.SCart.Items.RemoveAt(e.RowIndex);
        }
        else
        {
            Profile.SCart.Items[e.RowIndex].Quantity 
                                         = Quantity;
        }
        grdCart.EditIndex = -1;
        ReBindGrid();
    }
    protected void grdCart_RowCancelingEdit
             (object sender, GridViewCancelEditEventArgs e)
    {
        grdCart.EditIndex = -1;
        ReBindGrid();
    }
    protected void grdCart_RowDeleting
                 (object sender, GridViewDeleteEventArgs e)
    {
        Profile.SCart.Items.RemoveAt(e.RowIndex);
        ReBindGrid();
    }
    private void ReBindGrid()
    {
        grdCart.DataSource = Profile.SCart.Items;
        DataBind();
        TotalLabel.Text = string.Format("Total:{0,19:C}", 
                                     Profile.SCart.Total);
    }
        }
    }
----------------------------------------------------------------------------------------------------------

Anoop S replied to Elvin on 08-Oct-11 03:09 PM
What you want is

HttpContext.Current.Profile

http://msdn2.microsoft.com/en-us/library/2y3fs9xs%28VS.80%29.aspx

Reena Jain replied to Elvin on 08-Oct-11 03:09 PM
Hi,

I have same problem in my proyect, by using HttpCotntext.Current.Profile error has gone. so try this and let me know
Anoop S replied to Elvin on 08-Oct-11 03:12 PM
add
ProfileCommon profile = HttpContext.Current.Profile as ProfileCommon;
refer this for details
http://www.odetocode.com/Articles/440.aspx
Elvin replied to Anoop S on 08-Oct-11 07:55 PM
I am new to building a shopping cart. where exactly does HttpContext.Current.Profile goes or in other words where do I add it? Is it in the same page or in the web.config file?

my web config file is as follows:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<appSettings/>
<connectionStrings>
<!--        
   <add name="Zephyr_Air_DB2ConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Zephyr_Air_DB2.accdb;
   Persist Security Info=True"   oviderName="System.Data.OleDb" />           
   -->
<!--
<add name="ConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\CyBeRfOrCe\Desktop\Zephyr_Air_DB2.accdb"
   providerName="System.Data.OleDb" /> 
    -->
<!--<add name="ConnectionString2" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\tblsZephyrAir.mdf;Integrated Security=True;User Instance=True"
   providerName="System.Data.SqlClient" />-->
<add name="useThisConnStringYOOOOO!" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\tblsZephyrAir.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<!--   Added the following line 2,3,4,5,6,7 is set to Windows previously, and line 8-->
<system.web>
<authorization>
<allow users="?"/>
<allow roles="admin"/>
</authorization>
<roleManager enabled="true"/>
<authentication mode="Forms"/>
<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
<!--   Added the following web.config-->
<system.web>
<anonymousIdentification enabled="true"/>
<profile enabled="true">
<properties>
<add name="SCart" serializeAs="Binary" type="zephyrAir2.Cart" allowAnonymous="true"/>
</properties>
</profile>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Elvin replied to Anoop S on 08-Oct-11 08:02 PM
Changed only to this:
//ProfileCommon anonymousProfile = Profile.GetProfile(e.AnonymousID);
            ProfileCommon anonymousProfile = (anonymousProfile)HttpContext.Current.Profile;

But i get the same error as before where Profile is not found in current context plus these two others:

Error 1 The type or namespace name 'ProfileCommon' could not be found (are you missing a using directive or an assembly reference?) C:\Users\CyBeRfOrCe\Documents\Visual Studio 2008\Projects\zephyrAir2\Global.asax.cs 54 13 zephyrAir2

Error 2 The type or namespace name 'anonymousProfile' could not be found (are you missing a using directive or an assembly reference?) C:\Users\CyBeRfOrCe\Documents\Visual Studio 2008\Projects\zephyrAir2\Global.asax.cs 54 47 zephyrAir2

Anoop S replied to Elvin on 09-Oct-11 01:16 AM
Turns out that Web Applications don't support the auto generation of the ProfileCommon object like web site projects do. To get this to work install http://code.msdn.microsoft.com/WebProfileBuilder and then add it to the build process as described http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx.
Elvin replied to Anoop S on 09-Oct-11 09:19 AM
Do I have to store it in a folder of Visual Studio 2008? If so, which one?

dipa ahuja replied to Elvin on 09-Oct-11 09:31 AM
1. Enable the profile in web.config
<profile enabled="true">
2. Add the namespace :
using System.Web.Profile;
Done! 
dipa ahuja replied to Elvin on 09-Oct-11 09:41 AM
1. Enable the profile in web.config
<profile enabled="true">
2. Add the namespace :
using System.Web.Profile;
Done! 
Devil Scorpio replied to Elvin on 09-Oct-11 11:37 AM
Hi Elvin,

I think you've not imported the namespace the Profile class is in. suppose if the Profile class is under namespace "MyNamespace" import it to your page as well like


using MyNamespcae;

Elvin replied to dipa ahuja on 09-Oct-11 11:52 AM
I added it already before you posted and I get the same error:

I added this:

<system.web>
    <authorization>
      <allow users="?" />
      <allow roles="admin" />
    </authorization>
    <roleManager enabled="true" />
    <authentication mode="Forms" />
    <compilation debug="true">
</system.web>
<!--   Added the following web.config-->
  <system.web>


I also added this to the Global.asax.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using System.Web.Profile;

    <anonymousIdentification enabled="true"/>
    <profile enabled="true">
      <properties>
        <add name="SCart" serializeAs="Binary"
             type="zephyrAir2.Cart"
             allowAnonymous="true"/>
      </properties>
    </profile>
  </system.web>
dipa ahuja replied to Elvin on 09-Oct-11 12:27 PM
have you added the namespace ?

using System.Web.Profile;

If yes then post the error what you are getting. We understood where you are getting error but what error you are getting post that
Elvin replied to dipa ahuja on 09-Oct-11 10:55 PM
I am making a school project with team that  barely does something or code. 

Here is my project: