2014年7月13日 星期日

[RESOLVED] [C# net4] The parameter 'addresses' cannot be an empty string.


Hi there, hope in your help.


Why this code response with this error?


Can you help me?


The parameter 'addresses' cannot be an empty string.
Parameter name: addresses

code behind


            OdbcCommand objCmdSelect = new OdbcCommand(SQL, myConnectionString);
myConnectionString.Open();
OdbcDataReader reader = objCmdSelect.ExecuteReader();

if (reader.HasRows)
{
while (reader.Read())
{
SendEmail();
}
}
else
{
Response.Redirect("http://www.google.com/");
}
reader.Close();
reader.Dispose();
reader = null;



protected void SendEmail()
{
SmtpClient smtpClient = new SmtpClient("XXXXX");

System.Net.Mail.MailMessage mailMessagePlainText = new System.Net.Mail.MailMessage();
mailMessagePlainText.IsBodyHtml = true;
mailMessagePlainText.From = new MailAddress("myemail@gmail.com", "");

string[] toAddressArray;
toAddressArray = strGroup_emails.ToString().Split(new char[] { ';' });
foreach (string a in toAddressArray)
{
mailMessagePlainText.To.Add(a);
}

mailMessagePlainText.Bcc.Add(new MailAddress("myemail@gmail.com", ""));

mailMessagePlainText.Subject = "mail";

mailMessagePlainText.Body = "....";

try
{
smtpClient.Send(mailMessagePlainText);
}
catch (Exception ex)
{
throw (ex);
}
}



What does your sql query look like? Do you perhaps have an addresses parameter there that you are not passing anything to?





msmk



What does your sql query look like? Do you perhaps have an addresses parameter there that you are not passing anything to?





many thanks for help.


the problem occurs when the:


reader["group_emails"]

is null.


but is not sufficient the control of reader?


            if (reader.HasRows)
{
while (reader.Read())
{
....
}
}









I don't think you need both if (reader.HasRows and while (reader.Read()) because Read() will check if data is present so you are essentially checking for data twice. While (reader.Read()) is really enough.


Are you debugging your code? As I have seen in
this
 post, be careful about debugging a dataReader. If you drill down into the returned data, you end up reading the rows, which the datareader can only do once.



with this change now the code working regulary ... I don't understand ...


OdbcCommand objCmdSelect = new OdbcCommand(SQL, myConnectionString);
myConnectionString.Open();
OdbcDataReader reader = objCmdSelect.ExecuteReader();

if (reader.HasRows)
{
while (reader.Read())
{
if (reader["group_emails"] != DBNull.Value)
{
SendEmail();
}
}
}
else
{
Response.Redirect("http://www.google.com/");
}
reader.Close();
reader.Dispose();
reader = null;







Check you db - is group_emails really null?


Try changing this:


if (reader.HasRows)
{
while (reader.Read())
{
if (reader["group_emails"] != DBNull.Value)
{
SendEmail();
}
}
}

to this:


                while (reader.Read())
{
SendEmail();
}

and see if that helps



Try replacing this,


FROM:
mailMessagePlainText.From = new MailAddress("myemail@gmail.com", "");

TO:
mailMessagePlainText.From = new MailAddress("myemail@gmail.com");


ALSO:

FROM:
mailMessagePlainText.Bcc.Add(new MailAddress("myemail@gmail.com", ""));

TO:
mailMessagePlainText.Bcc.Add(new MailAddress("myemail@gmail.com"));

The MailAddress method has one/two (address, displayName)/three parameters. The display name can not be a NULL or an empty. So, set some value to the displayName parameter otherwise go with passing just one parameter.
http://msdn.microsoft.com/en-us/library/1s17zfkf.aspx



Where strGroup_ emails value from? Looks like it's has empty value.

沒有留言:

張貼留言