Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ExecuteReader, SmtpMail, Access empty cell errors

Status
Not open for further replies.

crxcte1

Programmer
May 22, 2003
74
US
The access database has 3 cells on each line and some are empty. When a empty cell is read I get an error. How could I step over an empty cell and so not to create an error?

strSQL="Select email1, email2, email3 from members where membertype=Officers";
try {
OleDbCommand cmd = new OleDbCommand(strSQL, myConn);
myConn.Open();
drMembers = cmd.ExecuteReader();
while (drMembers.Read()) {
string sTo = drMembers["email1"].ToString();
MyMail.To = sTo.Trim();
if (sTo != "" || sTo != null)
SmtpMail.Send(MyMail);//first email sent
sTo = drMembers["email2"].ToString();
MyMail.To = sTo.Trim();
if (sTo != "")
SmtpMail.Send(MyMail);//2nd email sent
sTo = drMembers["email3"].ToString();
MyMail.To = sTo.Trim();
if (sTo != "")
SmtpMail.Send(MyMail); //3rd email sent
}
Span1.InnerHtml ="<B><font color=green>Message sent</font></B>";
myConn.Close();
}
catch (Exception ex1){
Span1.InnerHtml ="<B><font color=Yellow>Message not sent</font></B>";
}
 
You need to trap for string.empty or DBNull.Value before you do any kind of assignment.ToString() from the drMembers object.
Code:
while (drMembers.Read()) {
string sTo;
if(!drMembers.Equals(DBNull.Value) && !drMembers["email1"].ToString() == string.empty)
  {
     sTo = drMembers["Email1"].ToString();
     MyMail.To = sTo.Trim();
  {
etc......
Of course you're going to want to do something else if it's null or "".
 
I tried your example and got an error as listed below;

Operator '!' cannot be applied to operand of type 'string'
Source Error:

Line 172: while (drMembers.Read()) {
Line 173: string sTo;
Line 174: if(!drMembers.Equals(DBNull.Value) && !drMembers["email1"].ToString() == string.empty){
Line 175: sTo = drMembers["email1"].ToString();
Line 176: MyMail.To = sTo.Trim();

Line: 174
 
Sorry, I'm out of it right now and shouldn't be anywhere near a computer. I tell you not to do the .ToString() thing and then I do it in my example as part of the validation. Get rid of this whole part of the if: && !drMembers["email1"].ToString() == string.empty.
Sorry again.
 
Yes, my head always hurts when I work with this code. I tried removing the .ToString which lead to another error. I wasn't sure if that was what you were referring to.

Compiler Error Message: CS0023: Operator '!' cannot be applied to operand of type 'object'

Source Error:


Line 172: while (drMembers.Read()) {
Line 173: string sTo;
Line 174: if(!drMembers.Equals(DBNull.Value) && !drMembers["email1"] == string.empty){
Line 175: sTo = drMembers["email1"].ToString();
Line 176: MyMail.To = sTo.Trim();


Source File: C:\ASPdotnet\MembersList\MailAp.aspx Line: 174
 
OK. New day. Had coffee.
Code:
string sTo;
while (drMembers.Read()) {
if(!drMembers[b]["Email1"][/b].Equals(DBNull.Value))
  {
     sTo = drMembers["Email1"].ToString();
     MyMail.To = sTo.Trim();
  {
etc......
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top