I am a new ASP.NET developer and I am developing a simple registration system. I am trying now to let the system administrator being able to send reminders to the registered users before the start date of the event. All the avaliable events will be listed in a GridView with a button for sending reminders to the registered users in that event.
For your information, I have the following database design:
Employees Table: NetID, Name
Events Table: ID, Title
BookingDetails Table: BookingID, EventID, NetID
**Please note that NetID is the username of the employee
ASP.NET Code:
<asp:GridView ID="ListOfAvailableEvents_GrivView" runat="server"
AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None" AllowPaging="True">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" />
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="StartDateTime" HeaderText="Start Date & Time" SortExpression="StartDateTime" />
<asp:BoundField DataField="EndDateTime" HeaderText="End Date & Time" SortExpression="EndDateTime" />
<asp:BoundField DataField="NumberOfSeats" HeaderText="Number of Seats" SortExpression="NumberOfSeats" />
<asp:BoundField DataField="Number of Bookings" HeaderText="Number of Bookings" ReadOnly="True"
SortExpression="Number of Bookings" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="sendButton" runat="server" CssClass="button" Text="Send Reminder →"
OnClick="btnSend_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle Font-Bold="True" CssClass="complete" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PM_RegistrationSysDBConnectionString %>"
SelectCommand="SELECT Events.Title, Events.Description, Events.Location, Events.StartDateTime, Events.EndDateTime, Events.NumberOfSeats, COUNT(BookingDetails.BookingID) AS [Number of Bookings] FROM Events INNER JOIN BookingDetails ON Events.ID = BookingDetails.EventID WHERE (Events.IsActive = 1) GROUP BY Events.Title, Events.Description, Events.Location, Events.StartDateTime, Events.EndDateTime, Events.NumberOfSeats ORDER BY Events.StartDateTime DESC"></asp:SqlDataSource>
C# Code:
protected void btnSend_Click(object sender, EventArgs e)
{
GridViewRow gvrow = (GridViewRow)(((Button)sender)).NamingContainer;
HiddenField1.Value = ListOfAvailableEvents_GrivView.DataKeys[gvrow.RowIndex].Value.ToString();
string title = gvrow.Cells[0].Text;
string description = gvrow.Cells[1].Text;
string location = gvrow.Cells[2].Text;
string startDateTime = gvrow.Cells[3].Text;
string endDateTime = gvrow.Cells[4].Text;
string connString = ".............................;";
using (SqlConnection conn = new SqlConnection(connString))
{
var sbEmailAddresses = new System.Text.StringBuilder(2000);
int eventID = Convert.ToInt32(HiddenField1.Value);
//Open DB connection
conn.Open();
string cmdText = @"SELECT dbo.Events.Title, dbo.Employees.NetID
FROM dbo.BookingDetails INNER JOIN
dbo.Events ON dbo.BookingDetails.EventID = @EventID INNER JOIN
dbo.Employees ON dbo.BookingDetails.NetID = dbo.Employees.NetID";
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.Parameters.AddWithValue("@EventID", eventID);
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
var sName = reader.GetString(0);
if (!string.IsNullOrEmpty(sName))
{
if (sbEmailAddresses.Length != 0)
{
sbEmailAddresses.Append(", ");
}
sbEmailAddresses.Append(sName).Append("@appServer.com");
}
}
}
reader.Close();
var sEMailAddresses = sbEmailAddresses.ToString();
string body = "..........................";
int sendCount = 0;
List<string> addressList = new List<string>(sEMailAddresses.Split(','));
StringBuilder addressesToSend = new StringBuilder();
for (int userIndex = 0; userIndex < addressList.Count; userIndex++)
{
sendCount++;
if (addressesToSend.Length > 0)
addressesToSend.Append(",");
addressesToSend.Append(addressList[userIndex]);
if (sendCount == 10 || userIndex == addressList.Count - 1)
{
SendEmail(addressesToSend.ToString(), "", "Registration REMINDER Notification", body, true);
addressesToSend.Clear();
sendCount = 0;
}
}
cmd.ExecuteNonQuery();
}
//reset the value of hiddenfield
HiddenField1.Value = "-1";
}
}
protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
{
SmtpClient sc = new SmtpClient("MAIL ADDRESS");
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("app@appServer.com", "Registration System");
// In case the mail system doesn't like no to recipients. This could be removed
//msg.To.Add("app@appServer.com");
msg.Bcc.Add(toAddresses);
msg.Subject = MailSubject;
msg.Body = MessageBody;
msg.IsBodyHtml = isBodyHtml;
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
// something bad happened
//Response.Write("Something bad happened!");
}
}
But I could not be able to send any emails. Could you please tell me why?
UPDATE:
I wrote the code and when I run it, I did not get any error. I tried to debug it and when I did that I found the call to SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml) method is not working and I don’t know why.
So could you please tell me how I can send emails to the registered users of the selected event?
check this for loop once