Hello Team,
I’ve pirated some code from this post and modified it to do Sales Order Ack’s (thanks @bmanners)
The code compiles fine and it works only when I run it on our Epicor Server via Remote Desktop
The code is meant to Generate the SOA, and then Print to PDF with a specific name and save it to a location on the server… and then launch a new email in Outlook with the PDF attached
When I run it on a Users computer, I start getting errors (below) - Can anybody give some clues please??? I believe it is related to security settings but I don’t know where to start with them
The Code:
private void bntSendSOA_Click(object sender, System.EventArgs args)
{
// ** Place Event Handling Code Here **
prepareSOAPDF();
}
private void prepareSOAPDF()
{
EpiDataView edvAutoAttachOrderHed = (EpiDataView)(oTrans.EpiDataViews["AutoAttachOrderHed"]);
EpiDataView edvOrderHed1 = ((EpiDataView)(this.oTrans.EpiDataViews["OrderHed"]));
DataRow editRow = edvOrderHed1.CurrentDataRow;
String soNum = editRow["OrderNum"].ToString();
int isoNum = Convert.ToInt32(editRow["OrderNum"]);
salesOrderAckAdapter soa = new salesOrderAckAdapter(oTrans);
soa.BOConnect();
soa.GetNewParameters();
soa.ReportData.SalesOrderAckParam[0].OrderNum = isoNum;
soa.ReportData.SalesOrderAckParam[0].AgentID = "SystemTaskAgent";
soa.ReportData.SalesOrderAckParam[0].ReportStyleNum = 1001;
Guid workstationid = Guid.NewGuid();
soa.ReportData.SalesOrderAckParam[0].WorkstationID = String.Format("{0}",workstationid);
soa.ReportData.SalesOrderAckParam[0].ArchiveCode = 1;
soa.ReportData.SalesOrderAckParam[0].AutoAction = "SSRSGenerate"; //generate
soa.SubmitToAgent("SystemTaskAgent",0,0);
//MessageBox.Show("Print Job Has been submitted.");
Ice.Proxy.BO.ReportMonitorImpl RM = WCFServiceSupport.CreateImpl<Ice.Proxy.BO.ReportMonitorImpl>((Ice.Core.Session)oTrans.Session, Epicor.ServiceModel.Channels.ImplBase<Ice.Contracts.ReportMonitorSvcContract>.UriPath);
int timer=0;
bool morepages;
SysRptLstListDataSet RptList;
RptList = RM.GetList(@"WorkStationID ='"+workstationid+"'", 0, 0, out morepages);
while (RptList.SysRptLstList.Count == 0 ) // setup a loop to look for when the report has been generated.
{
System.Threading.Thread.Sleep(1000);
timer = timer + 1;
if (timer > 120)
{
MessageBox.Show("Attempts to generate a SOA pdf has timed-out. Please try again, and if that does not work, contact ERP Support");
return;
}
RptList = RM.GetList(@"WorkStationID ='"+workstationid+"'", 0, 0, out morepages);
}
string tableguid;
tableguid = RptList.SysRptLstList[0].FileName.Replace("REPORT DATABASE: ","");
// make a pdf of the PO from the report
// string FileName = String.Format(@"\\SERVER\EpicorData\SOAReports\{0}.pdf", soNum);
string FileName = String.Format(@"//SERVER/EpicorData/SOAReports/{0}.pdf", soNum);
GenerateReportPDF(tableguid,FileName);
try
{
//MessageBox.Show("Attaching to email...");
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector MsgInspector = oMsg.GetInspector;
string Signature = oMsg.HTMLBody; //capture signature for appending to custom message later.
oMsg.Subject = "TWG Sales Order Acknowledgement " + soNum ;
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("test@test.com.au");
oRecip.Resolve();
oMsg.Attachments.Add(FileName,Outlook.OlAttachmentType.olByValue, Type.Missing,Type.Missing);
var FolderId = Guid.NewGuid().ToString("N").ToUpper();
var TempPath = Path.Combine(Path.GetTempPath(), FolderId);
if (!Directory.Exists(TempPath))
{
Directory.CreateDirectory(TempPath);
}
foreach (DataRowView row in edvAutoAttachOrderHed.dataView)
{
var AttachPath = row["FileName"].ToString();
var AttachFileName = Path.GetFileName(AttachPath);
var AttachNewPath = Path.Combine(TempPath, AttachFileName);
var AttachDesc = row["DrawDesc"].ToString();
File.Copy(AttachPath, AttachNewPath);
if (File.Exists(AttachNewPath))
{
oMsg.Attachments.Add(AttachNewPath, Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
}
}
MsgInspector.Activate(); // shows the new email message as topmost window.
}
catch (Exception Ex)
{
MessageBox.Show("SOA Email was attempted but failed. Please try again. If that doesn't work, please contact ERP Support");
}
}
private void GenerateReportPDF(String tableguid, String PDFfilename)
{
// Render arguments
byte[] result = null;
string report = "/reports/CustomReports/SalesOrderAcknowledgement/SOForm_LIVE";
string format = "PDF";
// setup report variables
string deviceInfo = null;
string extension;// = String.Empty;
string mimeType;// = String.Empty;
string encoding;// = String.Empty;
Warning[] warnings = null;
string[] streamIDs = null;
string historyId = null;
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = "TableGuid";
parameters[0].Value = tableguid;
// Create a Report Execution object
Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.ReportExecutionService rsExec = new ReportExecutionService();
rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
rsExec.Url = "http://SERVER/ReportServer/ReportExecution2005.asmx"; //ServerURL;
MessageBox.Show("Url "); ///<<< This message always shows
// Load the report
rsExec.LoadReport(report, historyId);
MessageBox.Show("LoadReport"); ///<<< This message sometimes shows
// pass parameters
rsExec.SetExecutionParameters(parameters, null);
MessageBox.Show("SetExecutionParameters"); ///<<< This message never shows
// get pdf of report
result = rsExec.Render(format, deviceInfo, out extension, out mimeType, out encoding, out warnings, out streamIDs);
MessageBox.Show("result"); ///<<< This message never shows
File.WriteAllBytes(PDFfilename, result);
MessageBox.Show("WriteAllBytes"); ///<<< This message never shows
}
When I run it on a users computer it gets partway through the execution and then throws the following errors
Application Error
Exception caught in: System.Web.Services
Error Detail
============
Message: The underlying connection was closed: An unexpected error occurred on a receive.
Inner Exception Message: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
Program: System.Web.Services.dll
Method: GetWebResponse
Client Stack Trace
==================
at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.ReportExecutionService.LoadReport(String Report, String HistoryID)
at Script.GenerateReportPDF(String tableguid, String PDFfilename)
at Script.prepareSOAPDF()
at Script.bntSendSOA_Click(Object sender, EventArgs args)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButtonBase.OnClick(EventArgs e)
at Ice.Lib.Framework.EpiButton.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButton.OnMouseUp(MouseEventArgs e)
at Ice.Lib.Framework.EpiButton.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Inner Exception
===============
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
Inner Exception
===============
An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
And sometimes this error
Application Error
Exception caught in: System.Web.Services
Error Detail
============
Message: The request was aborted: The request was canceled.
Program: System.Web.Services.dll
Method: GetWebResponse
Client Stack Trace
==================
at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.ReportExecutionService.SetExecutionParameters(ParameterValue[] Parameters, String ParameterLanguage)
at Script.GenerateReportPDF(String tableguid, String PDFfilename)
at Script.prepareSOAPDF()
at Script.bntSendSOA_Click(Object sender, EventArgs args)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButtonBase.OnClick(EventArgs e)
at Ice.Lib.Framework.EpiButton.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButton.OnMouseUp(MouseEventArgs e)
at Ice.Lib.Framework.EpiButton.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)