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
2

1 Answer 1

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>

   

5
  • @FBEvo1 you defined it as "CustomProfileProvider"... that has to be a real time, otherwise use the SQL profile provider: msdn.microsoft.com/en-us/library/014bec1k(v=vs.100).aspx
    – McGarnagle
    Sep 28, 2012 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, 2012 at 23:21
  • @FBEvo1 can't you just write <%= Profile.FirstName + " " + Profile.LastName %> wherever you want it displayed?
    – McGarnagle
    Sep 28, 2012 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, 2012 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, 2012 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.