Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

VB.net Using text files to create incremental numbers not working

Hi
I am testingthe function below that I am trying to create incremental invoice numbers with, ie 1,2,3,4 etc but for some reason the results are 1, 49, 54, 54, 54 etc

Sub Test
        MsgBox(oGet_Invoice_Number)
End Sub

    Function oGet_Invoice_Number() As Long
        Try
            Dim oLastInvoiceNumber As Long
            Dim oInvoiceNumber As Long
            If System.IO.Directory.Exists("c:\Program Files\KISS_Accounting") = False Then
                System.IO.Directory.CreateDirectory("c:\Program Files\KISS_Accounting")
            End If
            Dim FILE_NAME As String = "c:\Program Files\KISS_Accounting\Invoice_Number.txt"
            'So first see if text file is there - if not write the initial file with 1 as number
            If System.IO.File.Exists(FILE_NAME) = False Then
                Dim oWriter As New System.IO.StreamWriter(FILE_NAME)
                oWriter.Write("1")
                oWriter.Close()
                oGet_Invoice_Number = 1
                Exit Function
            Else
                Dim oReader As New System.IO.StreamReader(FILE_NAME)
                Dim oReadLine As String = oReader.Read()
                oLastInvoiceNumber = CLng(oReadLine)
                oReader.Close()
                oInvoiceNumber = oLastInvoiceNumber + 1
                Dim oWriter As New System.IO.StreamWriter(FILE_NAME)
                oWriter.Write(CStr(oInvoiceNumber))
                oWriter.Close()
                oGet_Invoice_Number = oInvoiceNumber
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Open in new window

0
Murray Brown
Asked:
Murray Brown
  • 2
1 Solution
 
KimputerCommented:
Change the Read to oReader.ReadLine()
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks. Well spotted!
0
 
MacroShadowCommented:
Use
Dim oReadLine As String = oReader.ReadLine()

Open in new window

instead of
Dim oReadLine As String = oReader.Read()

Open in new window

0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi MacroShadow. Sorry I accepted before seeing your post. Would have split the points
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now