2

When i created a profile and when i add items it always says not declared in the code behind!!

I tried to change the Framework of the project from Framework 4.0 to Framework 3.5 and it still didn't work.

It says FirstNamep , LastNamep are not declared .

And in the Web.config :

   <profile defaultProvider="CustomProfileProvider" enabled="true">

   <providers>

  </providers>

  <!-- Define the properties for Profile... -->
  <properties>
      <add name="FirstNamep" type="String" />
      <add name="LastNamep" type="String" />

  </properties>
</profile>

Behind the Code:

    Profile.FirstNamep = FirstNameTextBox.Text 
    Profile.LastNamep = LastNameTextBox.Text
FBEvo1
  • 61
  • 1
  • 12
  • possible duplicate of [Accessing the Profile object from Code Behind Bug? (ASP.NET 2.0 Provider Model)](http://stackoverflow.com/questions/407845/accessing-the-profile-object-from-code-behind-bug-asp-net-2-0-provider-model) – McGarnagle Sep 28 '12 at 22:53
  • Is it a website or WAP template you're using? Profiles are not available out of the box with the latter. – IrishChieftain Sep 28 '12 at 23:32

1 Answers1

0

The properties are dynamically generated at runtime, which means you can't access them from code-behind. What you can do is access them from your .ASPX pages using a script block (if that works for you). Like this.

<%@ Page Language="C#" %>

<script runat="server">
    public void Page_Init()
    {
        Profile.FirstNamep = "some dood";
    }
</script>

<div>Your name is <%= Profile.FirstNamep %></div>

It seems to be sort of "by design" that the Profile is available to .aspx pages, but not to the code behind.


If you've defined the default provider as CustomProfileProvider, then that has to be a class that inherits System.Web.Profile.ProfileProvider. Otherwise, you should use the default SQL profile provider.

<connectionStrings>
   <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>

<membership>
    <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
    </providers>

   

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • @FBEvo1 you defined it as "CustomProfileProvider"... that has to be a real time, otherwise use the SQL profile provider: http://msdn.microsoft.com/en-us/library/014bec1k(v=vs.100).aspx – McGarnagle Sep 28 '12 at 23:14
  • thank you i changes it from CustomProfileProvider to AspNetSqlProfileProvider and its running , However if i want to create a FirstName and LastName entry in my Register page so each User can have a FirstName and LastName and when they login instead of the UserName being displayed i want (FirstName + LastName), how can i do that? – FBEvo1 Sep 28 '12 at 23:21
  • @FBEvo1 can't you just write `<%= Profile.FirstName + " " + Profile.LastName %>` wherever you want it displayed? – McGarnagle Sep 28 '12 at 23:22
  • ok thanks again, Currently i have A FirstName and LastName textBox in my User Registration Page,so when i click the finish signup button how can it save the first and last name entries? ,Btw you have been very Helpful! – FBEvo1 Sep 28 '12 at 23:27
  • @FBEvo1 in the page load event (in the script block), just check for `Page.IsPostback`, then set the values using `Profile.FirstNamep = FirstName.Text`, and then `Profile.Save()`. – McGarnagle Sep 28 '12 at 23:29