bergertime
asked on
vb.net write a .pdf but locks the file once it is written
I've had to update a program someone else wrote in vb.net 2003 to vb.net 2005. It writes a pdf to a folder but won't release the file until I close the program. Here is the code below. I'm new to .net so please type slowly. Thanks.
Private Sub CreatePDF()
'Prints the airbill report
Dim ReportName As New AirbillReport
Dim myexportoptions As CrystalDecisions.Shared.Ex portOption s
Dim mydiskfiledestinationoptio ns As CrystalDecisions.Shared.Di skFileDest inationOpt ions
Dim myexportfile As String
myexportfile = "C:\Documents and Settings\mikeb\My Documents\Visual Studio 2005\Projects\AnatomicalSe rvicesProg ram\Airbil ls\" & txtAirbillNumber.Text & ".pdf"
mydiskfiledestinationoptio ns = New CrystalDecisions.Shared.Di skFileDest inationOpt ions()
mydiskfiledestinationoptio ns.DiskFil eName = myexportfile
myexportoptions = ReportName.ExportOptions
With myexportoptions
.DestinationOptions = mydiskfiledestinationoptio ns
.ExportDestinationType = CrystalDecisions.[Shared]. ExportDest inationTyp e.DiskFile
.ExportFormatType = CrystalDecisions.[Shared]. ExportForm atType.Por tableDocFo rmat
End With
'ReportName.SetDataSource( ShipmentIn formationD ataset)
'Inserted as testing for Crystal Decisions problem we are having 1-26-05
'ShipmentInformationDatase t.WriteXml ("c:\Shipm entData.xm l")
ReportName.Export()
ReportName.Close()
SendMessage(myexportfile)
'System.IO.File.Delete(mye xportfile)
End Sub
Private Sub CreatePDF()
'Prints the airbill report
Dim ReportName As New AirbillReport
Dim myexportoptions As CrystalDecisions.Shared.Ex
Dim mydiskfiledestinationoptio
Dim myexportfile As String
myexportfile = "C:\Documents and Settings\mikeb\My Documents\Visual Studio 2005\Projects\AnatomicalSe
mydiskfiledestinationoptio
mydiskfiledestinationoptio
myexportoptions = ReportName.ExportOptions
With myexportoptions
.DestinationOptions = mydiskfiledestinationoptio
.ExportDestinationType = CrystalDecisions.[Shared].
.ExportFormatType = CrystalDecisions.[Shared].
End With
'ReportName.SetDataSource(
'Inserted as testing for Crystal Decisions problem we are having 1-26-05
'ShipmentInformationDatase
ReportName.Export()
ReportName.Close()
SendMessage(myexportfile)
'System.IO.File.Delete(mye
End Sub
On the other hand a SendMessage() sub could cause this problem. Post your code here if the above does not help
ASKER
I think this may be the code your looking for.
Private Sub StoreAirbill()
'Declare and instantiate the dataset row.
Dim sir As ShipmentInformation.Shipme ntInformat ionRow
'Ready dataset row for data insertion.
sir = ShipmentInformationDataset .ShipmentI nformation .NewRow
'Databind dataset fields to form fields.
sir.AirbillNumber = txtAirbillNumber.Text
'Removed labels from form but left information incase I need to add it back.
sir.Labels = ""
sir.Origin = txtorigin.Text
sir.Destination = txtdestination.Text
sir.DeclaredValue = txtdeclaredvalue.Text
sir.DeliveryDate = txtdate.Text
sir.Time = txttime.Text
sir.ShipperRefNumber = txtshipperref.Text
sir.ConsigneeRefNumber = txtconsigneeref.Text
sir.ThirdPartyRefNumber = txtthirdref.Text
If optnextday.Checked = True Then
sir.NextDay = "X"
ElseIf opt2ndday.Checked = True Then
sir.SecondDay = "X"
ElseIf opt3to5day.Checked = True Then
sir.Threeto5Day = "X"
End If
If Optshipper.Checked = True Then
sir.Shipper = "X"
ElseIf optconsignee.Checked = True Then
sir.Consignee = "X"
ElseIf optthirdparty.Checked = True Then
sir.ThirdParty = "X"
End If
sir.ShipperName = txtShipperName.Text
sir.ShipperAddress = txtshipperaddress.Text
sir.ShipperCity = txtshippercity.Text
sir.ShipperState = txtshipperstate.Text
sir.ShipperZip = txtshipperzip.Text
sir.ShipperContact = txtshippercontactname.Text
sir.ShipperPhone = txtshippernumber.Text
'sir.ShipperShow = txtShipperShow.Text
'sir.ShipperBooth = txtShipperBooth.Text
sir.ConsigneeName = cmbConsigneeName.Text
sir.ConsigneeAddress = txtconsigneeaddress.Text
sir.ConsigneeCity = txtconsigneecity.Text
sir.ConsigneeState = txtconsigneestate.Text
sir.ConsigneeZip = txtconsigneezip.Text
sir.ConsigneeContact = txtconsigneecontactname.Te xt
sir.ConsigneePhone = txtconsigneenumber.Text
'sir.ConsigneeBooth = txtConsigneeBooth.Text
'sir.ConsigneeBooth = txtConsigneeBooth.Text
sir.ThirdName = cmbThirdPartyName.Text
sir.ThirdAddress = txtthirdaddress.Text
sir.ThirdCity = txtthirdcity.Text
sir.ThirdState = txtthirdstate.Text
sir.ThirdZip = txtthirdzip.Text
sir.ThirdContact = txtthirdcontactname.Text
sir.ThirdPhone = txtthirdnumber.Text
sir.Pieces1 = txtPieces1.Text
sir.Length1 = txtLength1.Text
sir.Width1 = txtWidth1.Text
sir.Height1 = txtHeight1.Text
sir.Description1 = txtDescription1.Text
sir.Weight1 = txtWeight1.Text
sir.Pieces2 = txtPieces2.Text
sir.Length2 = txtLength2.Text
sir.Width2 = txtWidth2.Text
sir.Height2 = txtHeight2.Text
sir.Description2 = txtDescription2.Text
sir.Weight2 = txtWeight2.Text
sir.Pieces3 = txtPieces3.Text
sir.Length3 = txtLength3.Text
sir.Width3 = txtWidth3.Text
sir.Height3 = txtHeight3.Text
sir.Description3 = txtDescription3.Text
sir.Weight3 = txtWeight3.Text
sir.Pieces4 = txtPieces4.Text
sir.Length4 = txtLength4.Text
sir.Width4 = txtWidth4.Text
sir.Height4 = txtHeight4.Text
sir.Description4 = txtDescription4.Text
sir.Weight4 = txtWeight4.Text
sir.Instructions = txtSpecialInstructions.Tex t
'Insert data to dataset.
ShipmentInformationDataset .ShipmentI nformation .Rows.Add( sir)
'Print the airbill using information entered into the dataset.
CreatePDF()
'Clears all information out of the dataset to ready it for a new entry.
ShipmentInformationDataset .Clear()
Private Sub StoreAirbill()
'Declare and instantiate the dataset row.
Dim sir As ShipmentInformation.Shipme
'Ready dataset row for data insertion.
sir = ShipmentInformationDataset
'Databind dataset fields to form fields.
sir.AirbillNumber = txtAirbillNumber.Text
'Removed labels from form but left information incase I need to add it back.
sir.Labels = ""
sir.Origin = txtorigin.Text
sir.Destination = txtdestination.Text
sir.DeclaredValue = txtdeclaredvalue.Text
sir.DeliveryDate = txtdate.Text
sir.Time = txttime.Text
sir.ShipperRefNumber = txtshipperref.Text
sir.ConsigneeRefNumber = txtconsigneeref.Text
sir.ThirdPartyRefNumber = txtthirdref.Text
If optnextday.Checked = True Then
sir.NextDay = "X"
ElseIf opt2ndday.Checked = True Then
sir.SecondDay = "X"
ElseIf opt3to5day.Checked = True Then
sir.Threeto5Day = "X"
End If
If Optshipper.Checked = True Then
sir.Shipper = "X"
ElseIf optconsignee.Checked = True Then
sir.Consignee = "X"
ElseIf optthirdparty.Checked = True Then
sir.ThirdParty = "X"
End If
sir.ShipperName = txtShipperName.Text
sir.ShipperAddress = txtshipperaddress.Text
sir.ShipperCity = txtshippercity.Text
sir.ShipperState = txtshipperstate.Text
sir.ShipperZip = txtshipperzip.Text
sir.ShipperContact = txtshippercontactname.Text
sir.ShipperPhone = txtshippernumber.Text
'sir.ShipperShow = txtShipperShow.Text
'sir.ShipperBooth = txtShipperBooth.Text
sir.ConsigneeName = cmbConsigneeName.Text
sir.ConsigneeAddress = txtconsigneeaddress.Text
sir.ConsigneeCity = txtconsigneecity.Text
sir.ConsigneeState = txtconsigneestate.Text
sir.ConsigneeZip = txtconsigneezip.Text
sir.ConsigneeContact = txtconsigneecontactname.Te
sir.ConsigneePhone = txtconsigneenumber.Text
'sir.ConsigneeBooth = txtConsigneeBooth.Text
'sir.ConsigneeBooth = txtConsigneeBooth.Text
sir.ThirdName = cmbThirdPartyName.Text
sir.ThirdAddress = txtthirdaddress.Text
sir.ThirdCity = txtthirdcity.Text
sir.ThirdState = txtthirdstate.Text
sir.ThirdZip = txtthirdzip.Text
sir.ThirdContact = txtthirdcontactname.Text
sir.ThirdPhone = txtthirdnumber.Text
sir.Pieces1 = txtPieces1.Text
sir.Length1 = txtLength1.Text
sir.Width1 = txtWidth1.Text
sir.Height1 = txtHeight1.Text
sir.Description1 = txtDescription1.Text
sir.Weight1 = txtWeight1.Text
sir.Pieces2 = txtPieces2.Text
sir.Length2 = txtLength2.Text
sir.Width2 = txtWidth2.Text
sir.Height2 = txtHeight2.Text
sir.Description2 = txtDescription2.Text
sir.Weight2 = txtWeight2.Text
sir.Pieces3 = txtPieces3.Text
sir.Length3 = txtLength3.Text
sir.Width3 = txtWidth3.Text
sir.Height3 = txtHeight3.Text
sir.Description3 = txtDescription3.Text
sir.Weight3 = txtWeight3.Text
sir.Pieces4 = txtPieces4.Text
sir.Length4 = txtLength4.Text
sir.Width4 = txtWidth4.Text
sir.Height4 = txtHeight4.Text
sir.Description4 = txtDescription4.Text
sir.Weight4 = txtWeight4.Text
sir.Instructions = txtSpecialInstructions.Tex
'Insert data to dataset.
ShipmentInformationDataset
'Print the airbill using information entered into the dataset.
CreatePDF()
'Clears all information out of the dataset to ready it for a new entry.
ShipmentInformationDataset
I don't see where you use the above code in a code provided in question.
Once again: do you get any error? If you do, show us the code raising error. If not, tell me how you know the file is locked?
Once again: do you get any error? If you do, show us the code raising error. If not, tell me how you know the file is locked?
ASKER
Sorry, I had posted a response to your first post. I must not have hit submit? Anyway, yes I do get an error. Here is what happens: If I leave 'System.IO.File.Delete(mye xportfile) uncommented then the first bill I process I get an error stating that System.IO.IOException the file you are trying to access is locked. Now if I comment out that 'System.IO.File.Delete(mye xportfile) then the first time I write a new bill number ( & txtAirbillNumber.Text &) it works fine and I can write as many as I want to. But if I write a bill with the same AirBillNumber I get the error that the file I am trying to access isd locked. Now I can open the folder that I'm writing the bills to and say I process 10 bills, If I right click and say delete on any of them, windows tells me thay are in use. If I shut down the program and restart, then I can delete the bills. I came home for lunch but I will post the code with the code you provided in it again. With the code you provided, my result was the same. I will also post the complete error I get. Thanks again.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did what you said last night. So the bug was in my Sendmessage just as you said. I added 'MyAttachment.Dispose()' to the end and now everything works as it should. Thanks again for your help. :)
Glad to be helpful
And thanks for the points. They made me a VB.NET Master in EE :)
And thanks for the points. They made me a VB.NET Master in EE :)
<...>
ReportName.Export()
ReportName.Close()
'-------------------------
ReportName = Nothing
myexportoptions = Nothing
mydiskfiledestinationoptio
'-------------------------
SendMessage(myexportfile)
'System.IO.File.Delete(mye
BTW, do you get any error?