.net Sdk - Test For Substrings Follow

0
Avatar
Legacy Poster

We have a VB.NET application which gathers data from a number of SQL Server and Visual Foxpro databases into a datatable.
The datatable is read a row at a time. A datarow is sent to a subroutine that extracts each column from the datarow to populate all the named substings on a .btw format document.
This application has been working successfully for a couple of years.

We are enhancing the application and will be modifying our .btw files. The new application will have more data to put onto labels than the previous version. We would like to get the updated application into production before we have updated all of our .btw files with the additional named substrings. If we try to set the value of a substring that does not exist, the application halts with an error.

Is there a way of checking if a named substring exists on a format file before trying to set the value of the substring?



A snippet of the code is shown below.

Sample Code:
============


Private Sub PrintNutritionLabel(ByRef btEngine As Engine, _
ByRef btFormat As LabelFormatDocument, _
ByRef FieldRow As DataRow, _
ByRef strLabelTemplate As String, _
ByRef strPrinter As String)

Open_Bartender_Format_File:

btFormat = btEngine.Documents.Open(strLabelTemplate)

Build_Label_Object:

btFormat.SubStrings("ProductDesc").Value = FieldRow.Item("ProductDesc").ToString.Trim

btFormat.SubStrings("UOM").Value = strSalesUnit.ToString.Trim

btFormat.SubStrings("ServesPerPack").Value = FieldRow.Item("ServesPerPack").ToString.Trim
btFormat.SubStrings("ServeSize").Value = FieldRow.Item("ServeSize").ToString.Trim

btFormat.SubStrings("Energy100").Value = Per100(FieldRow.Item("Energy"))
btFormat.SubStrings("EnergyPerServe").Value = PerServe(FieldRow.Item("Energy"), FieldRow.Item("ServeSize"))
.
.
.
more lines of code
.
.


btFormat.PrintSetup.PrinterName = strPrinter

If FieldRow.Item("NumLabels") <> 0 Then
btFormat.PrintSetup.IdenticalCopiesOfLabel = FieldRow.Item("NumLabels")
btFormat.Print()
btFormat.Close(SaveOptions.DoNotSaveChanges)
End If

End Sub

1 comments

0
Avatar
Shotaro Ito
Comment actions Permalink

[quote name='Richard S' timestamp='1342447149' post='2851']
Is there a way of checking if a named substring exists on a format file before trying to set the value of the substring?
[/quote]

Hi Richard,
You can list up all substrings in a document as sample below. (search .net SDK documentation by "Substrings".)

[code]Public Sub Demo()
' Initialize a new BarTender print engine.'
Using btEngine As New Engine()
' Start the BarTender print engine.'
btEngine.Start()

' Open a format document.'
Dim btFormat As LabelFormatDocument = btEngine.Documents.Open("C:\Format1.btw")

' Display the number of substrings in the format.'
Console.WriteLine("SubStrings Count: " & btFormat.SubStrings.Count)

' Iterate through and read each SubString Name, Type and Value.'
For Each substring As SubString In btFormat.SubStrings
Console.WriteLine("SubString Name: " & substring.Name & ", SubString Type: " & substring.Type & ", SubString Value: " & substring.Value)
Next substring

' Set a SubString Value using its index.'
btFormat.SubStrings(0).Value = "New SubString Value"

' Set a SubString Value using its name (case sensitive).'
btFormat.SubStrings("SubString1").Value = "New SubString Value"

' Close the current format without saving.'
btFormat.Close(SaveOptions.DoNotSaveChanges)

' Stop the BarTender print engine.'
btEngine.Stop()
End Using
End Sub
[/code]

Also, Substrings.GetAll() will returns list of all pairs of substring name and value in one string with specified delimiter.

Please sign in to leave a comment.