0

i am trying to send mail through check boxes and i done it but here i want when admin click on check boxes and press on button then admin get the value of document name and status from repeater and then send mail to user

like when admin send mail in any email id then it show like this when user receive mail document name: 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

this is email button code

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());
    }
}

but it shows me error

in this line

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

`Error: Object reference not set to an instance of an object

user2931015
  • 199
  • 2
  • 6
  • 20
  • Can you output what is in `ex` as it will give you a line number. – Dominic Zukiewicz Nov 22 '13 at 08:19
  • Debug and see which line is throwing the error. It might be connection is missing in config – Zaki Nov 22 '13 at 08:19
  • You also don't need `dt.Clear()` or `dt.Dispose()` as you aren't using it. It could be one of the cast's as well. – Dominic Zukiewicz Nov 22 '13 at 08:21
  • Most probably error is due to one of these three lines: 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; – PM. Nov 22 '13 at 08:22
  • when i send only email then email send successfully but when i code this ((Label)Repeateremail.Items[i].FindControl("DocName")).Text; string Status = ((Label)Repeateremail.Items[i].FindControl("Status")).Text; to send also document name and status it shows me error – user2931015 Nov 22 '13 at 08:29
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Nov 22 '13 at 08:33

1 Answers1

0

It could be your for loop not finding the controls. Using the 'as' keyword to convert into the desired type. It does not throw an exception. It instead attempts it, and returns null if it is not possible. Try using this format:

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
        {
        }
    }
} 

Then step through with the debugger and check all of those labels are not null. You will know soon enough, as the .Text will fail, but the debugger should alert you to that.

Dominic Zukiewicz
  • 7,704
  • 7
  • 37
  • 57
  • i try your code when i set a breakpoint in this code string emailId = emailLabel.Text; string DocName = docNameLabel.Text; string Status = statusLabel.Text; it shows me emailId like abcdef@gmail.com but in DocName and in Status it shows me like this "" – user2931015 Nov 22 '13 at 08:43
  • in this code string emailId = emailLabel.Text; string DocName = docNameLabel.Text; string Status = statusLabel.Text; it shows me emailId like abcdef@gmail.com but in DocName and in Status it shows me like this "" – user2931015 Nov 22 '13 at 08:45
  • Ok. So that isn't the source of your problem then. – Dominic Zukiewicz Nov 22 '13 at 08:49