Tuesday, December 15, 2015

SSIS package Import Items from Excel file to WMS system

We found CSR missing enter some item information sometime. Customer send us a excel file. The item information inside the file. We can get item information from excel file and then import to WMS SQL table. So I created this package to import items to our WMS.
1. Create a SSIS project in Microsoft Visual Visual Studio.
2. Name the package "InsertItems.dtsx".

3.Create the following Parameters.

4.Create Data Flow Task

5.Edit the Data Flow detail.

6.InsertItems.dtsx control flow.
The script code.

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Net.Mail;
using System.Net;
#endregion

namespace ST_fbf9ced29caf40f891b78b2e509a9bd5
{
    /// <summary>
    /// ScriptMain is the entry point class of the script.  Do not change the name, attributes,
    /// or parent of this class.
    /// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
        #region Help:  Using Integration Services variables and parameters in a script
        /* To use a variable in this script, first ensure that the variable has been added to 
         * either the list contained in the ReadOnlyVariables property or the list contained in 
         * the ReadWriteVariables property of this script task, according to whether or not your
         * code needs to write to the variable.  To add the variable, save this script, close this instance of
         * Visual Studio, and update the ReadOnlyVariables and 
         * ReadWriteVariables properties in the Script Transformation Editor window.
         * To use a parameter in this script, follow the same steps. Parameters are always read-only.
         * 
         * Example of reading from a variable:
         *  DateTime startTime = (DateTime) Dts.Variables["System::StartTime"].Value;
         * 
         * Example of writing to a variable:
         *  Dts.Variables["User::myStringVariable"].Value = "new value";
         * 
         * Example of reading from a package parameter:
         *  int batchId = (int) Dts.Variables["$Package::batchId"].Value;
         *  
         * Example of reading from a project parameter:
         *  int batchId = (int) Dts.Variables["$Project::batchId"].Value;
         * 
         * Example of reading from a sensitive project parameter:
         *  int batchId = (int) Dts.Variables["$Project::batchId"].GetSensitiveValue();
         * */

        #endregion

        #region Help:  Firing Integration Services events from a script
        /* This script task can fire events for logging purposes.
         * 
         * Example of firing an error event:
         *  Dts.Events.FireError(18, "Process Values", "Bad value", "", 0);
         * 
         * Example of firing an information event:
         *  Dts.Events.FireInformation(3, "Process Values", "Processing has started", "", 0, ref fireAgain)
         * 
         * Example of firing a warning event:
         *  Dts.Events.FireWarning(14, "Process Values", "No values received for input", "", 0);
         * */
        #endregion

        #region Help:  Using Integration Services connection managers in a script
        /* Some types of connection managers can be used in this script task.  See the topic 
         * "Working with Connection Managers Programatically" for details.
         * 
         * Example of using an ADO.Net connection manager:
         *  object rawConnection = Dts.Connections["Sales DB"].AcquireConnection(Dts.Transaction);
         *  SqlConnection myADONETConnection = (SqlConnection)rawConnection;
         *  //Use the connection in some code here, then release the connection
         *  Dts.Connections["Sales DB"].ReleaseConnection(rawConnection);
         *
         * Example of using a File connection manager
         *  object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);
         *  string filePath = (string)rawConnection;
         *  //Use the connection in some code here, then release the connection
         *  Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);
         * */
        #endregion


/// <summary>
        /// This method is called when this script task executes in the control flow.
        /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        /// To open Help, press F1.
        /// </summary>
public void Main()
{
// TODO: Add your code here
            OleDbDataAdapter AddedNewItemsDA = new OleDbDataAdapter();
            System.Data.DataTable AddedNewItemsDT = new System.Data.DataTable();
            AddedNewItemsDA.Fill(AddedNewItemsDT, Dts.Variables["User::AddedItems"].Value);
            string AddedItemsStr = "";

            OleDbDataAdapter DuplicateItemsDA = new OleDbDataAdapter();
            System.Data.DataTable DuplicateItemsDT = new System.Data.DataTable();
            DuplicateItemsDA.Fill(DuplicateItemsDT, Dts.Variables["User::DuplicateItems"].Value);
            string DuplicateItemsStr = "";

            string ToAddress = Dts.Variables["$Package::ToAddress"].Value.ToString();

            //MessageBox.Show(ToAddress);


            int AddedItemCount = AddedNewItemsDT.Rows.Count;
            int DuplicateItemCount = DuplicateItemsDT.Rows.Count;


            if (AddedItemCount != 0)
            {
                AddedItemsStr = "Added the following " + AddedItemCount.ToString() + " new item(s). \r\n";
                foreach (DataRow row in AddedNewItemsDT.Rows)
                {
                    AddedItemsStr = AddedItemsStr + row.ItemArray[0].ToString() + "\r\n";
                }
            }
            else
            {
                AddedItemsStr = "No item(s) are added! \r\n";
            }


            if (DuplicateItemCount !=0)
            {
                DuplicateItemsStr = "The following " + DuplicateItemCount.ToString() + " item(s) are duplicate. They are already in the system. \r\n";
                foreach (DataRow row in DuplicateItemsDT.Rows)
                {
                    DuplicateItemsStr = DuplicateItemsStr + row.ItemArray[0].ToString() + "\r\n";
                }
            }

            string Mailbody = AddedItemsStr + "\r\n" + "\r\n" + DuplicateItemsStr;
            //MessageBox.Show(Mailbody);

            SendMail(ToAddress, Mailbody);

Dts.TaskResult = (int)ScriptResults.Success;
            
}

