示例#1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (AttCnt == 0 && lvAttachedFiles.Items.Count == 0)
            {
                Close();
                return;
            }


            int Id = 0;

            if (glAttTable == AttachmentsTableName.Audit_Attachments)
            {
                Id = AuditId;
            }
            else if (glAttTable == AttachmentsTableName.FIDetail_Attachments)
            {
                Id = DetailId;
            }
            if (UpdateAuditOnAttSave(Id, glAttTable) == false)
            {
                MessageBox.Show("Error: No files attached!");
                return;
            }

            if (lvAttachedFiles.Items.Count > 0)
            {
                List <ListViewItem> newLvItems = new List <ListViewItem>();

                foreach (ListViewItem lvi in lvAttachedFiles.Items)
                {
                    if (lvi.SubItems.Count == 1) //only filename into lv -> from db
                    {
                        LvFileInfo lvfi = saveAttachmentLocally(Id, RevNo, lvi.SubItems[0].Text, glAttTable);

                        newLvItems.Add(new ListViewItem(new string[] { lvfi.FileName, lvfi.FilePath }));
                    }
                    else //path and filename into lv -> from local dir : ok
                    {
                        newLvItems.Add(lvi);
                    }
                }

                //update old records
                //UpdateAttachments_IsCurrent(Id, RevNo);
                RevNo += 1;
                //insert attachments into db - IsCurrent = 1
                foreach (ListViewItem lvi in newLvItems)
                {
                    byte[] attFileBytes = File.ReadAllBytes(lvi.SubItems[1].Text);

                    if (!InertIntoTable_AttachedFiles(Id, RevNo, lvi.SubItems[0].Text, attFileBytes, glAttTable))
                    {
                        MessageBox.Show("File save failed: " + lvi.SubItems[0].Text);
                    }
                }
            }
            //else
            //{
            //update old records
            //UpdateAttachments_IsCurrent(Id, RevNo);
            //}

            success = true;

            AttCnt = lvAttachedFiles.Items.Count;
            if (AttCnt > 0)
            {
                MessageBox.Show("File(s) attached successfully!");
            }

            Close();
        }
示例#2
0
        //private bool UpdateAttachments_IsCurrent(int auditId, int revNo)
        //{
        //    bool ret = false;

        //    SqlConnection sqlConn = new SqlConnection(SqlDBInfo.connectionString);
        //    string InsSt = "UPDATE [dbo].[Attachments] SET [IsCurrent] = 0 WHERE AuditId = @id AND RevNo = @RevNo";
        //    try
        //    {
        //        sqlConn.Open();

        //        SqlCommand cmd = new SqlCommand(InsSt, sqlConn);

        //        cmd.Parameters.AddWithValue("@id", auditId);
        //        cmd.Parameters.AddWithValue("@RevNo", revNo);

        //        cmd.CommandType = CommandType.Text;
        //        cmd.ExecuteNonQuery();

        //        ret = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("The following error occurred: " + ex.Message);
        //    }
        //    sqlConn.Close();

        //    return ret;
        //}

        LvFileInfo saveAttachmentLocally(int Id, int RevNo, string Filename, AttachmentsTableName attTable)
        {
            LvFileInfo ret      = new LvFileInfo();
            string     tempPath = Path.GetTempPath(); //C:\Users\hkylidis\AppData\Local\Temp\

            try
            {
                if (!Directory.Exists(tempPath))
                {
                    MessageBox.Show("Error. Please check your privileges on " + tempPath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred: " + ex.Message);
                return(ret);
            }

            SqlConnection sqlConn  = new SqlConnection(SqlDBInfo.connectionString);
            string        SelectSt = "";

            if (attTable == AttachmentsTableName.Audit_Attachments)
            {
                SelectSt = "SELECT [Name], [FileContents] FROM [dbo].[Audit_Attachments] WHERE AuditId = @Id and RevNo = @RevNo and Name = @Filename ";
            }
            else if (attTable == AttachmentsTableName.FIDetail_Attachments)
            {
                SelectSt = "SELECT [Name], [FileContents] FROM [dbo].[FIDetail_Attachments] WHERE FIDetailId = @Id and RevNo = @RevNo and Name = @Filename ";
            }
            SqlCommand cmd = new SqlCommand(SelectSt, sqlConn);

            try
            {
                sqlConn.Open();
                cmd.Parameters.AddWithValue("@Id", Id);
                cmd.Parameters.AddWithValue("@RevNo", RevNo);
                cmd.Parameters.AddWithValue("@Filename", Filename);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    string realFileName = reader["Name"].ToString().Trim();
                    //string tempFile = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + "~" + realFileName);
                    //temp file -> attachment name with temp name and tilda 'tmp123~ΦΕΚ123.pdf'
                    string tempFile = Path.Combine(tempPath, realFileName);
                    try
                    {
                        File.WriteAllBytes(tempFile, (byte[])reader["FileContents"]);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("An error occured while saving temporarilly the attached file: '" + realFileName +
                                        "'\r\n\r\n\r\nDetails:\r\n" + ex.Message);
                        try
                        {
                            tempFile = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + "~" + realFileName);
                            File.WriteAllBytes(tempFile, (byte[])reader["FileContents"]);

                            MessageBox.Show("Caution! File will be saved as: " + tempFile);
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show("Caution! File " + realFileName + " will not be saved!\r\n" + ex2.Message);
                        }
                    }

                    ret = new LvFileInfo {
                        FileName = realFileName, FilePath = tempFile
                    };
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred: " + ex.Message);
                return(ret);
            }

            return(ret);
        }