I am sending an url to an email by buton click. When the email user is clicking the url he want to face login cardinals. I need to do the job without login cardinals.
Public Function SendMail(ByVal RuequestByEmpId As String, ByVal RuequestToEmpName As String, ByVal RuequestToEmpEmail As String) As Integer
Dim credential As New System.Net.NetworkCredential("joycseuiu#gmail.com", "something")
Dim msg As New System.Net.Mail.MailMessage()
Dim msgSubject As String = String.Empty
Dim strHeading As String = String.Empty
Dim EmployeeID As String = String.Empty
Dim Name As String = String.Empty
Dim Designation As String = String.Empty
Dim Email As String = String.Empty
Dim Department As String = String.Empty
Dim LeaveTYpe As String = String.Empty
Dim Reason As String = String.Empty
Dim LeaveAppID As String = String.Empty
Dim LeaveTo As String = String.Empty
Dim LeaveFrom As String = String.Empty
Dim Status As String = String.Empty
Dim i As Integer = 0
dsEmployeeInfoWithLeaveManagement.SelectParameters("EmployeeID").DefaultValue = RuequestByEmpId
dsEmployeeInfoWithLeaveManagement.DataBind()
dataview = dsEmployeeInfoWithLeaveManagement.Select(System.Web.UI.DataSourceSelectArguments.Empty)
Try
EmployeeID = dataview.Item(0).Row.Item("EmployeeID").ToString()
Name = dataview.Item(0).Row.Item("Name").ToString()
Designation = dataview.Item(0).Row.Item("Designation").ToString()
Department = dataview.Item(0).Row.Item("Department").ToString()
Email = dataview.Item(0).Row.Item("Email").ToString()
LeaveTYpe = dataview.Item(0).Row.Item("LeaveTYpe").ToString()
Reason = dataview.Item(0).Row.Item("Reason").ToString()
LeaveAppID = dataview.Item(0).Row.Item("LeaveAppID").ToString()
LeaveTo = dataview.Item(0).Row.Item("LeaveTo").ToString()
LeaveFrom = dataview.Item(0).Row.Item("LeaveFrom").ToString()
Status = dataview.Item(0).Row.Item("Status").ToString()
Catch ex As Exception
End Try
Dim SenderEmpName As String = "Joy Acharya"
Dim SenderEmpEmail As String = "joycseuiu#gmail.com"
msg.From = New System.Net.Mail.MailAddress(Email, Name)
msg.[To].Add(New System.Net.Mail.MailAddress(RuequestToEmpEmail, RuequestToEmpName))
Dim appSettings = ConfigurationManager.AppSettings
Dim result As String = appSettings("mailSender")
Dim strBody As New StringBuilder()
strBody.Append("<div>" & vbCr & vbLf & "<table>" & vbCr & vbLf & "<tr>" & vbCr & vbLf & "<td>")
msg.Subject = "Leave Application Approve"
strBody.Append("A request for leave has been submitted for your approval.")
strBody.Append("</td>" & vbCr & vbLf & "</tr>" & vbCr & vbLf & "<tr>" & vbCr & vbLf & " <td>" & vbCr & vbLf & "<span>Request By:</span>")
strBody.Append(Name)
strBody.Append("</td>" & vbCr & vbLf & "</tr>" & vbCr & vbLf & "<tr>" & vbCr & vbLf & " <td>" & vbCr & vbLf & "<span>ID:</span>")
strBody.Append(EmployeeID)
strBody.Append("</td>" & vbCr & vbLf & "</tr>" & vbCr & vbLf & "<tr>" & vbCr & vbLf & " <td>" & vbCr & vbLf & "<span>Designation:</span>")
strBody.Append(Designation)
strBody.Append("</td>" & vbCr & vbLf & "</tr>" & vbCr & vbLf & "<tr>" & vbCr & vbLf & " <td>" & vbCr & vbLf & "<span>Department:</span>")
strBody.Append(Department)
strBody.Append("</td>" & vbCr & vbLf & "</tr>" & vbCr & vbLf & "<tr>" & vbCr & vbLf & " <td>" & vbCr & vbLf & "<span>Leave Type:</span>")
strBody.Append(LeaveTYpe)
strBody.Append("</td>" & vbCr & vbLf & "</tr>" & vbCr & vbLf & "<tr>" & vbCr & vbLf & " <td>" & vbCr & vbLf & "<span>Purpose:</span>")
strBody.Append(Reason)
strBody.Append("</td>" & vbCr & vbLf & "</tr>" & vbCr & vbLf & "<tr>" & vbCr & vbLf & " <td>" & vbCr & vbLf & "<span>Leave To:</span>")
strBody.Append(LeaveTo)
strBody.Append("</td>" & vbCr & vbLf & "</tr>" & vbCr & vbLf & "<tr>" & vbCr & vbLf & " <td>" & vbCr & vbLf & "<span>Leave From:</span>")
strBody.Append(LeaveFrom)
strBody.Append("</td></tr>")
strBody.Append("</table>")
strBody.Append("<br />")
strBody.Append("<table>")
strBody.Append("<tr>")
strBody.Append("<td>")
strBody.Append(" " + "<b>Approve</b>" + "")
strBody.Append("</td>")
strBody.Append("<td>")
strBody.Append(" " + "<b>Reject</b>" + "")
strBody.Append("</td>")
strBody.Append("</tr>")
strBody.Append("<table>")
strBody.Append("</div>" & vbCr & vbLf & " <div>" & vbCr & vbLf & " Thank you" & vbCr & vbLf & " <br />")
strBody.Append("=======================================================================================")
strBody.Append("</div>")
msg.Body = strBody.ToString()
msg.IsBodyHtml = True
msg.Priority = System.Net.Mail.MailPriority.High
msg.Priority = System.Net.Mail.MailPriority.High
Dim c As New SmtpClient()
'----------------------------------
'Create the SMTP Client
c.Host = "smtp.gmail.com"
c.Credentials = credential
c.Port = 587
c.EnableSsl = True
Try
c.Send(msg)
Catch ex As Exception
End Try
Return i
End Function
When the email user clicking the Approve link, he has to face the
login cardinals.
I want that, email user will just click the link and he/she will be enable
to redirect the page.
How could i do that.
And thanks in advance.
Set RsItem = Conn.Execute("EXEC E_UpdateDevBehaviourSmalls #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID")& " ,#cCompID=" & cCompetenceid & " ,#reason=" &reason & " ,#comptype=" & comptype &",#GID=" & GID & " ,#Behaviour='" & MakeSendable(Behaviour) & "' ,#Deadline='" & deadlinedatetime & "' ,#DevBehaviour='" & MakeSendable(DevBehaviour) & "' ,#Why='" & MakeSendable(Why) & "' ,#ExtraNote='" & MakeSendable(ExtraNote) & "'")
if GID = 0 then
if not RsItem.eof then
GID = RsItem.fields(0).value
if reason = 0 then
'add dummy devbehaviour detail
Set RsItem =Conn.Execute("EXEC E_UpdateDevBehaviourDetail #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & GID & " ,#DID=0 ,#TextField1='dummy' ,#educ= 0 ,#TextField2='dummy' ,#TextField3='dummy' ,#TextField4='dummy'")
end if
end if
end if
When I try to execute the code above (full code below) I got the following error:
(It gets stuck at the following part: if not RsItem.fields(0).value)
ADODB.Recordset error '800a0e78'
Operation is not allowed when the object is closed.
Can anyone help me with this error?
<%
Dim DID
Dim GID
Dim cCompetenceid
Dim Behaviour
Dim Deadline
Dim DevBehaviour
Dim Why
Dim ExtraNote
MakeConn
Session("OnlinePageID") = 106
InsertLogItem "S:12"
If Session("EUserType")=1 Then
If UCase(Request("Action"))="SAVEDETAIL" Then
If not Request("DID")="" Then
DID = Request("DID")
Else
DID = 0
End If
Conn.Execute("EXEC E_UpdateDevBehaviourDetail #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & Request("GID") & " ,#DID=" & DID & " ,#TextField1='" & MakeSendable(Request("TextField1")) & "' ,#TextField2='" & MakeSendable(Request("TextField2")) & "' ,#TextField3='" & MakeSendable(Request("TextField3")) & "' ,#TextField4='" & MakeSendable(Request("TextField4")) & "'")
Conn.Execute("EXEC E_SignIDP #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#SignStatus=0")
End If
End If
'response.write Session("EUserType") & "<br>"
If Session("EUserType")=1 Then
Select Case UCase(Request("Action"))
Case "EDIT"
If not Request("GID")="" Then
'response.write "EXEC E_GetDevBehavior #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & Request("GID") & "<br>"
Set RsItem = Conn.Execute("EXEC E_GetDevBehavior #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & Request("GID"))
If not RsItem.EOF Then
Behaviour = Replace (RsItem("Behaviour"),"''","'")
Deadline = RsItem("Deadline")
DevBehaviour = Replace (RsItem("DevBehaviour"),"''","'")
Why = Replace (RsItem("Why"),"''","'")
ExtraNote = Replace (RsItem("ExtraNote"),"''","'")
Else
Response.End
End If
'RsItem.close
'Set RsItem = nothing
Else
End If
Case "SAVE"
If not Request("GID")="" Then
Set RsItem = Conn.Execute("EXEC E_GetDevBehavior #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & Request("GID"))
If not RsItem.EOF Then
Behaviour = Replace (RsItem("Behaviour"),"''","'")
Deadline = RsItem("Deadline")
DevBehaviour = Replace (RsItem("DevBehaviour"),"''","'")
Why = Replace (RsItem("Why"),"''","'")
ExtraNote = Replace (RsItem("ExtraNote"),"''","'")
Else
Response.End
End If
'RsItem.close
'Set RsItem = nothing
end if
dag = Day(Now())
maand = Month(Now())
jaar = Year(Now())
uur = Hour(Time)
minuten = Minute(Time)
seconden = Second(Time)
if len(dag)< 2 then dag ="0" & dag
if len(maand) < 2 then maand ="0" & maand
if len(uur) < 2 then uur ="0" & uur
if len(minuten) < 2 then minuten ="0" & minuten
if len(seconden) < 2 then seconden ="0" & seconden
datum= jaar & "-" & maand & "-" & dag
tijd = uur & ":" & minuten& ":" & seconden
datumtijd = datum & " " & tijd
Conn.Execute("EXEC E_UpdatePOPStartDate #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID"))
Conn.Execute("EXEC E_UpdateStartDate #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#StartDate='" & datumtijd & "'")
If not Request("GID")="" Then
GID = Request("GID")
newcomp = false
Else
GID = 0
Deadline = CDate(FormatDate("31/12/"&year(now)))
newcomp = true
End If
'response.write Request("cCompetenceid") &"<br>"
if not Request("cCompetenceid") = "" then
cCompetenceid = Request("cCompetenceid")
else
cCompetenceid = 0
end if
if not Request("reason") = "" then
reason = Request("reason")
else
reason = 0
end if
if not Request("comptype") = "" then
comptype = Request("comptype")
else
comptype = 1
end if
Select Case (Request("COMPID"))
Case 1460
Behaviour = Request("Behaviour")
Case 1461
Deadline = Request("DeadlineDay") & "-" & Request("DeadlineMonth") & "-" & Request("DeadlineYear")
Deadline = CDate(FormatDate(Deadline))
Case 1462
DevBehaviour = Request("DevBehaviour")
Case 1463
Why = Request("Why")
Case 1464
ExtraNote = Request("ExtraNote")
End Select
deadlinedate = CDate(Deadline)
deadlineyear = year(deadlinedate)
deadlinemonth = month(deadlinedate)
deadlineday = day(deadlinedate)
deadlinedatetime = deadlineyear & "-" & deadlinemonth & "-" & deadlineday & " 00:00:00"
'response.write "EXEC E_UpdateDevBehaviourSmalls #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID")& " ,#cCompID=" & cCompetenceid & " ,#reason=" &reason & " ,#comptype=" & comptype &",#GID=" & GID & " ,#Behaviour='" & MakeSendable(Behaviour) & "' ,#Deadline='" & deadlinedatetime & "' ,#DevBehaviour='" & MakeSendable(DevBehaviour) & "' ,#Why='" & MakeSendable(Why) & "' ,#ExtraNote='" & MakeSendable(ExtraNote) & "'"
'response.end
Set RsItem = Conn.Execute("EXEC E_UpdateDevBehaviourSmalls #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID")& " ,#cCompID=" & cCompetenceid & " ,#reason=" &reason & " ,#comptype=" & comptype &",#GID=" & GID & " ,#Behaviour='" & MakeSendable(Behaviour) & "' ,#Deadline='" & deadlinedatetime & "' ,#DevBehaviour='" & MakeSendable(DevBehaviour) & "' ,#Why='" & MakeSendable(Why) & "' ,#ExtraNote='" & MakeSendable(ExtraNote) & "'")
if GID = 0 then
if not RsItem.eof then
GID = RsItem.fields(0).value
if reason = 0 then
' add dummy devbehaviour detail
Set RsItem =Conn.Execute("EXEC E_UpdateDevBehaviourDetail #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & GID & " ,#DID=0 ,#TextField1='dummy' ,#educ= 0 ,#TextField2='dummy' ,#TextField3='dummy' ,#TextField4='dummy'")
end if
end if
end if
'RsItem.close
'Set RsItem = nothing
Conn.Execute("EXEC E_SignIDP #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#SignStatus=0")
if newcomp = true then
tempstr = "../popoverview.asp"
ClientScript("parent.location.href = '../bottomframe.asp?GID=" & GID & "&" & SetID &"&ViewID=4'" )
else
response.write " in"
tempstr = "compoverview.asp?Action=Edit&GID="&GID
response.redirect tempstr
' ClientScript("location.href =" & tempstr)
end if
End Select
End If
conn.close
set conn= nothing
%>
Stored procedure:
USE [Q]
GO
/****** Object: StoredProcedure [dbo].[E_UpdateDevBehaviourSmalls] Script Date: 17/10/2013 15:05:53 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[E_UpdateDevBehaviourSmalls]
(#ClientID int,
#UserID int,
#cCompID int,
#reason int,
#comptype int,
#GID int,
#Behaviour varchar(250),
#Deadline datetime,
#DevBehaviour text,
#Why text,
#ExtraNote text)
AS
If (#GID = 0)
BEGIN
INSERT INTO DevBehaviour(ClientID,UserID,Behaviour,Deadline,DevBehaviour,Why,ExtraNote,cCompId,reason,comptype)
VALUES(#ClientID,#UserID,#Behaviour,#Deadline,#DevBehaviour,#Why,#ExtraNote, #cCompID,#reason,#comptype)
SELECT ##identity
END
Else
BEGIN
UPDATE DevBehaviour
SET Behaviour=#Behaviour, Deadline=#Deadline, DevBehaviour=#DevBehaviour, Why=#Why, ExtraNote=#ExtraNote, cCompId = #cCompID, reason = #reason, comptype = #comptype
WHERE (ClientID = #ClientID) AND (UserID = #UserID) AND (GID = #GID)
END
I was able to reproduce your problem. Please try the stored proc below (using nocount)
USE [Q]
GO
/****** Object: StoredProcedure [dbo].[E_UpdateDevBehaviourSmalls] Script Date: 17/10/2013 15:05:53 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[E_UpdateDevBehaviourSmalls]
(#ClientID int,
#UserID int,
#cCompID int,
#reason int,
#comptype int,
#GID int,
#Behaviour varchar(250),
#Deadline datetime,
#DevBehaviour text,
#Why text,
#ExtraNote text)
AS
set nocount on
If (#GID = 0)
BEGIN
INSERT INTO DevBehaviour(ClientID,UserID,Behaviour,Deadline,DevBehaviour,Why,ExtraNote,cCompId,reason,comptype)
VALUES(#ClientID,#UserID,#Behaviour,#Deadline,#DevBehaviour,#Why,#ExtraNote, #cCompID,#reason,#comptype)
SELECT ##identity
END
Else
BEGIN
UPDATE DevBehaviour
SET Behaviour=#Behaviour, Deadline=#Deadline, DevBehaviour=#DevBehaviour, Why=#Why, ExtraNote=#ExtraNote, cCompId = #cCompID, reason = #reason, comptype = #comptype
WHERE (ClientID = #ClientID) AND (UserID = #UserID) AND (GID = #GID)
END
set nocount off
I'm using this with my server to send mails. Which is working perfectly.I want to try it out on my localhost app that I made.
Set myMail=CreateObject("CDO.Message")
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/bodyformat") = 0 ' 0 - html, 1 - text
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/mailformat") = 0 ' 0 - mime, 1 - text
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "206.183.108.132"
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
myMail.Configuration.Fields.Update
myMail.Subject = "Your New Password for Leave App"
myMail.From = rs("email")
myMail.To = "somename#domain.com"
msgg = msgg & "Dear" & " " & session("Username") & vbcrlf & vbcrlf
msgg = msgg & "This is your new password" & vbcrlf & vbcrlf
msgg = msgg & "YOUR CHANGED PASSWORD" & vbcrlf
msgg = msgg & "- - - - - - - - - - - - - - - - - - - - - - - - - - -" & vbcrlf
msgg = msgg & "User/Login Name :" & session("Username") & vbcrlf
msgg = msgg & "Password :" & request.Form("new_pass2") & vbcrlf
msgg = msgg & "- - - - - - - - - - - - - - - - - - - - - - - - - - -" & vbcrlf & vbcrlf
msgg = msgg & "Please sign in to your account using the user name and password above." & vbcrlf & vbcrlf
msgg = msgg & "Thanks" & vbcrlf
myMail.TextBody = msgg
myMail.Send
set myMail = nothing
Check if your local Machine can reach the SMTP server. It might be blocked in some firewall or router. This website can perform a simple check for you: http://www.canyouseeme.org/
Of course, you might want to check it in the code as well, or via Telnet: http://www.simplescripts.de/smtp-check-port-25-telnet-command.htm
Here is the contents of an include file I created for sending emails:
[note at the top it includes another include that has a fn_dirty() for the purpose of adding things like quotes back in.] ask me if you want that function.
<!-- #INCLUDE FILE = "i_fn_dirty.asp" -->
<%
function email(s_name_from,s_address_from,s_reply_to,s_subject,s_recipients_list,s_msg,s_type_email,s_msg_error_add,s_RemoteHost)
if (s_msg_error_add<>"") then s_msg_error_add = "<hr>" & vbCrLf & s_msg_error_add
if (s_RemoteHost="default") then s_RemoteHost = application("s_mail_server")
'recipients_list = "Scott,scott#changed.net;Andy,andy#changed.net" etc
array_recipients = split(s_recipients_list,";",-1,1)
'so recipients array now looks like this:
'array_recipients(0) = "Scott,scott#changed.net"
'array_recipients(1) = "Andy,andy#changed.net"
'-- Create the Mailer Object
Set Mailer = Server.CreateObject("SoftArtisans.SMTPMail")
'-- Set the Mail Properties
Mailer.RemoteHost = s_RemoteHost
Mailer.FromName = s_name_from
Mailer.FromAddress = s_address_from
if (s_reply_to<>"") then Mailer.ReplyTo = s_reply_to
Mailer.Subject = s_subject
a = ""
For Each Item in array_recipients
array_data = split(Item,",",-1,1)
s_name = array_data(0)
s_email_addr = array_data(1)
if (s_name<>"" and s_email_addr<>"") then
Mailer.AddRecipient s_name, s_email_addr
a = a & "name: " & s_name & ", email: " & s_email_addr & " | "
end if
Next
if (s_type_email = "text") then
Mailer.BodyText = s_msg
else
s_msg_html = replace(s_msg,vbCrLf,"<br>",1,-1,1)
Mailer.HTMLText = s_msg_html
end if
'-- Fire off the email message
if (Mailer.SendMail) then
'yay it worked
Set Mailer = Nothing
else
'try one more time
if (Mailer.SendMail) then
'yay it worked
Set Mailer = Nothing
else
msg = "<br>Error in i_fn_email.asp: " & Mailer.Response & "<br>"
msg = msg & "s_name_from = " & s_name_from & "<br>"
msg = msg & "s_address_from = " & s_address_from & "<br>"
msg = msg & "s_subject = " & s_subject & "<br>"
msg = msg & "recips list = " & a & "<br>"
msg = msg & s_msg_error_add
session("msg") = msg
Set Mailer = Nothing
response.redirect ("error_report.asp")
end if
end if
end function
%>
i have three columns column(0),column(1),column(2)and i want tag for these three columns in following code for this line: "strNewContents = strNewContents & "" & columns(0) & "" & columns(1) & "" & columns(2) & "" & vbcrlf:"
actual code:
Dim strFileName1
Dim objFSO, objTextFile
Dim intLineNumber, strNewContents, strReadLineText,strLineNumbers
dim data, columns
strFileName1 = "saveimagename.txt"
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(Server.MapPath(strFileName1))
intLineNumber = 0
strLineNumbers = ""
data = split(objTextFile.readall(), vbcrlf)
for intLineNumber = 0 to ubound(data)
columns = split(data(intLineNumber), ",", 3)
if (ubound(columns) = 2) then
''//strNewContents = "<td class='red'>" & columns(0) & "</td><td class='blue'>" & columns(2) & "</td>"
strNewContents = strNewContents & "<tr><td>" & columns(0) & "</td><td>" & columns(1) & "</td><td>" & columns(2) & "</td></tr>" & vbcrlf
end if
next
You are already using Response.Write with strings.
You simply:
Response.Write "" & linkName & ""
My Gridview won't page or sort. By this I mean that the data in the GridView doesn't change when I try to sort by a column or when I try to page through the GridView.
I have it inside an UpdatePanel on a user control (.ascx). The code below works fine when I try it outside of the user control.
Here's my code:
<asp:GridView runat="server" ID="grdStats" AutoGenerateColumns="false" Width="100%" AllowSorting="true"
AllowPaging="true" PageSize="20" PagerSettings-Mode="NumericFirstLast" PagerSettings-Position="TopAndBottom">
<Columns>
<asp:BoundField DataField="campaignname" HeaderText="Campaign Name" SortExpression="campaignname" />
<asp:BoundField DataField="site_name" HeaderText="Outlet" SortExpression="site_name" />
<asp:BoundField DataField="filename" HeaderText="Media" SortExpression="filename" />
<asp:BoundField DataField="playinterval" HeaderText="Play Interval" SortExpression="playinterval" />
</Columns>
</asp:GridView>
Here's the backend:
Protected Sub grdStats_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdStats.PageIndexChanging
grdStats.PageIndex = e.NewPageIndex
GetCampaignData()
End Sub
Protected Sub grdStats_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdStats.Sorting
If e.SortExpression = ViewState("sortExpr") Then
ViewState("sortExpr") = e.SortExpression & " desc"
Else
ViewState("sortExpr") = e.SortExpression
End If
GetCampaignData()
End Sub
' ######## POPULATE DATA #########
Private Sub GetCampaignData(Optional ByVal CompanyID As Integer = 0)
Dim sql As String = Nothing
' Check user access level.
If Session("userlevel") = 1 Then
' Current user is admin.
' If a company has been filtered, get it's data, else, get all campaign data.
If CompanyID <> 0 Then
sql = "SELECT" & vbCrLf & _
" a.campaignid," & vbCrLf & _
" a.companyid," & vbCrLf & _
" a.campaignname," & vbCrLf & _
" a.filename," & vbCrLf & _
" startdate," & vbCrLf & _
" enddate," & vbCrLf & _
" a.playinterval," & vbCrLf & _
" a.ActiveStatus," & vbCrLf & _
" b.*," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS c" & vbCrLf & _
" WHERE DAY(date_played) = DAY(CURRENT_DATE)" & vbCrLf & _
" AND c.campaignid = a.campaignid" & vbCrLf & _
" ) AS today," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS d" & vbCrLf & _
" WHERE date_played > CURRENT_DATE - INTERVAL 7 DAY" & vbCrLf & _
" AND d.campaignid = a.campaignid" & vbCrLf & _
" ) AS past_week," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS e" & vbCrLf & _
" WHERE e.campaignid = a.campaignid" & vbCrLf & _
" ) AS playcount" & vbCrLf & _
"FROM campaigns AS a" & vbCrLf & _
"INNER JOIN companies AS b ON a.companyid = b.companyid" & vbCrLf & _
"WHERE a.ActiveStatus = 1" & vbCrLf & _
"AND a.companyid = " & CompanyID
Else
sql = "SELECT" & vbCrLf & _
" a.campaignid," & vbCrLf & _
" a.companyid," & vbCrLf & _
" a.campaignname," & vbCrLf & _
" a.filename," & vbCrLf & _
" startdate," & vbCrLf & _
" enddate," & vbCrLf & _
" a.playinterval," & vbCrLf & _
" a.ActiveStatus," & vbCrLf & _
" b.*," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS c" & vbCrLf & _
" WHERE DAY(date_played) = DAY(CURRENT_DATE)" & vbCrLf & _
" AND c.campaignid = a.campaignid" & vbCrLf & _
" ) AS today," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS d" & vbCrLf & _
" WHERE date_played > CURRENT_DATE - INTERVAL 7 DAY" & vbCrLf & _
" AND d.campaignid = a.campaignid" & vbCrLf & _
" ) AS past_week," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS e" & vbCrLf & _
" WHERE e.campaignid = a.campaignid" & vbCrLf & _
" ) AS playcount" & vbCrLf & _
"FROM campaigns AS a" & vbCrLf & _
"INNER JOIN companies AS b ON a.companyid = b.companyid" & vbCrLf & _
"WHERE a.ActiveStatus = 1"
End If
Else
' User is not an admin.
sql = "SELECT" & vbCrLf & _
" a.campaignid," & vbCrLf & _
" a.companyid," & vbCrLf & _
" a.campaignname," & vbCrLf & _
" a.filename," & vbCrLf & _
" startdate," & vbCrLf & _
" enddate," & vbCrLf & _
" a.playinterval," & vbCrLf & _
" a.ActiveStatus," & vbCrLf & _
" b.*," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS c" & vbCrLf & _
" WHERE DAY(date_played) = DAY(CURRENT_DATE)" & vbCrLf & _
" AND c.campaignid = a.campaignid" & vbCrLf & _
" ) AS today," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS d" & vbCrLf & _
" WHERE date_played > CURRENT_DATE - INTERVAL 7 DAY" & vbCrLf & _
" AND d.campaignid = a.campaignid" & vbCrLf & _
" ) AS past_week," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS e" & vbCrLf & _
" WHERE e.campaignid = a.campaignid" & vbCrLf & _
" ) AS playcount" & vbCrLf & _
"FROM campaigns AS a" & vbCrLf & _
"INNER JOIN companies AS b ON a.companyid = b.companyid" & vbCrLf & _
"WHERE a.ActiveStatus = 1" & vbCrLf & _
"AND a.companyid = " & CInt(Session("companyid"))
End If
grdCampaigns.DataSource = get_data(sql)
grdCampaigns.DataBind()
For i As Integer = 0 To grdCampaigns.Rows.Count - 1
grdCampaigns.Rows(i).Cells(2).Text = Microsoft.VisualBasic.Left(grdCampaigns.Rows(i).Cells(2).Text, 10)
grdCampaigns.Rows(i).Cells(3).Text = Microsoft.VisualBasic.Left(grdCampaigns.Rows(i).Cells(3).Text, 10)
Next
End Sub
Public Function get_data(ByVal SQLStatement As String) As DataView
'Populates the datatable
Dim dt As Data.DataTable
dt = db.GetDataTable(SQLStatement)
Dim dv As System.Data.DataView = New System.Data.DataView(dt)
If Not ViewState("sortExpr") Is Nothing Then
dv.Sort = ViewState("sortExpr")
Else
dv = dt.DefaultView
End If
Session("dv5") = dv
Return dv
End Function
Any help would be greatly appreciated.
My apologies the code I pasted in the question, although correct for it's purpose, was not the right code relevant to this question. That said, I'd like to thank you Icarus for taking the time to look over my code.
The paging/sorting works. What I did to fix it was to replace the call to GetCampaignData() within the PageIndexChanging and Sorting event handlers to the SQL code and data binding methods required to give me the results I needed.