Get Document Attachments from a sales document and save them to a temp file

This article shows how to save document attachments from a Sales Document to a temp file.  

Just a sample of some of my code, I am not a trained programmer but this works.

 

 

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
 
Public Class docAttachments
    Dim userinfo As New user
    Public FileNames As New List(Of String)
    Public Sub SelectbyDocumentId(DocumentId As String)
 
        Dim reader As System.Data.IDataReader = Nothing
 
        Dim SQLString As String =
                "SELECT CO00105.fileName,BinaryBlob FROM CO00105 " &
                "inner join coAttachmentItems on coAttachmentItems.Attachment_ID=CO00105.Attachment_ID " &
                "WHERE(DOCNUMBR)='" & Trim(DocumentId) & "' and EmailAllowAttachments=1"
 
        Dim connStr As SqlConnection = New SqlConnection(userinfo.ConnectionString)
 
        Dim command As SqlCommand = New SqlCommand(SQLString, connStr)
        connStr.Open()
        reader = command.ExecuteReader()
 
        Try
            Do While reader.Read
                'populate Filenames to be used
                FileNames.Add(Trim(reader("filename")))
 
                Dim filename As String = Path.Combine(Path.GetTempPath(), RTrim(reader("fileName")))
                'save file to user's temp directory
                System.IO.File.WriteAllBytes(filename, reader("BinaryBlob"))
            Loop
        Catch ex As Exception
            Throw
        Finally
            If Not reader Is Nothing Then reader.Close()
        End Try
 
    End Sub
 
End Class

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences