发送URL时,对象引用未设置为对象的实例

时间:2012-06-29 13:30:57

标签: c# asp.net

我的网络表单中有密码恢复控件。 输入所有必要的特定信息后,将向用户的电子邮件帐户发送一封电子邮件。

就像 - 如果用户忘记了他/她的密码,将密码更改链接发送到用户的电子邮件帐户而不是新密码。

但是,在单击“提交”按钮后,“对象引用未设置为对象的实例”。错误出现。

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. And the error occurred on line 66. 

Source Error: 

Line 64:         System.Web.UI.WebControls.Login Login1 =   (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");   
Line 65:         MembershipUser pwRecover = Membership.GetUser(Login1.UserName);
**Line 66:         Guid userInfoId2 = (Guid)pwRecover.ProviderUserKey;**

以下是邮件正文中发送网址的后面代码:

 protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{

    System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");   
    MembershipUser pwRecover = Membership.GetUser(Login1.UserName);
    Guid userInfoId2 = (Guid)pwRecover.ProviderUserKey;


    //Create an url that will link to a UserProfile.aspx and
    //accept a query string that is the user's id

    //setup the base of the url
    string domainName = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;

    //setup the second half of the url
    string confirmationPage = "/UserProfile.aspx?ID=" + userInfoId2.ToString();

    //combine to make the final url
    string url = domainName + confirmationPage;

    // Replace <%VerifyUrl%> placeholder with url value
    e.Message.Body = e.Message.Body.Replace("<%ResetPassword%>", url);
}

1 个答案:

答案 0 :(得分:1)

问题是pwRecover从未设置过。问题就在于:

MembershipUser pwRecover = Membership.GetUser(Login1.UserName);

Membership.GetUser未找到Login1.UserName;

的用户

尝试在此行上设置断点,以找出Login1.UserName实际上是什么。然后在数据库中查找该用户名。