Solved

Creating a new document in vb.net using DM 5.1.05 API results in zero length file

Posted on 2007-11-14
3
1,096 Views
Last Modified: 2010-08-05
Using the example from the API reference and modifying it for vb.net from vb6, i am creating a new document in DM.  The result is that no error is thrown, the document does get created but the version of the document in DM is 0 length.  Trapping the "TotalBytesWritten" return value also reveals that no bytes were written.  The errDescription and errNumber both are OK.

Here is my VB.Net code

        Dim DocNumber As Long
        Dim VersionID As Long
        Dim TotalFileSize As Long
        Dim TotalBytesWritten As Long
        Dim szErr As String
        Dim bdata() As Byte
        Dim objDoc As New PCDCLIENTLib.PCDDocObject
        objDoc.SetProperty("%TARGET_LIBRARY", gszLibrary)
        objDoc.SetDST(gszDst)
        'Set the Profile Form for this document.
**code that sets the profile field values purposely omitted from ee post ***    

    Dim objPutStream As New Object
        objPutStream = objPutDoc.GetPropertyValue("%CONTENT")
        Dim FileName
        FileName = "C:\Test\005.doc"
        Dim fs As FileStream = File.Open(FileName, FileMode.Open, FileAccess.Read)
        Dim r As BinaryReader = New BinaryReader(fs)
        Dim bytesToWrite As Integer

        'FileOpen(1, FileName, OpenMode.Binary, OpenAccess.Read, OpenShare.LockRead, bdata.Length)
         TotalFileSize = fs.Length
        TotalBytesWritten = 0
        Me.lblImportProgress.Text = "Uploading Image: " & FileName
        bdata = r.ReadBytes(TotalFileSize)
        objPutStream.Write(bdata, TotalBytesWritten)
 
          fs.Close()
        objPutStream = Nothing
        objDoc = Nothing
        objDoc = New PCDCLIENTLib.PCDDocObject
        objDoc.SetDST(gszDst)
        objDoc.SetObjectType(gclsTemplate.DMProfile.ToString)
        objDoc.Update()


The code example from the api reference used the OPEN and GET methods.
Here is the crux of the writing portion from the example:

Dim DocNumber As Long
Dim VersionID As Long
Dim TotalFileSize As Long
Dim TotalBytesWritten As Long
Dim Buffread As Long
'Set bdata ridiculously low for test purposes
'to increase the likelihood of corruption.
Dim bdata(16) As Byte
Dim objDoc As PCDDocObject
Set objDoc = New PCDDocObject
objDoc.SetProperty "%TARGET_LIBRARY", Library
objDoc.SetDST DST
'Set the Profile Form for this document.
***PROFILE FIELD VALUES OMITTED FOR EE POST***
Dim objPutDoc As PCDPutDoc
Set objPutDoc = CreateObject( _
"PCDClient.PCDPutDoc")
objPutDoc.SetDST DST

objPutDoc.NextRow
Dim objPutStream As Object
Set objPutStream = _
objPutDoc.GetPropertyValue("%CONTENT")
Dim FileName
FileName = txtFileName.Text

Open FileName For Binary Access Read As #1
TotalFileSize = LOF(1)
While (Not EOF(1))
TotalBytesWritten = TotalFileSize
If TotalBytesWritten > 0 Then

If (TotalBytesWritten > UBound(bdata)) Then

TotalBytesWritten = UBound(bdata) + 1

Else

TotalFileSize = TotalBytesWritten
End If

Get #1, , bdata
objPutStream.Write bdata, TotalBytesWritten
Debug.Print TotalBytesWritten, _
objPutStream.BytesWritten, _
objPutStream.ErrNumber, _
objPutStream.ErrDescription
TotalBytesWritten = TotalFileSize - _
TotalBytesWritten
End If

Wend

objPutStream.SetComplete
Close #1
objDoc.Update
If objDoc.ErrNumber <> 0 Then
Debug.Print objDoc.ErrNumber, _
objDoc.ErrDescription
End If
Set objPutDoc = Nothing
Set objDoc = Nothing
Debug.Print "Upload completed!"


Thank you in advance.

0
Comment
Question by:howardseay
3 Comments
 

Author Comment

by:howardseay
Comment Utility
I finally figured it out.  There were a few things wrong.  Here is the corrected block of code.

        Dim FileName

        FileName = "P:\Projects\COH_DIM\images\005.doc"

        Dim fs As FileStream = File.Open(FileName, FileMode.Open, FileAccess.Read)

        Dim breader As BinaryReader = New BinaryReader(fs)

        Dim bdata(1024) As Byte
 

        TotalFileSize = fs.Length

        Dim bytesToWrite As Integer

        bytesToWrite = 1024

        Dim lStart As Long

        Dim lLength As Long

        lStart = 0

        lLength = 1024

        Do

            breader.Read(bdata, 0, lLength)

            objPutStream.Write(bdata, lLength)

            If lStart = TotalFileSize Then Exit Do
 

            If lStart + 1024 > TotalFileSize Then

                lLength = TotalFileSize - lStart - 1

            End If
 

            lStart = lStart + lLength + 1

        Loop

        objPutStream.SetComplete()

Open in new window

0
 

Accepted Solution

by:
EE_AutoDeleter earned 0 total points
Comment Utility
howardseay,
Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.

EE_AutoDeleter
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
A short article about problems I had with the new location API and permissions in Marshmallow
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now