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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble looping data in email...only getting 1 result 2

Status
Not open for further replies.

citizenzen

Programmer
Jun 28, 2007
102
US
I have tried everything and I can't get the data to loop in my email. I am only getting one record, where I should get several per record.

I took out the variables in the while loop and declared those. i tried

===================CODE=========================
protected void RejTapeBut_Click(object sender, EventArgs e)
//begin mailer class for rejection
MailAddress From = new MailAddress("Email Address here", "Display Name");
MailAddress To = new MailAddress("email address here", "User's name");
MailMessage rejectMail = new MailMessage(From, To);
rejectMail.Subject = "Rejected Tape Request";
//rejectMail.Body = txtBody.Text.Trim();

//Begin rejection Email

string rejEmailStr = ConfigurationManager.ConnectionStrings["tapelibraryconn"].ConnectionString;

SqlConnection rejConn = new SqlConnection(rejEmailStr);
try
{
rejConn.Open();
SqlCommand rejTapeComm = new SqlCommand("select * from view_showTapeReqsDetails WHERE TapeReqID=" + TapeRequestsGrid.SelectedValue, rejConn);

SqlDataReader rejReader = rejTapeComm.ExecuteReader();

//variables in email

string User;
int myRequest;
string Email;
string Number ;
string Show ;
string Barcode ;

do
//while (rejReader.Read())
while (rejReader.Read())
{
User = (string)rejReader["User"];
myRequest = (int)rejReader["TapeReqID"];
Email = (string)rejReader["UserEmail"];
Number = (string)rejReader["Number"];
Show = (string)rejReader["ShowTitle"];
Barcode = (string)rejReader["Barcode"];

rejectMail.Body = "Rejected Request #:" + myRequest + "<br>" + Show + "<br>Barcode:" + Barcode + "<br>Number:" + Number;
}
while (rejReader.NextResult());

rejectMail.IsBodyHtml = true;

SmtpClient rejectClient = new SmtpClient("IP #");
rejectClient.Send(rejectMail);

TapeReqsLbl.Text = "Email Sent Successfully";

rejReader.Close();

}
catch (SqlException ex)
{
TapeReqsLbl.Text= "Database Error Occured while sending: <br>" + ex.ToString();
}
catch (Exception ex)
{
TapeReqsLbl.Text = "General Error Occured while sending: <br>" + ex.ToString();
}
finally
{
rejConn.Close();
}

}
=============================================
 
This code looks like it should work, except you need rejectMail.Body to be += to append to what's already there.

while (rejReader.Read())
{
User = (string)rejReader["User"];
myRequest = (int)rejReader["TapeReqID"];
Email = (string)rejReader["UserEmail"];
Number = (string)rejReader["Number"];
Show = (string)rejReader["ShowTitle"];
Barcode = (string)rejReader["Barcode"];

rejectMail.Body += "Rejected Request #:" + myRequest + "<br>" + Show + "<br>Barcode:" + Barcode + "<br>Number:" + Number;
}
 
YOu are getting the last record in your dataset?

I'd guess its because you are not sending the email after you have buit it, but overwriting the data.

Either do as suggested above (in which case you will get the last user, email etc, and a chained set of barcode / numbers)

or if you need to send the emails to different people (I asusme you do as you are storing an email address in every record) then send the email in the loop..

K
 
WOW! Thanks so much. My problem was the +=. I put that in my code and it worked fine.

THANK YOU BOTH for your help!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top