PDA

View Full Version : membership.getuser()provideruserkey problem


stevebramall
06-23-2007, 02:46 AM
Hi again

please could you help?

I am trying to get an aspx page to retreive the userid of the currently logged in user of my site. I have used




Partial Class MasterPage


Inherits System.Web.UI.MasterPage





Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


useridvalue.Text = Membership.GetUser().ProviderUserKey.ToString





End Sub


this is so that I can filter the objects only to show their own collections.





This works fine and displays the userID - still in test mode - i'll make it non visible for live version.





Howver, when user logs out I get an error



Object reference not set to an instance of an object.


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:





Line 8: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Line 9:
Line 10: useridvalue.Text = Membership.GetUser().ProviderUserKey.ToString
Line 11:
Line 12:


which I think is becuase the above cannot contain a null.





I have tried to establish an condition -


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


useridvalue.Text = Membership.GetUser().ProviderUserKey.ToString





' Dim userInfo As MembershipUser = Membership.GetUser(Login1.UserName)


' If userInfo Is Nothing Then


'useridvalue.Text = "no-one is logged in"


'Else


'useridvalue.Text = Membership.GetUser().ProviderName.ToString


'End If


End Sub


but this does not work becasue it always shows "no-one logged in " even why they are. It does, however, work in the sense that it does not crash the page.





Help- Please could you give me some advice on how to use the memebrship.getuser working so that the page knows the userId when logged on but would not throw an exception is no-one is in.





many thanks





As you can see from previous posts - i'm still relatively knew to programming


steve

vvsharma
06-23-2007, 07:37 AM
'Membership.GetUser().ProviderUserKey.ToString '
Also, useridvalue.Text = Membership.GetUser() (try not using the ProviderUserKey property,which may have caused the exception) should be fine as it returns the information for the current logged-on membership user .

Alternatively you can use something like :

If User.Identity.IsAuthenticated Then
useridvalue.Text = User.Identity.Name
Else
useridvalue.Text = 'no-one is logged in'
End If

Vikram

DiscountASP.NET
www.DiscountASP.NET (http://www.discountasp.net/)

stevebramall
06-24-2007, 07:12 AM
Thanks vvsharma (http://community.discountasp.net/profile.aspx?f=24&m=18264&p=3296),

Actually that worked well but I also did some research about authentication events and I now understand why your suggestion works which is better.


My next problem is an error about Guid.

Summary: How can I get the collection add to automatically send the userID from the currently logged on user?


I want the user to be able to add a photo collection but the class "PhotoCollection" must send as a paremeter the userID as well as collection ID, name, descrpition. The reason is I only want the logged on user to see their own photo collections and not every one else's.

I have a userID as a uniqueidentifier in the collection table as a FK to the asp_users table in a stadard SQL 2005 db

My Photo collection class is:-




Imports Microsoft.VisualBasic


Public Class PhotoCollection


Private _userID As Guid


Private _name As String


Private _description As String


Public Sub New(ByVal name As String, ByVal description As String, ByVal userid As Guid)


MyBase.New()


_userID = userid


_name = name


_description = description


End Sub


Public ReadOnly Property userID() As Guid


Get


Return _userID


End Get


End Property


Public ReadOnly Property name() As String


Get


Return _name


End Get


End Property


Public ReadOnly Property description() As String


Get


Return _description


End Get


End Property


End Class








In my addcollection.aspx page I want to pass the userID parameter to the db via a stored proc called add collection





ALTER PROCEDURE [dbo].[add_collection]


(


@name varchar(300),


@desc varchar(300),


@userID uniqueidentifier


)


AS


insert into collection ([name], description, userID) values (@name, @desc, @userID)





/* SET NOCOUNT ON */


RETURN


which seems OK but I'm having difficulty getting the userID variable in the addcollection.aspx to send the guid.





Currently my mark up is:-








<%@ Page Language="VB" MasterPageFile="../admin.master" %>


<%@ Import Namespace="System.Threading" %>


<%@ Import Namespace="System.Data.SqlClient" %>


<%@ Import Namespace="System.Data" %>


<%@ Import Namespace="System.Drawing" %>


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">











<script runat="server">








Protected Sub SaveCollection(ByVal sender As Object, ByVal e As System.EventArgs)





Dim userID As Guid = New System.Guid(Trim(Request.QueryString("userid")))





If txtDesc.Text <> "" And txtName.Text <> "" Then





http://community.discountasp.net/emoticons/mad.gifDim pc As New PhotoCollection(userID.Guid, txtName.Text, txtDesc.Text)


http://community.discountasp.net/emoticons/mad.gifTHIS IS WHERE THE ERROR IS BEING HIGHLIGHTED- saying Guid is not memeber os system.guid. I have tried userID.NewGuid but get "qualifiying expression will not be evaluated....


'hand off the new collection to the data access class


Dim result As Boolean = PhotoDB.InsertCollection(pc)





If result = True Then


Label1.Text = txtName.Text &amp; " has been added to the list of collections! Click <a href='addphoto.aspx'>here</a> to add pictures to this collection."


End If


End If





End Sub





</script>





<asp:TextBox ID="txtName" runat="server"></asp:TextBox>


Name[b]


[b]


<asp:TextBox ID="txtDesc" runat="server" Height="85px" Width="200px" TextMode="MultiLine"></asp:TextBox>


Description[b]


[b]


<asp:Button ID="Button1" runat="server" Text="Save" OnClick="SaveCollection" />


[b]


[b]


<asp:Label ID="Label1" runat="server"></asp:Label>


[b]


[b]


<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="../secure/admin.aspx">Back to Admin...</asp:LinkButton>








</asp:Content>


<script runat="server">


Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)





Page.Theme = AppSettings.CurrentTheme


Page.Title = AppSettings.PageTitle





End Sub


</script>








The PhotoDB class being refered to above is








Imports Microsoft.VisualBasic


Imports System.Data


Imports System.Data.SqlClient


Imports System.Drawing.Imaging


Imports System.IO


Public Class PhotoDB


Public Shared ReadOnly Property ConnectionString() As String


Get


Return ConfigurationManager.ConnectionStrings("my_sql_db").ConnectionString


End Get


End Property


Public Shared Function InsertCollection(ByVal pc As PhotoCollection) As Boolean


Try


Dim conn As New SqlConnection()


Dim cmd As New SqlCommand()


conn.ConnectionString = PhotoDB.ConnectionString


cmd.Connection = conn


conn.Open()


cmd.CommandType = CommandType.StoredProcedure


cmd.CommandText = "add_collection"


' Create a SqlParameter for each parameter in the stored procedure.


Dim userIDParam As New SqlParameter("@userID", pc.userID)


Dim nameParam As New SqlParameter("@name", pc.name)


Dim descParam As New SqlParameter("@desc", pc.description)


cmd.Parameters.Add(userIDParam)


cmd.Parameters.Add(nameParam)


cmd.Parameters.Add(descParam)


cmd.ExecuteNonQuery()


Return True


Catch ex As Exception





Throw (ex)


End Try


End Function








I AM KEEN TO LEARN BUT I AM STUCK AND ANY HELP HERE WOULD BE MOST WELCOME.





Summary: How can I get the collection add to automatically send the userID from the currently logged on user?

vvsharma
06-25-2007, 06:50 AM
Why dont you keep userID as a simple string.What makes you to have it as a GUID?
FYI :System.GUID.NewGuid().ToString() can be returned as a string value in case you want to use GUID as the unique identifier foe all the users?


Vikram

DiscountASP.NET
www.DiscountASP.NET (http://www.discountasp.net/)