Solved

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

Posted on 2014-04-07
4
209 Views
Last Modified: 2014-04-07
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
Comment
Question by:murbro
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 39984360
Change the Read to oReader.ReadLine()
0
 

Author Closing Comment

by:murbro
ID: 39984386
Thanks. Well spotted!
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 39984415
Use
Dim oReadLine As String = oReader.ReadLine()

Open in new window

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

Open in new window

0
 

Author Comment

by:murbro
ID: 39984427
Hi MacroShadow. Sorry I accepted before seeing your post. Would have split the points
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

14 Experts available now in Live!

Get 1:1 Help Now