开发者

DetailView error: Object reference not set to an instance of an object

开发者 https://www.devze.com 2023-02-12 10:08 出处:网络
I am trying to update a user information and get the following error: Object reference not set to an instance of an object. Description: An

I am trying to update a user information and get the following 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 20: 'parameterless constructor Line 21: Line 22:

user.Email = DirectCast(e.NewValues(0), String) Line 23: user.Comment = DirectCast(e.NewValues(1), String) Line 24: user.IsApproved = CBool(e.NewValues(2))

The .vb code is:

Protected Sub UserInfo_ItemUpdating(ByVal sender As Object, ByVal e As DetailsViewUpdateEventArgs)
        'Need to handle the update manually because MembershipUser does not have a
        'parameterless constructor  

        user.Email = DirectCast(e.NewValues(0), String)
        user.Comment = DirectCast(e.NewValues(1), String)
        user.IsApproved = CBool(e.NewValues(2))

        Try
            ' Update user info:
            Membership.UpdateUser(user)

            ' Update user roles:
            UpdateUserRoles()

            UserUpdateMessage.Text = "Update Successful."

            e.Cancel = True
            UserInfo.ChangeMode(DetailsViewMode.[ReadOnly])
        Catch ex As Exception
            UserUpdateMessage.Text = "Update Failed: " + ex.Message

            e.Cancel = True
            UserInfo.ChangeMode(DetailsViewMode.[ReadOnly])
        End Try
    End Sub

Below is the .aspx code for the detailView:

 Protected Sub UserInfo_ItemUpdating(ByVal sender As Object, ByVal e As DetailsViewUpdateEventArgs)
        'Need to handle the update manually because MembershipUser does not have a
        'parameterless constructor  

        user.Email = DirectCast(e.NewValues(0), String)
        user.Comment = DirectCast(e.NewValues(1), String)
        user.IsApproved = CBool(e.NewValues(2))

        Try
       开发者_JAVA百科     ' Update user info:
            Membership.UpdateUser(user)

            ' Update user roles:
            UpdateUserRoles()

            UserUpdateMessage.Text = "Update Successful."

            e.Cancel = True
            UserInfo.ChangeMode(DetailsViewMode.[ReadOnly])
        Catch ex As Exception
            UserUpdateMessage.Text = "Update Failed: " + ex.Message

            e.Cancel = True
            UserInfo.ChangeMode(DetailsViewMode.[ReadOnly])
        End Try
    End Sub

Any idea how i can sort this?

Any help would be greatly appreciated.

Thanks


My first guess, without seeing more code, would be that your user variable is null. Did you set it when you rendered the page? I assume your code above is a postback and so user lost it's value.

0

精彩评论

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

关注公众号