Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

I'm getting an error on this line below :-

email = myDataReader.GetValue(i).ToString();

What I'm trying to do is just retrieve multiple email addresses from MySQL and send email.

Based on the example that I have found, it stores in the arraylist, but in my case it gives me error.

I have look on the Google but still not able to correct the error.

Could some one help me out please ? Thanks.

C#
protected void searchEmail ()
    {
         MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=;database=obsystem");
         con.Open();

         MySqlCommand cmd = new MySqlCommand("SELECT cusEmail from monthlytracker AND MONTH(paymentDate)='" + mm + "' AND YEAR(paymentDate)='" + year + "'AND status='" + Unpaid + "'",con);

         MySqlDataReader myDataReader = cmd.ExecuteReader();

         ArrayList list_emails = new ArrayList();

         int i = 0, email = 0;

         while (myDataReader.Read())
            {
                
                email = myDataReader.GetValue(i).ToString();

                list_emails.Add(email); //Add email to a arraylist

                i = i + 1 - 1; //increment or ++i

            }

            con.Close(); //Close connection

 

            foreach (string email_to in list_emails)

            {

                MailMessage mail = new MailMessage();

                mail.To.Add(email_to);

                mail.Subject = "Welcome to C#";

                mail.From = new MailAddress("");

                mail.Body = "Test";

                SmtpClient smtp = new SmtpClient("SMTP Server");

                smtp.Send(mail);

            }
    }
Posted

Without the error message, I'm pretty much reduced to guessing, but the chances are that you need to authentication to your SmtpClient: Have a look at this Sending an Email in C# with or without attachments: generic routine.[^] which I know works! :laugh:
 
Share this answer
 
My guess would be that the problem is that you declared email as an integer:
C#
int i = 0, email = 0;
and are trying to assign a string to it:
C#
email = myDataReader.GetValue(i).ToString();
This does not work and should be the cause of the error message.

You do not need the declaration and assignment of the email variable, just use:
C#
list_emails.Add( myDataReader.GetValue(i).ToString() );


Also I would like to note that the statement i = i + 1 - 1; has no effect and it is the same as i = i;, so this entire line could be omitted.
As the reader only has one column you are retrieving there is also no need to declare and use the i variable, so the statement to add the e-mail to the list becomes:
C#
list_emails.Add( myDataReader.GetValue( 0 ).ToString() );
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900