无法找到导致“对象引用未设置为对象实例”的错误

时间:2013-11-22 08:17:54

标签: asp.net email nullreferenceexception

我正在尝试通过复选框发送邮件而且我已经完成了但是在这里我想要当管理员点击复选框并按下按钮然后管理员从转发器获取文档名称和状态的值然后发送邮件给用户

当管理员在任何电子邮件ID中发送邮件时,当用户收到邮件文档名称时,它会显示如下:abc status:reject

DocID  DocName  Uplaodedfile  UserEmail          DocType  DepType status
1      ABC      def.pdf       abcdef@gmail.com   pdf      hr      reject
2      hr       hrdoc.pdf     johkety@gmail.com  pdf      hr      approve

这是电子邮件按钮代码

protected void btnSendMail_Click(object sender, EventArgs e)
{
    string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
    SqlConnection mySQLconnection = new SqlConnection(connStr);
    string empId = string.Empty;
    DataTable dt = new DataTable();

    try
    {
        mySQLconnection.Open();

        for (int i = 0; i < Repeateremail.Items.Count; i++)
        {
            CheckBox checkboc = ((CheckBox)Repeateremail.Items[i].FindControl("chkSelect"));

            if (checkboc != null)
            {
                if (checkboc.Checked == true)
                {
                    //get Current EMAIL_ID from the DataKey
                    string emailId = (Label)Repeateremail.Items[i].FindControl("lbl_email")).Text;
                    string DocName = ((Label)Repeateremail.Items[i].FindControl("DocName")).Text;
                    string Status =  ((Label)Repeateremail.Items[i].FindControl("Status")).Text;

                    //write code to send mail
                    SendEmailUsingGmail(emailId,DocName,Status);
                    dt.Clear();
                    dt.Dispose();
                }
                else if (checkboc.Checked == false)
                {
                }
            }
        }               
    }
    catch (Exception ex)
    {
       emailsent.Text="Failed";
    }
    finally
    {
      empId = string.Empty;
    }
}

private void SendEmailUsingGmail(string toEmailAddress,string DocName,string Status)
{
    try
    {
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new NetworkCredential("johmm@gmail.com", "12234");
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        MailMessage message = new MailMessage();
        message.From = new MailAddress("johmm@gmail.com");
        message.To.Add(toEmailAddress);
        message.To.Add(DocName);
        message.To.Add(Status);
        message.Subject = "Write your email subject here";
        message.Body = "write the content of the email here";
        smtp.Send(message);
    }
    catch (Exception ex)
    {
        Response.Write("Error occured: " + ex.Message.ToString());
    }
}

但它显示错误

在这一行

catch (Exception ex)
{
   emailsent.Text="Failed";
}
  

`错误:对象引用未设置为对象的实例

1 个答案:

答案 0 :(得分:0)

可能是你的循环没有找到控件。使用'as'关键字转换为所需的类型。它没有抛出异常。它改为尝试它,如果不可能则返回null。尝试使用以下格式:

for (int i = 0; i < Repeateremail.Items.Count; i++)
{
    CheckBox checkbox = Repeateremail.Items[i].FindControl("chkSelect")) as CheckBox;

    if (checkbox != null) 
    {
        if (checkbox.Checked) 
        {
            Label emailLabel = Repeateremail.Items[i].FindControl("lbl_email")) as Label;
            Label docNameLabel = Repeateremail.Items[i].FindControl("DocName")) as Label;
            Label statusLabel = Repeateremail.Items[i].FindControl("Status")) as Label;
            //get Current EMAIL_ID from the DataKey
            string emailId = emailLabel.Text;
            string DocName = docNameLabel.Text;
            string Status =  statusLabel.Text;

            //write code to send mail
            SendEmailUsingGmail(emailId,DocName,Status);
            dt.Clear();
            dt.Dispose();
        }
        else
        {
        }
    }
} 

然后逐步调试并检查所有这些标签是否为空。您很快就会知道,.Text将失败,但调试器会提醒您。