获取“对象引用未设置为对象的实例”错误.vb

时间:2012-01-08 08:50:22

标签: asp.net vb.net visual-studio visual-studio-2010

我没有说明为什么我收到此错误,代码正在运行并且运行得更早。我试图通过按钮更新用户的电子邮件。它工作正常但现在由于某种原因它不断给我错误: “你调用的对象是空的。 第34行:EmailTextBox.Text = u.Email“

现在已经抓了几个小时,任何人都知道它可能是什么?

我的aspx.vb代码是:

Public Class _Default
    Inherits System.Web.UI.Page
    Dim u As MembershipUser

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 u = Membership.GetUser(User.Identity.Name)
    If Not IsPostBack Then
        EmailTextBox.Text = u.Email
    End If
End Sub


Public Sub UpdateEmailButton_OnClick(ByVal sender As Object, ByVal args As EventArgs)
    Try
        u.Email = EmailTextBox.Text
    Membership.UpdateUser(u)

    Msg.Text = "User e-mail updated."
Catch e As System.Configuration.Provider.ProviderException

    Msg.Text = e.Message
End Try
End Sub

aspx代码:

 <h3>Update E-Mail Address for <%=User.Identity.Name%></h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  <table cellpadding="3" border="0">
    <tr>
      <td>E-mail Address:</td>
      <td><asp:TextBox id="EmailTextBox" MaxLength="128" Columns="30" runat="server" /></td>
      <td><asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
                                    ControlToValidate="EmailTextBox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="UpdateEmailButton" 
                      Text="Update E-mail" 
                      OnClick="UpdateEmailButton_OnClick" 
                      runat="server" /></td>
    </tr>
  </table>

1 个答案:

答案 0 :(得分:0)

您需要进入调试模型并查看 u = Membership.GetUser(User.Identity.Name)是否为null。

我猜Membership.GetUser(User.Identity.Name)返回null。可能是你记录的超时,可能只是MembershipUser发生错误。