Hi everybody,
Here I am again with something that I am pretty sure that will help some people to solve the problem to use the asp .NET persistent profile functionality with new Web Application projects or migrated Websites projects into that solution type.
I expended a lot of time trying to retrieve the data from the aspnet_profile table but the Profile.GetProfile() function does not exist neither the HttpContext.Current.Profile bring me any data.
looking for a solution, I found that I need to create a custom class, ProfileBase inherited with the methods GetProfile to create the profile calling the create method of the ProfileBase SuperClass and return the data converting it to the created class with cast, but always return Nothing because error casting.
So, what I did.
Web.Config
<profile defaultProvider="SqlProvider" enabled="true">
<providers>
<clear />
<add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="EDI" applicationName="ediprov" description="SQLProfile provider for EDI" />
</providers>
<properties>
<add name="Nombre" type ="string"/>
<add name="SAPKey" type ="string"/>
</properties>
</profile>
Saving the profile
To save the profile I only used the ProfileBase like this
Dim profile As ProfileBase = ProfileBase.Create(CreateUserWizard1.UserName, True)
profile("Nombre") = TxtNombre.Text
profile("SAPKey") = txtSAPKey.Text
profile.Save()
Retrieving the Profile Data from Database
I make a class to retrieve the data from the database and fill the HttpContext.Current.Profile properties for a user, so then we can use it.
Imports System.Web.Profile
Imports System.Data
Imports System.Data.SqlClient
Public Class UserProfile
Inherits ProfileBase
Public Shared Sub GetUserProfile(UserName As String)
Dim con As SqlConnection = Nothing
Try
Dim rootWebConfig As System.Configuration.Configuration
rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/" & ConfigurationManager.AppSettings("SiteName"))
con = New SqlConnection(rootWebConfig.ConnectionStrings.ConnectionStrings("EDI").ToString())
Dim Cmd As SqlCommand
Dim dt As New DataTable
Dim da As New SqlDataAdapter
'
Cmd = Con.CreateCommand
Cmd.Connection.Open()
Cmd.CommandText = "SELECT * FROM aspnet_profile where userid in (SELECT userid FROM aspnet_users WHERE UserName = '" & UserName & "')"
da.SelectCommand = Cmd
da.Fill(dt)
Dim cntproperties As Long
Dim arrPSettings() As String
For Each dr As DataRow In dt.Rows
arrPSettings = dr("PropertyNames").ToString.Split(":")
cntproperties = arrPSettings.Count / 4
For ix = 0 To cntproperties Step 4
HttpContext.Current.Profile.SetPropertyValue(arrPSettings(ix), dr("PropertyValuesString").ToString.Substring(arrPSettings(ix + 2), arrPSettings(ix + 3)))
Next
Next
Catch ex As Exception
Finally
con.Close()
End Try
End Sub
Public Property Nombre() As String
Get
Return HttpContext.Current.Profile.GetPropertyValue("Nombre")
End Get
Set(value As String)
value = HttpContext.Current.Profile.GetPropertyValue("Nombre")
End Set
End Property
Public Property SAPKey() As String
Get
Return HttpContext.Current.Profile.GetPropertyValue("SAPKey")
End Get
Set(value As String)
value = HttpContext.Current.Profile.GetPropertyValue("SAPKey")
End Set
End Property
End Class
Retrieving the Profile
If you use MasterPages it is effort saving to call the GetUserProfile there to have the Context Updated,
calling it like this.
UserProfile.GetUserProfile(HttpContext.Current.User.Identity.Name)
After this you can get the property values from,
HttpContext.Current.Profile.GetPropertyValue("Nombre")
Hope it helps,
Greetings
No hay comentarios:
Publicar un comentario