        public void SendMail(string to, string mailbody)
        {
            MailAddress Mailto = new MailAddress(to);
            MailAddress Mailfrom = new MailAddress("smtpxx@dxxfxx.com");
            MailMessage mail = new MailMessage(Mailfrom, Mailto);
            mail.Subject = "NAV Adding New Items Log.";
            mail.Body = mailbody;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "Smtp.office365.com";
            smtp.Port = 587;
            smtp.Credentials = new NetworkCredential("smtpxx@dxxfxx.com", "yourpassword");
            smtp.EnableSsl = true;
            smtp.Send(mail); 
        }
        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion
        
}
}

7.Create a Master.dtsx package.


8.Master package control flow.

9.After run the package. It will send an email as log.


Wednesday, December 9, 2015

C# Windows Form Program to monitor EDI files transferring.

        Recently, users report to me that the EDI incoming and outgoing files sit on the network share folder for a long time. When this happens, we need to restart the AS2 connector service and restart the EDI tasks. But we need monitor the files and get alert first.
        So I write this C# program to monitor the files. It will check files every 15 minutes.
        1. We need to create two csv files(incoming.csv, and outgoing.csv). It includes Customer, Folder Path, Number of Files (sit on this folder), (current) Status, Last Stauts.
         2. Use Visual Studio to create a C# windows form project. Add two lables, two dataGridViews, and one button. Just like the following.
           3. Code for the from1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Threading.Tasks;

namespace MonitorDeltaFiles
{
    public partial class Form1 : Form
    {

        string mailstring = "";
        public Form1()
        {
            InitializeComponent();
        }

        //run the following code when load the form.
         private void Form1_Load(object sender, EventArgs e)
        {
            refreshform();
            System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
            timer1.Interval = 1000 * 60 * 15;
            timer1.Tick += new System.EventHandler(timer1_Tick);
            timer1.Start();
        }


        // function to refresh the from
        private void refreshform()
        {
            mailstring = "";
            int GridViewHeight = 0;
            updatestatus("incoming.csv");
            List<string[]> incomingrows = File.ReadAllLines("incoming.csv").Select(x => x.Split(',')).ToList();
            DataTable incomingdt = new DataTable();
            incomingdt.Columns.Add("Customer");
            incomingdt.Columns.Add("Folder Path");
            incomingdt.Columns.Add("Number of Files");
            incomingdt.Columns.Add("Status");
            incomingdt.Columns.Add("Last Status");
            incomingrows.ForEach(x =>
            { incomingdt.Rows.Add(x); });
            dataGridView1.DataSource = incomingdt;
            GridViewHeight = GetHeightandformatting(dataGridView1);

            lbl_outgoing.Location = new Point(13, 54 + GridViewHeight + 38);
            dataGridView2.Location = new Point(43, 54 + GridViewHeight + 38 + 23);

            updatestatus("outgoing.csv");
            List<string[]> outgoingrows = File.ReadAllLines("outgoing.csv").Select(x => x.Split(',')).ToList();
            DataTable outgoingdt = new DataTable();
            outgoingdt.Columns.Add("Customer");
            outgoingdt.Columns.Add("Folder Path");
            outgoingdt.Columns.Add("Number of Files");
            outgoingdt.Columns.Add("Status");
            outgoingdt.Columns.Add("Last Status");
            outgoingrows.ForEach(x =>
            { outgoingdt.Rows.Add(x); });
            dataGridView2.DataSource = outgoingdt;
            GridViewHeight = GetHeightandformatting(dataGridView2);

            if (mailstring.Length > 0)
            {
                mailstring = "Please check the following path(s).\r\n" + "\r\n" + mailstring;
                Task.Factory.StartNew(() => { SendMail("victor.quan@durafreight.com", mailstring); });
                //SendMail("victor.quxx@dxxxxx.com", mailstring);
               this.Activate();
            }
        }

