G
Guest
Ok I don't know what is the problem with my code But I am trying to build a
newsletter that gathers parameters from 3 textbox controls and a Listbox
control. The form then queries the event table based on 2 of the textboxes
one for the start date of the events required and one for the last date of
the events required. The 3rd textbox works fine. It passes the text from the
textbox to the stringbuilder I built on the codebehind just fine. As well the
Listbox works as expected. The problem I am having is with the Events table
described earlier.
I set an event to iterate through the rows in the Event Table and stuff the
information into a String. This string is then appended to the StringBuilder
to add to the email message.
The Problem:
WHen the I iterate through the Event Table in the dataset it doesn't stop
reading one row when there is one row to read. It keeps reading the same row
over and over again. I can't explain it. I think it repeatadly loops
through the data for every email address table and stuffs the information
into one email.
Below is the Codebehind:
public class EventBroadcast : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table EBroadcast;
protected System.Web.UI.WebControls.LinkButton ImageAttach;
protected System.Web.UI.WebControls.ListBox IMGList;
protected System.Web.UI.WebControls.TextBox StartDate;
protected System.Web.UI.WebControls.TextBox Message;
protected System.Web.UI.WebControls.TextBox EndDate;
// Event Gathering.
protected System.Data.SqlClient.SqlConnection clubconn;
protected System.Data.SqlClient.SqlDataAdapter clubadapt;
protected System.Data.DataSet dsClub;
protected System.Text.StringBuilder MailString;
protected System.Web.Mail.MailMessage MailSite;
protected System.Data.SqlClient.SqlCommand clubcmd;
protected System.Data.SqlClient.SqlDataReader clubDR;
protected System.Data.SqlClient.SqlDataAdapter clubadapt2;
protected System.Data.DataSet dsClub2;
string strEventRelease;
public string strclubconn
{
get{return ConfigurationSettings.AppSettings["ConnectionString"];}
}
public string EMessage
{
get{return Message.Text.ToString().Replace("\n","<br>");}
}
public string stringpath
{
get{return Server.MapPath(@"..\BroadcastIMG\");}
}
private void Page_Load(object sender, System.EventArgs e)
{
ImageAttach.Attributes.Add("onClick","javascript:window.open('../Secured/EmailIMG.aspx','imgWindow','width=700, height=275');");
if(!Page.IsPostBack)
{
GetDirPath(stringpath);
}
}
public void GetDirPath(string path)
{
string[] ImgList=Directory.GetFiles(path);
foreach(string Filename in ImgList)
{
string filename=Path.GetFileName(Filename);
IMGList.Items.Add(filename);
IMGList.DataValueField=path + filename;
IMGList.SelectedIndex=-1;
}
}
protected void Button_Click(object sender, System.EventArgs e)
{
DateTime StDate=System.Convert.ToDateTime(StartDate.Text);
DateTime EnDate=System.Convert.ToDateTime(EndDate.Text);
BindData(StDate,EnDate);
string strEmailSelect="SELECT Test.* FROM Test";
clubconn=new SqlConnection(strclubconn);
clubconn.Open();
dsClub2=new DataSet();
clubadapt2=new SqlDataAdapter(strEmailSelect,clubconn);
clubadapt2.Fill(dsClub2,"Email");
clubconn.Close();
Session["MailBody"]=MailString.ToString();
foreach(DataRow dr1 in dsClub2.Tables["Email"].Rows)
{
BindData(StDate,EnDate);
MailSite=new MailMessage();
MailSite.From="(e-mail address removed)";
MailSite.To=dr1["Email"].ToString();
MailSite.Cc="(e-mail address removed)";
MailSite.Subject="News Release";
MailSite.BodyFormat=MailFormat.Html;
MailSite.Body=MailString.ToString();
SmtpMail.SmtpServer="smtp.paconline.net";
SmtpMail.Send(MailSite);
}
Response.Redirect("../Secured/MassConfirm.aspx");
}
public string EventList(System.DateTime SDate, System.DateTime EDate)
{
string strEventSelect="SELECT EVNR_tbl.* FROM EVNR_tbl, img_tbl WHERE
EVNR_tbl._Date BETWEEN '" + SDate.ToString("yyyy/MM/dd") + "' AND '" +
EDate.ToString("yyyy/MM/dd") + "' ORDER BY EVNR_tbl._Date ASC";
clubconn=new SqlConnection(strclubconn);
clubconn.Open();
dsClub=new DataSet();
clubadapt=new SqlDataAdapter(strEventSelect,clubconn);
clubadapt.FillSchema(dsClub,SchemaType.Source,"EVNR_tbl");
clubadapt.Fill(dsClub,"EVNR");
foreach(DataRow dr in dsClub.Tables["EVNR"].Rows)
{
strEventRelease += "<br><br>" + System.Convert.ToDateTime(dr["_Date"]) +
" " + dr["EventTitle"] + "<br>";
strEventRelease +=
dr["Description"].ToString().Replace("\n","<br>").Trim() + "<br>";
strEventRelease += "For details press <a
href='http://www.sonar.bc.ca/EventCalendar.aspx'> here </a>" + "<br>";
}
return strEventRelease;
}
public System.Text.StringBuilder BindData(System.DateTime SDate,
System.DateTime EDate)
{
EventList(SDate,EDate);
MailString=new System.Text.StringBuilder(99000);
// Set Email Body.
MailString.Append("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0
Transitional//EN' >");
MailString.Append("<HTML>");
MailString.Append("<HEAD>");
MailString.Append("<meta name='GENERATOR' Content='Microsoft Visual
Studio .NET 7.1'>");
MailString.Append("<meta name='CODE_LANGUAGE' Content='C#'>");
MailString.Append("<meta name='vs_defaultClientScript'
content='JavaScript'>");
MailString.Append("<meta name='vs_targetSchema'
content='http://schemas.microsoft.com/intellisense/ie5'>");
MailString.Append("</HEAD>");
MailString.Append("<body MS_POSITIONING='GridLayout' bgColor='black'>");
MailString.Append("<form id='Form1' method='post' runat='server'>");
MailString.Append("<table borderColor='#ffffff' cellSpacing='0'
cellPadding='0' width='500' align='center' border='1'>");
MailString.Append("<tr>");
MailString.Append("<td>");
MailString.Append("<table cellSpacing='0' cellPadding='0' width='100%'
border='0'>");
MailString.Append("<tr>");
MailString.Append("<td colspan='2'><IMG
SRC='http://www.sonar.bc.ca/images/Header.jpg'></td>");
MailString.Append("</tr>");
MailString.Append("<tr>");
MailString.Append("<td colspan='2' style='HEIGHT: 48px'>");
MailString.Append("<table cellpadding='10' cellspacing='0' border='0'
width='100%'>");
MailString.Append("<tr bgcolor='#000033'>");
MailString.Append("<td style='FONT-SIZE: 10px; COLOR: white; FONT-FAMILY:
Verdana'>" + EMessage + "</td>");
MailString.Append("</tr>");
MailString.Append("</table>");
MailString.Append("</td>");
MailString.Append("</tr>");
MailString.Append("<tr>");
MailString.Append("<td>");
MailString.Append("<table cellpadding='10' cellspacing='0' border='0'
width='100%'>");
MailString.Append("<tr>");
MailString.Append("<td style='FONT-SIZE: 10px; WIDTH: 251px; COLOR:
white; FONT-FAMILY: Verdana' vAlign='Top'>");
MailString.Append(strEventRelease);
MailString.Append("</td>");
MailString.Append("<td style='FONT-SIZE: 10px; COLOR: white; FONT-FAMILY:
Verdana' vAlign='top'>");
MailString.Append("<p>Promotions</p>");
foreach(ListItem i in IMGList.Items)
{
if(i.Selected)
{
MailString.Append("<img src='http://www.sonar.bc.ca/BroadcastIMG/" +
i.Text + "'" + ">" +"<br>");
MailString.Append("<br><hr noshade='true'><br>");
}
}
MailString.Append("</td>");
MailString.Append("</tr>");
MailString.Append("</table>");
MailString.Append("</td>");
MailString.Append("</tr>");
MailString.Append("</table>");
MailString.Append("</td>");
MailString.Append("</tr>");
MailString.Append("</table>");
MailString.Append("</form>");
MailString.Append("</body>");
MailString.Append("</HTML>");
return MailString;
}
}
I know this is a lot to read but, for the life of me, I can't figure out
what is wrong. Please someone help.
newsletter that gathers parameters from 3 textbox controls and a Listbox
control. The form then queries the event table based on 2 of the textboxes
one for the start date of the events required and one for the last date of
the events required. The 3rd textbox works fine. It passes the text from the
textbox to the stringbuilder I built on the codebehind just fine. As well the
Listbox works as expected. The problem I am having is with the Events table
described earlier.
I set an event to iterate through the rows in the Event Table and stuff the
information into a String. This string is then appended to the StringBuilder
to add to the email message.
The Problem:
WHen the I iterate through the Event Table in the dataset it doesn't stop
reading one row when there is one row to read. It keeps reading the same row
over and over again. I can't explain it. I think it repeatadly loops
through the data for every email address table and stuffs the information
into one email.
Below is the Codebehind:
public class EventBroadcast : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table EBroadcast;
protected System.Web.UI.WebControls.LinkButton ImageAttach;
protected System.Web.UI.WebControls.ListBox IMGList;
protected System.Web.UI.WebControls.TextBox StartDate;
protected System.Web.UI.WebControls.TextBox Message;
protected System.Web.UI.WebControls.TextBox EndDate;
// Event Gathering.
protected System.Data.SqlClient.SqlConnection clubconn;
protected System.Data.SqlClient.SqlDataAdapter clubadapt;
protected System.Data.DataSet dsClub;
protected System.Text.StringBuilder MailString;
protected System.Web.Mail.MailMessage MailSite;
protected System.Data.SqlClient.SqlCommand clubcmd;
protected System.Data.SqlClient.SqlDataReader clubDR;
protected System.Data.SqlClient.SqlDataAdapter clubadapt2;
protected System.Data.DataSet dsClub2;
string strEventRelease;
public string strclubconn
{
get{return ConfigurationSettings.AppSettings["ConnectionString"];}
}
public string EMessage
{
get{return Message.Text.ToString().Replace("\n","<br>");}
}
public string stringpath
{
get{return Server.MapPath(@"..\BroadcastIMG\");}
}
private void Page_Load(object sender, System.EventArgs e)
{
ImageAttach.Attributes.Add("onClick","javascript:window.open('../Secured/EmailIMG.aspx','imgWindow','width=700, height=275');");
if(!Page.IsPostBack)
{
GetDirPath(stringpath);
}
}
public void GetDirPath(string path)
{
string[] ImgList=Directory.GetFiles(path);
foreach(string Filename in ImgList)
{
string filename=Path.GetFileName(Filename);
IMGList.Items.Add(filename);
IMGList.DataValueField=path + filename;
IMGList.SelectedIndex=-1;
}
}
protected void Button_Click(object sender, System.EventArgs e)
{
DateTime StDate=System.Convert.ToDateTime(StartDate.Text);
DateTime EnDate=System.Convert.ToDateTime(EndDate.Text);
BindData(StDate,EnDate);
string strEmailSelect="SELECT Test.* FROM Test";
clubconn=new SqlConnection(strclubconn);
clubconn.Open();
dsClub2=new DataSet();
clubadapt2=new SqlDataAdapter(strEmailSelect,clubconn);
clubadapt2.Fill(dsClub2,"Email");
clubconn.Close();
Session["MailBody"]=MailString.ToString();
foreach(DataRow dr1 in dsClub2.Tables["Email"].Rows)
{
BindData(StDate,EnDate);
MailSite=new MailMessage();
MailSite.From="(e-mail address removed)";
MailSite.To=dr1["Email"].ToString();
MailSite.Cc="(e-mail address removed)";
MailSite.Subject="News Release";
MailSite.BodyFormat=MailFormat.Html;
MailSite.Body=MailString.ToString();
SmtpMail.SmtpServer="smtp.paconline.net";
SmtpMail.Send(MailSite);
}
Response.Redirect("../Secured/MassConfirm.aspx");
}
public string EventList(System.DateTime SDate, System.DateTime EDate)
{
string strEventSelect="SELECT EVNR_tbl.* FROM EVNR_tbl, img_tbl WHERE
EVNR_tbl._Date BETWEEN '" + SDate.ToString("yyyy/MM/dd") + "' AND '" +
EDate.ToString("yyyy/MM/dd") + "' ORDER BY EVNR_tbl._Date ASC";
clubconn=new SqlConnection(strclubconn);
clubconn.Open();
dsClub=new DataSet();
clubadapt=new SqlDataAdapter(strEventSelect,clubconn);
clubadapt.FillSchema(dsClub,SchemaType.Source,"EVNR_tbl");
clubadapt.Fill(dsClub,"EVNR");
foreach(DataRow dr in dsClub.Tables["EVNR"].Rows)
{
strEventRelease += "<br><br>" + System.Convert.ToDateTime(dr["_Date"]) +
" " + dr["EventTitle"] + "<br>";
strEventRelease +=
dr["Description"].ToString().Replace("\n","<br>").Trim() + "<br>";
strEventRelease += "For details press <a
href='http://www.sonar.bc.ca/EventCalendar.aspx'> here </a>" + "<br>";
}
return strEventRelease;
}
public System.Text.StringBuilder BindData(System.DateTime SDate,
System.DateTime EDate)
{
EventList(SDate,EDate);
MailString=new System.Text.StringBuilder(99000);
// Set Email Body.
MailString.Append("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0
Transitional//EN' >");
MailString.Append("<HTML>");
MailString.Append("<HEAD>");
MailString.Append("<meta name='GENERATOR' Content='Microsoft Visual
Studio .NET 7.1'>");
MailString.Append("<meta name='CODE_LANGUAGE' Content='C#'>");
MailString.Append("<meta name='vs_defaultClientScript'
content='JavaScript'>");
MailString.Append("<meta name='vs_targetSchema'
content='http://schemas.microsoft.com/intellisense/ie5'>");
MailString.Append("</HEAD>");
MailString.Append("<body MS_POSITIONING='GridLayout' bgColor='black'>");
MailString.Append("<form id='Form1' method='post' runat='server'>");
MailString.Append("<table borderColor='#ffffff' cellSpacing='0'
cellPadding='0' width='500' align='center' border='1'>");
MailString.Append("<tr>");
MailString.Append("<td>");
MailString.Append("<table cellSpacing='0' cellPadding='0' width='100%'
border='0'>");
MailString.Append("<tr>");
MailString.Append("<td colspan='2'><IMG
SRC='http://www.sonar.bc.ca/images/Header.jpg'></td>");
MailString.Append("</tr>");
MailString.Append("<tr>");
MailString.Append("<td colspan='2' style='HEIGHT: 48px'>");
MailString.Append("<table cellpadding='10' cellspacing='0' border='0'
width='100%'>");
MailString.Append("<tr bgcolor='#000033'>");
MailString.Append("<td style='FONT-SIZE: 10px; COLOR: white; FONT-FAMILY:
Verdana'>" + EMessage + "</td>");
MailString.Append("</tr>");
MailString.Append("</table>");
MailString.Append("</td>");
MailString.Append("</tr>");
MailString.Append("<tr>");
MailString.Append("<td>");
MailString.Append("<table cellpadding='10' cellspacing='0' border='0'
width='100%'>");
MailString.Append("<tr>");
MailString.Append("<td style='FONT-SIZE: 10px; WIDTH: 251px; COLOR:
white; FONT-FAMILY: Verdana' vAlign='Top'>");
MailString.Append(strEventRelease);
MailString.Append("</td>");
MailString.Append("<td style='FONT-SIZE: 10px; COLOR: white; FONT-FAMILY:
Verdana' vAlign='top'>");
MailString.Append("<p>Promotions</p>");
foreach(ListItem i in IMGList.Items)
{
if(i.Selected)
{
MailString.Append("<img src='http://www.sonar.bc.ca/BroadcastIMG/" +
i.Text + "'" + ">" +"<br>");
MailString.Append("<br><hr noshade='true'><br>");
}
}
MailString.Append("</td>");
MailString.Append("</tr>");
MailString.Append("</table>");
MailString.Append("</td>");
MailString.Append("</tr>");
MailString.Append("</table>");
MailString.Append("</td>");
MailString.Append("</tr>");
MailString.Append("</table>");
MailString.Append("</form>");
MailString.Append("</body>");
MailString.Append("</HTML>");
return MailString;
}
}
I know this is a lot to read but, for the life of me, I can't figure out
what is wrong. Please someone help.