We need a way to export a CSV when a user marks a Customer Shipment Entry Header as “Ready to Invoice”. This file then needs to be deposited on our network at a specified location.
Currently, I have a Standard Data Directive under ShipHead that creates a StringBuilder and exports the data as an email. However, when I go to create a file and ship it to our location it bugs out and gives the following error:
Current Code:
// Create a list to hold the data
List<Tuple<String, int, int, String, decimal>> dataList = new List<Tuple<String, int, int, String, decimal>>();
// Loop through the returns and add each row of data to the list
foreach (var Line_iterator in (from e in Db.ShipDtl where e.Company == Company_c && e.PackNum == PackNum_C select e).ToList())
{
var JobNum_C = Line_iterator.JobNum;
var LineNum_c = Line_iterator.PackLine;
var Pick_c = 2;
var MaterialName_C = Line_iterator.PartNum;
var Quality_C = Line_iterator.OurInventoryShipQty;
Tuple<String, int, int, String, decimal> data = Tuple.Create(JobNum_C, LineNum_c, Pick_c, MaterialName_C, Quality_C);
dataList.Add(data);
}
StringBuilder sb = new StringBuilder();
foreach (Tuple<string, int, int, string, decimal> row in dataList)
{
for (int i = 0; i < dataList.Count; i++)
{
sb.Append(dataList[i]);
// Add a tab character between columns, except for the last column
if (i < dataList.Count - 1)
{
sb.Append("\t");
}
}
// Add a new line character to move to the next row
sb.AppendLine();
}
// Print the output to the console
//Console.WriteLine(sb.ToString());
//Email test
var email = "Reciever email";
var sender = "sender email";
var emailMessage = sb.ToString();
var body = emailMessage;
var mailer = this.GetMailer(async: true);
var messages = new Ice.Mail.SmtpMail();
messages.SetFrom(sender);
messages.SetTo(email);
messages.Subject = "TEST";
/*messages.SetCC();*/
/*messages.SetBcc(); */
messages.SetBody(body);
mailer.Send(messages);
//File creation
DateTime thisDay = DateTime.Today;
//sb is a StringBuilder object that contains the CSV data
string csvFileName = "myCsvFile.csv";
string csvFilePath = @"NetworkLocation"+csvFileName;
// Create a StreamWriter object to write to the CSV file
using (StreamWriter writer = new StreamWriter(csvFilePath))
{
// Write the contents of the StringBuilder object to the StreamWriter object
writer.Write(sb.ToString());
// Flush and close the StreamWriter object
writer.Flush();
writer.Close();
}