Sorry mates for bothering you once more.
The code below is part of the help I got here. I am just trying to extend it but it is not working.
This app sends multiple emails at once. User can check a box to send one email or s/he can check several / all boxes and send batch email to multiple recipients.
This works perfectly well.
The issue is that before an email is sent, there is a field on the database called sentFlag.
By default, the value is 'No'.
However, after an email is sent, this sentFlag needs to be updated to 'Yes', all after the Send Email button is clicked.
We would like the update to occur after the Send Email button is clicked. The email gets sent and the record gets updated all at once.
I have been trying all day to get this to work but no update occurs after the Send Email button is clicked.
Could you guys please help out one more time?
<img class="img-responsive" style="margin: auto;width:100%;" src="../images/Header.png" alt="" width="1177" height="168" usemap="#Map"/>
<br /><br />
<div style="margin-left: 300px;">
<asp:Label ID="lblTotal" runat="server" Style="font-size: 14px; font-weight: 600; color: firebrick;" Text="Label"></asp:Label><br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="3"
OnPageIndexChanging="OnPageIndexChanging" DataKeyNames="mailID">
<Columns>
<asp:TemplateField HeaderText="Select All">
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="mailID" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="FullName" HeaderText="Name" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="EmpEmail">
<ItemTemplate>
<asp:HyperLink ID="lnkEmail" runat="server" Text='<%# Eval("EmpEmail") %>' NavigateUrl='<%# Eval("EmpEmail", "mailto:{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$("[id*=GridView1] [id*=chkAll]").on("click", function () {
//Get the reference of Header CheckBox.
var chkAll = $(this);
//Loop through all GridView CheckBoxes except Header CheckBox.
$("[id*=GridView1] [id*=chkSelect]").not("[id*=chkAll]").each(function () {
$(this)[0].checked = chkAll[0].checked;
});
});
</script>
<script type="text/javascript">
$("[id*=GridView1] [id*=chkSelect]").on("click", function () {
//Get the reference of Header CheckBox.
var chkAll = $("[id*=GridView1] [id*=chkAll]");
//Set Header CheckBox checked to true.
chkAll[0].checked = true;
//Loop through all GridView CheckBoxes except Header CheckBox.
$("[id*=GridView1] [id*=chkSelect]").not("[id*=chkAll]").each(function () {
if (!$(this).is(":checked")) {
chkAll[0].checked = false;
return;
}
});
});
</script>
<br />
<asp:Button Text="Send Email Reminder" runat="server" OnClick="SendReminderEmail" /><br />
</div>
Partial Class VB
Inherits System.Web.UI.Page
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim checkboxArray As ArrayList
If ViewState("CheckBoxArray") Is Nothing Then
checkboxArray = New ArrayList()
Else
checkboxArray = CType(ViewState("CheckBoxArray"), ArrayList)
End If
If Me.IsPostBack Then
Dim checkBoxIndex As Integer
Dim checkAllWasChecked As Boolean = False
Dim chkAll As CheckBox = CType(GridView1.HeaderRow.Cells(0).FindControl("chkAll"), CheckBox)
Dim checkAllIndex As String = "chkAll-" & GridView1.PageIndex
If chkAll.Checked Then
If checkboxArray.IndexOf(checkAllIndex) = -1 Then
checkboxArray.Add(checkAllIndex)
End If
Else
If checkboxArray.IndexOf(checkAllIndex) <> -1 Then
checkboxArray.Remove(checkAllIndex)
checkAllWasChecked = True
End If
End If
For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
Dim chk As CheckBox = CType(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)
checkBoxIndex = GridView1.PageSize * GridView1.PageIndex + (i + 1)
If chk.Checked Then
If checkboxArray.IndexOf(checkBoxIndex) = -1 AndAlso Not checkAllWasChecked Then
checkboxArray.Add(checkBoxIndex)
End If
Else
If checkboxArray.IndexOf(checkBoxIndex) <> -1 OrElse checkAllWasChecked Then
checkboxArray.Remove(checkBoxIndex)
End If
End If
End If
Next
End If
ViewState("CheckBoxArray") = checkboxArray
GridView1.DataSource = LoadData()
GridView1.DataBind()
lblTotal.Text = LoadData().Rows.Count.ToString() & " members have yet to complete the form"
End Sub
Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
If ViewState("CheckBoxArray") IsNot Nothing Then
Dim CheckBoxArray As ArrayList = CType(ViewState("CheckBoxArray"), ArrayList)
Dim checkAllIndex As String = "chkAll-" & GridView1.PageIndex
If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
Dim chkAll As CheckBox = CType(GridView1.HeaderRow.Cells(0).FindControl("chkAll"), CheckBox)
chkAll.Checked = True
End If
For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
Dim chk As CheckBox = CType(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)
chk.Checked = True
Else
Dim CheckBoxIndex As Integer = GridView1.PageSize * (GridView1.PageIndex) + (i + 1)
If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 Then
Dim chk As CheckBox = CType(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)
chk.Checked = True
End If
End If
End If
Next
End If
End Sub
Protected Function LoadData() As DataTable
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("mailID", GetType(Integer)),
New DataColumn("FullName", GetType(String)),
New DataColumn("EmpEmail", GetType(String))})
' Replace the connection string, query, and field names with your actual database details
' Dim connectionString As String = "strConnString"
Dim query As String = "SELECT mailID, FullName, EmpEmail FROM EmailNotifications where sent='No'"
Using sqlConnection As New SqlConnection(constring)
Using sqlCommand As New SqlCommand(query, sqlConnection)
sqlConnection.Open()
Dim reader As SqlDataReader = sqlCommand.ExecuteReader()
While reader.Read()
' Assuming your database fields are named "Id", "Name", and "Email"
Dim mailId As Integer = Convert.ToInt32(reader("mailID"))
Dim fullName As String = Convert.ToString(reader("FullName"))
Dim email As String = Convert.ToString(reader("EmpEmail"))
dt.Rows.Add(mailId, fullName, email)
End While
End Using
End Using
Return dt
End Function
Protected Sub SendReminderEmail(ByVal sender As Object, ByVal e As EventArgs)
Dim dtCustomers As DataTable = New DataTable()
dtCustomers.Columns.AddRange(New DataColumn(1) {New DataColumn("fullName", GetType(String)), New DataColumn("email", GetType(String))})
GridView1.AllowPaging = False
GridView1.DataBind()
Dim checkboxArray As ArrayList = CType(ViewState("CheckBoxArray"), ArrayList)
Dim index As Integer = 1
End If
index += 1
Next
GridView1.AllowPaging = True
GridView1.DataBind()
Dim subject As String = "Completion Reminder"
Dim body As String = "Hello {0},<br /><br />Please remember to complete the form before deadline.<br /><br />Thanks."
Parallel.ForEach(dtCustomers.AsEnumerable(),
Function(row)
Return SendEmail(row("email").ToString(), subject, String.Format(body, row("FullName")))
End Function)
'Update record here
For Each gRow As GridViewRow In GridView1.Rows
Dim ckBox As CheckBox = CType(gRow.FindControl("chkSelect"), CheckBox)
If ckBox.Checked Then
Dim PK As Integer = CInt(GridView1.DataKeys(gRow.RowIndex)("mailID"))
MyUpdate(PK)
End If
Next
LoadData()
End Sub
Private Function SendEmail(recipient As String, subject As String, body As String) As Boolean
Dim mm As New MailMessage()
mm.[To].Add(recipient)
Dim EmailSender As New MailAddress("jdoe@yahoo.com")
mm.From = EmailSender
mm.Subject = subject
mm.Body = body
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
'Configure an SmtpClient to send the mail.
Dim client As New SmtpClient("myhostname")
client.EnableSsl = False
client.Send(mm)
Return True
End Function
Private Sub MyUpdate(ByVal PK As Integer)
Dim strSQL As String = "UPDATE EmailNotifications SET sent='Yes' WHERE sent='No' and mailID = @ID"
Using conn As SqlConnection = New SqlConnection(constring)
Using cmdSQL As SqlCommand = New SqlCommand(strSQL, conn)
conn.Open()
cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = PK
cmdSQL.ExecuteNonQuery()
End Using
End Using
End Sub
End Class
As always, thank you very much.