title: DetailView error: Object reference not set to an instance of an object
body: <p>I am trying to update a user information and get the following error:</p>
  
  <blockquote>
    <p>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.</p>
    
    <p>Exception Details:
    System.NullReferenceException: Object
    reference not set to an instance of an
    object.</p>
    
    <p>Source Error:</p>
    
    <p>Line 20:         'parameterless
    constructor   Line 21:  Line 22:<br>
    user.Email =
    DirectCast(e.NewValues(0), String)
    Line 23:         user.Comment =
    DirectCast(e.NewValues(1), String)
    Line 24:         user.IsApproved =
    CBool(e.NewValues(2))</p>
  </blockquote>
  
  <p>The .vb code is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Below is the .aspx code for the detailView:</p>
  
  <pre><code> 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
  </code></pre>
  
  <p>Any idea how i can sort this?</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Thanks</p>