You are right Gary. The information is being storing in aspnet_profile table, in a field as delimeted string. Great.. !!
I Soon realized that if (not often) come in need of query the table and read the vaIues for the strored properties, It will not going to be so handy. Yes the vb code runs great and the properties are displayed correctly on my page but I like to feel that I can query (Sql Management Studio) everything easy and read the values not as a string but columns. Firstly I thought to start writing my own udf function but after a little search I found what I was looking for. I found a lot of posts asking about how to query that table and I am gonna share it with you because it is a really nice solution.
check it here
First create these two functions on SQL Server.
Code:
CREATE FUNCTION dbo.fn_GetElement
(
@ord AS INT,
@str AS VARCHAR(8000),
@delim AS VARCHAR(1) )
RETURNS INT
AS
BEGIN
-- If input is invalid, return null.
IF @str IS NULL
OR LEN(@str) = 0
OR @ord IS NULL
OR @ord < 1
-- @ord > [is the] expression that calculates the number of elements.
OR @ord > LEN(@str) - LEN(REPLACE(@str, @delim, '')) + 1
RETURN NULL
DECLARE @pos AS INT, @curord AS INT
SELECT @pos = 1, @curord = 1
-- Find next element's start position and increment index.
WHILE @curord < @ord
SELECT
@pos = CHARINDEX(@delim, @str, @pos) + 1,
@curord = @curord + 1
RETURN
CAST(SUBSTRING(@str, @pos, CHARINDEX(@delim, @str + @delim, @pos) - @pos) AS INT)
END
Code:
CREATE FUNCTION dbo.fn_GetProfileElement
(
@fieldName AS NVARCHAR(100),
@fields AS NVARCHAR(4000),
@values AS NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
BEGIN
-- If input is invalid, return null.
IF @fieldName IS NULL
OR LEN(@fieldName) = 0
OR @fields IS NULL
OR LEN(@fields) = 0
OR @values IS NULL
OR LEN(@values) = 0
RETURN NULL
-- locate FieldName in Fields
DECLARE @fieldNameToken AS NVARCHAR(20)
DECLARE @fieldNameStart AS INTEGER,
@valueStart AS INTEGER,
@valueLength AS INTEGER
-- Only handle string type fields (:S:)
SET @fieldNameStart = CHARINDEX(@fieldName + ':S',@Fields,0)
-- If field is not found, return null
IF @fieldNameStart = 0 RETURN NULL
SET @fieldNameStart = @fieldNameStart + LEN(@fieldName) + 3
-- Get the field token which I've defined as the start of the
-- field offset to the end of the length
SET @fieldNameToken = SUBSTRING(@Fields,@fieldNameStart,LEN(@Fields)-@fieldNameStart)
-- Get the values for the offset and length
SET @valueStart = dbo.fn_getelement(1,@fieldNameToken,':')
SET @valueLength = dbo.fn_getelement(2,@fieldNameToken,':')
-- Check for sane values, 0 length means the profile item was
-- stored, just no data
IF @valueLength = 0 RETURN ''
-- Return the string
RETURN SUBSTRING(@values, @valueStart+1, @valueLength)
END
According to Gary’s properties example use the query below, you can also save it as a view ready for execution.
Code:
SELECT dbo.aspnet_Profile.UserId, dbo.aspnet_Users.UserName, dbo.fn_GetProfileElement(N'FirstName', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS FirstName, dbo.fn_GetProfileElement(N'LastName', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS LastName, dbo.fn_GetProfileElement(N'Gender', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS Gender, dbo.fn_GetProfileElement(N'BirthDate', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS BirthDate, dbo.fn_GetProfileElement(N'Occupation', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS Occupation, dbo.fn_GetProfileElement(N'Website', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS Website, dbo.fn_GetProfileElement(N'Forum.Posts', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS ForumPosts, dbo.fn_GetProfileElement(N'Forum.AvatarUrl', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS ForumAvatarUrl, dbo.fn_GetProfileElement(N'Forum.Signature', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS ForumSignature, dbo.fn_GetProfileElement(N'Address.PostalCode', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS AddressPostalCode, dbo.fn_GetProfileElement(N'Address.City', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS AddressCity, dbo.fn_GetProfileElement(N'Address.State', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS AddressState, dbo.fn_GetProfileElement(N'Address.Country', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS AddressCountry, dbo.fn_GetProfileElement(N'Contacts.Phone', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS ContactsPhone, dbo.fn_GetProfileElement(N'Contacts.Fax', dbo.aspnet_Profile.PropertyNames,
dbo.aspnet_Profile.PropertyValuesString) AS ContactsFax
FROM dbo.aspnet_Profile INNER JOIN
dbo.aspnet_Users ON dbo.aspnet_Profile.UserId = dbo.aspnet_Users.UserId AND dbo.aspnet_Profile.UserId = dbo.aspnet_Users.UserId
Gary thank you, thank you, thank you, thank you..!!!!!! You are newbies’ heaven..! Yours tutorial as this one, as about membership database, restricted menu.., sql injections have helped me so much..! thank you man, you are a really great guy…