locked
Profile asp.net vb RRS feed

  • Question

  • User1717218719 posted

    Hi all,

    I have been haveing some issues getting my code for my profile page to work. I am not recieving any errors but am unable to fill my textbox with the data from the profile sql datatable even though there is data in the table. note also I am using a web application

    My code is below, any help would be great.

    Many Thanks

    <%@ Page Language="vb" AutoEventWireup="true" CodeBehind="MyProfile.aspx.vb" %>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>My Profile Page</title>
    </head>
    <body>
     <form id="form1" runat="server">
        <div>
            <h1>
                Profile Settings for: <asp:LoginName ID="LoginName1" runat="server" />
            </h1>
            
            <table border="1" style=width:50%>
                <tr>
                    <td>UserId:</td>
                    <td><asp:label ID="UserId" runat="server" ></asp:label></td>
                </tr>
    
                <tr>
                    <td>Gender:</td>
                    <td><asp:label ID="Gender" runat="server" ></asp:label></td>
                </tr>
            
                <tr>
                    <td>Age:</td>
                    <td><asp:label ID="Age" runat="server"></asp:label></td>
                </tr>        
            
                 <tr>
                    <td>Roles:</td>
                    <td><asp:ListBox ID="RoleList" runat="server"/></td>
                </tr>       
            </table>
        </div>
        </form>
    </body>
    </html>
    
    Public Class MyProfile
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    '-- I have tried the below cod with no success
    
            'Response.Write(HttpContext.Current.Profile.PropertyValues("UserId"))
    
            'UserId.Text = Profile.UserId 
            'Gender.Text = Profile.Gender
            'Age.Text = Profile.Age.ToString()
    
            'RoleList.DataSource = Roles.GetRolesForUser(User.Identity.Name)
            'RoleList.DataBind()
    
        End Sub
    
    End Class
    
    
    <?xml version="1.0" encoding="utf-8"?>
    
    <!--For more information on how to configure your ASP.NET application, please visit
      https://go.microsoft.com/fwlink/?LinkId=169433-->
    
    <configuration>
      <connectionStrings>
        <add name="myConnectionString" connectionString="server=xxxxxxxx;initial catalog=xxxxxxxx;persist security info=True;" providerName="System.Data.SqlClient" />
      </connectionStrings>
    
      <system.web>
        <compilation debug="true" strict="false" explicit="true" targetFramework="4.7.2" />
        <httpRuntime targetFramework="4.7.2" />
        
        
        
        <profile defaultProvider="SqlProvider">
          <providers>
            <clear/>
            <add name="SqlProvider"
               type="System.Web.Profile.SqlProfileProvider"
               connectionStringName="myConnectionString"
               applicationName="/"
               description="SqlProfileProvider for SampleApplication" />
          </providers>
          <properties>
            <add name="UserId" />
            <add name="Gender" type="string"/>
            <add name="Age" type="Int32"/>
          </properties>
        </profile>
           
        <pages>
          <namespaces>
            <add namespace="System.Web.Optimization" />
          </namespaces>
          <controls>
            <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
          </controls>
        </pages>
      </system.web>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
            <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
            <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <system.codedom>
        <compilers>
          <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
          <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
        </compilers>
    
      </system.codedom>
    
    </configuration>
    
    
     Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
        </compilers>
      </system.codedom>
    </configuration>-->

    Wednesday, September 18, 2019 8:40 AM

All replies

  • User288213138 posted

    Hi E.RU,

    Do you set allowAnonymous="true"?

    AllowAnonymous specifies a Boolean value that indicates whether the property is managed for anonymous users. By default, this is false. If you want the property to be available for unauthenticated users, you can set the property to true. So if you not set the allowAnonymous, you will can't get the property.

    More information you can refer to this link: 

    https://docs.microsoft.com/en-us/previous-versions/aspnet/ewfkf772(v=vs.100)#anonymous-users

    Best regards,

    Sam

    Thursday, September 19, 2019 11:14 AM
  • User1717218719 posted

    Thanks Samwu for your reply,

    unfortunatly this made no difference to my code, and I still have the same probelm.

    is there anhing silly like i need to open connection string or something like that taht u can see wrong with my code?

    many thanks for your help.

    Friday, September 20, 2019 11:23 AM
  • User288213138 posted

    Hi E.RU,

    I am very sorry that I did not answer your question in time.

    E.RU

    note also I am using a web application

    E.RU

    'UserId.Text = Profile.UserId
            'Gender.Text = Profile.Gender
            'Age.Text = Profile.Age.ToString()

    As far as I know, Web application can't directly use Label1.Text = Profile.XXX as in the website. Compiling this way will directly report errors.

    In web application, the code that reads the Profile value can be changed as below code:

    HttpContext.Current.Profile.GetProfileGroup("GroupName").GetPropertyValue("PropertyName");  //Profile has a grouping
    
    HttpContext.Current.Profile.GetPropertyValue("GroupName.PropertyName");  //Profile is another way of writing it
    
    HttpContext.Current.Profile.GetPropertyValue("PropertyName"); //Profile without grouping

    I test code:

    <profile defaultProvider="SqlProvider">
          <providers>
            <clear/>
            <add name="SqlProvider"
               type="System.Web.Profile.SqlProfileProvider"
               connectionStringName="myConnectionString"
               applicationName="/"
               description="SqlProfileProvider for SampleApplication" />
          </providers>
          <properties>
            <add name="UserId" defaultValue="this is userid" />
            <add name="Gender" type="string" defaultValue="this is gender"/>
            <add name="Age" type="Int32" defaultValue="18"/>
          </properties>
        </profile>


    UserId.Text = HttpContext.Current.Profile.GetPropertyValue("UserId")
    Gender.Text = HttpContext.Current.Profile.GetPropertyValue("Gender")
    Age.Text = HttpContext.Current.Profile.GetPropertyValue("Age")

    The result:

    Best regards,

    Sam

    Wednesday, September 25, 2019 12:04 PM
  • User1717218719 posted

    Thank you for your reply it is most helpful.
    the issue i am still facing is that I am trying to fill the profile information from an sql database rather thank using a default value. are u familiar with how I should code this?  
       

    Monday, September 30, 2019 7:57 AM
  • User288213138 posted

    Hi E.RU,

    fill the profile information from an sql database

    You can try to use the SQL Table Profile Provider.

    The <profile> node in web.config file tells ASP.NET to use the custom SQLTableProfileProvider rather than the built-in default version. In the snippet below you can see the inherits attribute's value tells ASP.NET the type followed by a comma and then the assembly where it can find the type.  The table attribute points to the name of the SQL database table where the Profile data is being stored. In this case the table name is aspnet_Profile2.

    <profile enabled="true" 
            defaultProvider="TableProfileProvider"  
            inherits="Microsoft.Samples.ProfileCommon,SQLTableProfileProviderCS">
      <providers>
        <clear/>
        <add name="TableProfileProvider"
                type="Microsoft.Samples.SqlTableProfileProvider, SQLTableProfileProviderCS"
                connectionStringName="LocalSqlServer" table="aspnet_Profile2" 
                applicationName="/" />
      </providers>
    </profile>

    Best regards,

    Sam

    Monday, September 30, 2019 11:03 AM