        // function update the csv files.
        private void updatestatus(string filename)
        {
            int returnFileNum = 0 ;
            StreamReader sr = new StreamReader(filename);
            var lines = new List<string[]>();
            while (!sr.EndOfStream)
            {
                string[] Line = sr.ReadLine().Split(',');
                lines.Add(Line);
            }
            sr.Close();
            foreach (var item in lines)
            {
                returnFileNum = checkfiles(item[1]);
                item[4] = item[3];
                if (returnFileNum == 0)
                {
                    item[2] = "0";
                    item[3] = "Green";
                }
                else
                {
                    if (returnFileNum < Int32.Parse(item[2]))
                    {
                        item[2] = returnFileNum.ToString();
                        item[3] = "Orange";
                    }
                    else
                    {
                        item[2] = returnFileNum.ToString();
                        item[3] = "Red";
                    }
                }
            }

            StreamWriter newfile = new StreamWriter(filename);
            foreach (var item in lines)
            {
                newfile.WriteLine(item[0] + "," + item[1] + "," + item[2] + "," + item[3] +"," + item[4]);
            }
            lines.Clear();
            newfile.Close();
        }


        //get the files number in the folder
        private int checkfiles(string fullfilename)
        {
            string[] files = Directory.GetFiles(fullfilename, "*.*", SearchOption.TopDirectoryOnly);
            return files.Length;
        }


        //format the table, high light red when both current status and last status are red.
        private int GetHeightandformatting(DataGridView myDataGridView)
        {
            myDataGridView.AutoResizeColumns();

            int height = 0;
            foreach (DataGridViewRow row in myDataGridView.Rows)
            {
                height += row.Height;
            }
            height += myDataGridView.ColumnHeadersHeight;

            int width = 0;
            foreach (DataGridViewColumn col in myDataGridView.Columns)
            {
                width += col.Width;
            }

            myDataGridView.ClientSize = new Size(width + 3, height + 2);

            foreach (DataGridViewRow Myrow in myDataGridView.Rows)
            {
                if (Myrow.Cells[3].Value.ToString() == "Red" & Myrow.Cells[4].Value.ToString() == "Red")
                {
                    Myrow.Cells[3].Style.BackColor = Color.Red;
                    Myrow.Cells[4].Style.BackColor = Color.Red;
                    mailstring = mailstring + Myrow.Cells[1].Value.ToString() + "\r\n";
                }
            }
            return height;
        }

        private void btnrefresh_Click(object sender, EventArgs e)
        {
            refreshform();
        }

        //user timer, run it very 15 minutes.
        private void timer1_Tick(object sender, EventArgs e)
        {
            refreshform();
        }

        //send email alert when find files sit in folder more than 30 minutes.
        private void SendMail(string to, string mailbody)
        {
            MailAddress Mailto = new MailAddress(to);
            MailAddress Mailfrom = new MailAddress("smtpxx@dxxxx.xxx");
            MailMessage mail = new MailMessage(Mailfrom, Mailto);
            mail.Subject = "Please check the AS2 and Delta Server";
            mail.Body = mailbody;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "Smtp.office365.com";
            smtp.Port = 587;
            smtp.Credentials = new NetworkCredential("smtpxx@dxxxx.xxx", "Yourpassword");
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }

    }
}

4.Test.