开发者

How do I pull a GUID from my database in order to Update a Username

I have a SQL database that is different than the one in ASP.net so I had to create a custom membership Provider. The provider to get the user looks like this: My SQL class name is CustomSqlProvider:

Public Overrides Function GetUser(ByVal username As String, _
    ByVal userIsOnline As Boolean) As MembershipUser
    'Dim connectionString As String = "Server=***;Database=***;User Id=***password=****"'

    Dim conn As SqlConnection = New SqlConnection(connectionString)
    Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
    cmd.CommandType = CommandType.StoredProcedure

    cmd.Parameters.Add("@UserName", SqlDbType.NVarChar)
    cmd.Parameters.Add("@Password", SqlDbType.NVarChar)

    Dim u As MembershipUser = Nothing
    Dim reader As SqlDataReader = Nothing

    Try
        conn.Open()

        If userIsOnline Then
            Dim updateCmd As SqlCommand = New SqlCommand("Update_User", conn)

            updateCmd.Parameters.Add("@UserName", SqlDbType.NVarChar)
            updateCmd.Parameters.Add("@F_Name", SqlDbType.NVarChar)
            updateCmd.Parameters.Add("@L_Name", SqlDbType.NVarChar)
            updateCmd.Parameters.Add("@PWD", SqlDbType.VarChar)
            updateCmd.Parameters.Add("@Email", SqlDbType.VarChar)

            updateCmd.ExecuteNonQuery()
        End If

    Catch e As SqlException
        'If WriteExceptionsToEventLog Then
        '    WriteToEventLog(e, "Get_User, as String")
        '    Throw New ProviderException(exceptionMessage)
        'Else
        'Throw e
        'End If
    Finally
        If Not reader Is Nothing Then reader.Close()

        conn.Close()
    End Try

    Return u
End Function

Public Overrides Function GetUser(ByVal providerUserKey As Object, _
    ByVal userIsOnline As Boolean) As MembershipUser

    Dim conn As SqlConnection = New SqlConnection(connectionString)
    Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
    cmd.Comma开发者_高级运维ndType = CommandType.StoredProcedure

    cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = providerUserKey

    Dim u As MembershipUser = Nothing
    Dim reader As SqlDataReader = Nothing

    Try
        conn.Open()

        reader = cmd.ExecuteReader()

        If reader.HasRows Then
            reader.Read()
            u = GetUserFromReader(reader)

            If userIsOnline Then
                Dim updateCmd As SqlCommand = New SqlCommand("Update_User", conn)

                updateCmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier)


                updateCmd.ExecuteNonQuery()
            End If
        End If
    Catch e As SqlException
        If WriteExceptionsToEventLog Then
            WriteToEventLog(e, "GetUser(Object, Boolean)")

            Throw New ProviderException(exceptionMessage)
        Else
            Throw e
        End If
    Finally
        If Not reader Is Nothing Then reader.Close()

        conn.Close()
    End Try

    Return u
End Function

Now what I want to do is I first login to my website, it authenticates, and I am in. I am then redirected to another page where I can change my Username. But I can't seem to get the GUID of the logged in User in order to change it. I'm using a stored procedure that Updates the Users Table.

I have the following code on the Page where I can change credentials:

Dim currentUser as MembershipUser = Membership.GetUser() Dim CurrentUSerId as Guid = CType(currentUser.ProviderUserKey, Guid)

I get reference not set to an instance of an object. Any help would be appreciated.

Thank you for the edit. I have also added get and set properties so I can be able to add values to those parameters. The parameters in the Update_User sproc are inputs. My Get_User sproc looks like this..which is why I think it is not getting the GUID because when it is run it asks for Username and password and then it returns everything including the Guid back:

GO
/****** Object:  StoredProcedure [dbo].[Get_User]    Script Date: 07/14/2010 09:16:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Get_User]
    @UserName nvarchar(50),
    @Password varchar(50)
AS
    SELECT USER_ID, USER_NAME, F_NAME, L_NAME, BUILDING_ID, SIP_ROLE, INTERNAL_ID, PWD, EMAIL FROM dbo.USERS WHERE USER_NAME = @UserName AND PWD = @Password
    RETURN

///////////////////////////////////////////////////////////////////////////////////////////

After adding those gets and sets and assigning parameter variables to my Sproc parameters I get the following error:

Reference not set to an instance of an object.

at this line of code:

Dim currentUser As MembershipUser = Membership.GetUser()
Dim UserId As Guid = CType(currentUser.ProviderUserKey, Guid)

And this is what I have on the UserInfo.aspx.vb Page under the Button click:

Dim sql As New SqlClient.SqlCommand("Update_User", con)
        sql.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = UserId
        sql.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = txtUserName.Text


In the first GetUser OverRide where does it execute the following line:

Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)

I can't see that the command is executed, I have been known to be incredibly dumb though :)

Perhaps your code needs to follow the pattern in the Get_User by ProviderUserKey pattern, I've added some missing things and questions, hope this helps.

    Public Overrides Function GetUser(ByVal username As String, _
    ByVal userIsOnline As Boolean) As MembershipUser
    'Dim connectionString As String = "Server=***;Database=***;User Id=***password=****"'

    Dim conn As SqlConnection = New SqlConnection(connectionString)
    Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
    cmd.CommandType = CommandType.StoredProcedure

    cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = "??????"
    cmd.Parameters.Add(("@Password", SqlDbType.NVarChar).Value = "??????"

    Dim u As MembershipUser = Nothing
    Dim reader As SqlDataReader = Nothing

    Try
        conn.Open()
        reader = cmd.ExecuteReader()
        If reader.HasRows Then
            reader.Read()
            u = GetUserFromReader(reader)


            If userIsOnline Then
                Dim updateCmd As SqlCommand = New SqlCommand("Update_User", conn)
                'Are these input or output parameters?
                updateCmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = u.UserName
                updateCmd.Parameters.Add("@F_Name", SqlDbType.NVarChar).Value = "??????"
                updateCmd.Parameters.Add("@L_Name", SqlDbType.NVarChar).Value = "??????"
                updateCmd.Parameters.Add("@PWD", SqlDbType.VarChar).Value = "??????"
                updateCmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = "??????"

                updateCmd.ExecuteNonQuery()
            End If
        End If
    Catch e As SqlException
        'If WriteExceptionsToEventLog Then
        '    WriteToEventLog(e, "Get_User, as String")
        '    Throw New ProviderException(exceptionMessage)
        'Else
        'Throw e
        'End If
    Finally
        If Not reader Is Nothing Then reader.Close()

        conn.Close()
    End Try

    Return u
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