Debugging Errors Unhanded Exception

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime.
Parameter name: value
   at System.DateTime.AddTicks(Int64 value)
   at System.DateTime.Add(Double value, Int32 scale)
   at CDMFtp.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\Frank\Documents\Visual Studio 2013\Projects\CDMFtp\CDMFtp\Form1.vb:line 20
   at System.EventHandler.Invoke(Object sender, EventArgs e)


Getting this now. No compile error as well again

Imports System.IO.Compression ' Declaring refrences
Imports System.IO ' Declaring refrences
Imports System.Web
Imports System.Net.Mail

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim startPath1 As String = "D:\Bootdrv\AlohaQs\DATA\" ' Setting the DATA Folder For Zipping
        Dim startPath2 As String = "D:\Bootdrv\AlohaQs\NEWDATA\" ' Setting the NEWDATA Folder for Zipping
        Dim zipPath1 As String = "D:\Bootdrv\AlohaQs\WILL_DATA.zip" ' Nameing the Backup ZIP
        Dim zipPath2 As String = "D:\Bootdrv\AlohaQs\WILL_NEWDATA.ZIP" 'Nameing the Back up ZIP
        Dim objCurrentDate As Date ' Setting todays Date
        Dim objDataFileDate ' Declaring the file directory
        Dim intDays As Integer
        Dim objDate As Date
        Dim strDatex As String
        Dim myFTPaddress As String = "ftp://" ' ftp server
        Dim myFTPuserName As String = "username" ' ftp username
        Dim myFTPuserPassword As String = "pw" 'ftp password
        intDays = -14
        Do While intDays >= -44
            objDate = objCurrentDate.AddDays(intDays) ' add days up to - 30 days
            strDatex = objDate.ToString("yyyyMMdd") ' format date
            objDataFileDate = Directory.GetCreationTime("D:Bootdrv\AlohaQS\" & strDatex)
            Dim startPath4 As String = ("D:\Bootdrv\AlohaQS\" & strDatex)
            Dim zipPath3 As String = ("D:\Bootdrv\AlohaQS\" & strDatex)
            ZipFile.CreateFromDirectory(startPath4, zipPath3)
            Dim myFiles() As String = {"D:\Bootdrv\AlohaQS\" & strDatex}  '// files for Uploading
            For Each mySelectedFile As String In myFiles '// loop thru your files.
                My.Computer.Network.UploadFile(mySelectedFile, myFTPaddress, myFTPuserName, myFTPuserPassword)
            Next
            MessageBox.Show("The upload was successfull!!!")
            Dim strDebout1 As StreamWriter
            strDebout1 = File.CreateText("D:\Bootdrv\AlohaQS\TMP\FTPBACKUPDebout.txt")
            strDebout1.Write("FTP Back Up Successfull")
            strDebout1.Close()
            intDays = intDays - 1
        Loop
        ZipFile.CreateFromDirectory(startPath1, zipPath1)
        ZipFile.CreateFromDirectory(startPath2, zipPath2)
        Dim myFiles1 As String = ("D:\Bootdrv\Alohaqs\WILL_DATA.ZIP, D:\Bootdrv\Alohaqs\WILL_NEWDATA.ZIP")
        For Each mySelectedFile As String In myFiles1 '// loop thru your files.
            My.Computer.Network.UploadFile(mySelectedFile, myFTPaddress, myFTPuserName, myFTPuserPassword)
        Next
        Dim strDebout As StreamWriter
        strDebout = File.CreateText("D:\Bootdrv\AlohaQS\TMP\FTPBACKUPDebout.txt")
        strDebout.Write("FTP Back Up Successfull")
        strDebout.Close()

    End Sub
End Class

Open in new window

Fradojr88Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Seems like  
objDate = objCurrentDate.AddDays(intDays)

Open in new window

don't work as expected.

btw, don't use type prefixes. Especially as you don't do it consequently, you'll get hard to read code.

Also separate your logic. Build first the list of files to archive. Then upload those files.
0
Fradojr88Author Commented:
Can you explain type prefixes I believe I know what you are talking about but not sure. The whole principle of the program is to take the system date - 30 days up to 14 days from today and upload those zipped files to a ftp and this to only one once a month. The directories are created in a 20150805 format.  I was re-looking at the code and I may not even need the get file creation date line any more as its not checking that any more. The files need to run in a loop if possible as the date will be forever changing.
0
ste5anSenior DeveloperCommented:
Like obj and str. Use a more "modular" approach. Start with:

 
Imports System.IO.Compression
Imports System.IO
Imports System.Web
Imports System.Net.Mail
Module Module1

    Sub Main()

        Dim Directories As List(Of String)

        Directories = CreateDirectoryList("C:\Temp\")
        ZipDirectories(Directories)
        UploadZips(Directories)

        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub

    Function CreateDirectoryList(ABaseDirectory As String) As List(Of String)

        Dim DayOffset As Integer = -1
        Dim Result As List(Of String) = New List(Of String)
        Dim TestDirectory As String
        Dim TestDate As String

        CreateDirectoryList = New List(Of String)
        Do While DayOffset >= -44
            TestDate = Now.AddDays(DayOffset).ToString("yyyyMMdd")
            TestDirectory = ABaseDirectory & TestDate
            If Directory.Exists(TestDirectory) Then
                Result.Add(TestDirectory)
            End If

            DayOffset = DayOffset - 1
        Loop

        If Result.Count = 0 Then
            LogMessage("Nothing to do.")
        End If

        CreateDirectoryList = Result

    End Function

    Private Sub LogMessage(p1 As String)

        ' Your logging code here.

    End Sub

    Sub ZipDirectories(ADirectories As List(Of String))

        Console.WriteLine("Zipping..")

    End Sub

    Private Sub UploadZips(ADirectories As List(Of String))

        Console.WriteLine("Uploading..")
        For Each Directory As String In ADirectories
            Console.WriteLine("* " & Directory)
        Next Directory

    End Sub

End Module

Open in new window


btw, the problem is the uninitalized objCurrentDate variable..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Fradojr88Author Commented:
Alright, I will give that a shot. Wont have much time to fool with the code for a while but will rewrite as a module approach.
0
ste5anSenior DeveloperCommented:
I use modules cause it is simpler to debug, cause you can use Console.WriteLine() to output some information.

You can to the same on a form.
0
Jacques Bourgeois (James Burger)PresidentCommented:
About your question on type prefixes.

First of all, you are inconsistent when naming your variables. Why use obj (a very generic prefix) while you use more coherent int and str prefixes for integers and strings. You have some strings with str, but others that use my and still others that do not have a prefix. Using prefixes becomes useless when you do not do it coherently, as pointed out by ste5an.

And that old habit of using a prefix to identify variables is considered obsolete nowadays. There are two main reasons for that.

The first one is that while they made sense when languages used only a few dozens of types, they become meaningless with the number of types we have nowadays (over 10000 in the framework). Is str a String, a Stream, a StreamReader, a StringBuilder, or any of the dozens of classes whose name starts with str.

The second one is that it made sense at a time where the editors were less versatile and when programmers regularly printed their code. But nowadays, nobody generates printout, and the editors will give you the type in plain in a tooltip when you hover over a variable. It tells a lot more than using trying to remember all these 3 letters codes.

You might still see them in web pages and magazines, because these are the equivalents of printouts and will not give you the tooltip that the editor does. But in real code, you make your code more readable by not using them.

However, I will go a little further on todays conventions. The way you type a variable name can bring information about it. Ste5an puts uppercase characters at the beginning of variable names. In most conventions (there are as many conventions as there are programmers), Uppercase letters are reserved for constants, the name of classes and their members (properties and methods). Parameters to methods and local variables are declared with a lowercase character as the first one. This way, when you encounter something whose name starts with a lowercase, you do not have to look far, you automatically understand that it is defined in the method, and typically not modifiable anywhere else.

Look at Microsoft examples on MSDN, and this is the convention they use.
1
ste5anSenior DeveloperCommented:
This is my old VB(A) style. Constants are upper-case only. Member variables are always accessed with Me.MemberName. Thus having local variables with CamelCase and is not a problem :)
0
Fradojr88Author Commented:
I just finished a visual basic class as i am just now getting into the in debt programming classes and it was what they were teaching and the book recommended.
0
Jacques Bourgeois (James Burger)PresidentCommented:
Well, you will be able to teach something to your teachers.

Old teachers have old habits. And old habits are hard to lose. If your book is a training manual written by teacher, it might suffer from the same problem. It if is a commercial book, well you should know that a lot of basic books about VB were originally written 15 years ago. They are updated with every new version of Visual Studio to incorporate the new stuff, but the old code is usually not revised. So your book might also not follow the conventions suggested for Visual Basic.

Look at the Microsoft General Naming Conventions. One of the first rules in the page is DO NOT use Hungarian notation, which is the term commonly used to describe the use of prefixes for variable names.

If you click on the Older Versions link at the top of the page, and go to version 2.0, you will see the same statement. Version 2.0 is the version of the framework that was used 10 years ago, so this rule has been there for a while.
1
Fradojr88Author Commented:
The form loads but it does not seem to be doing anything. I have rewritten everything in Modules and I am at a lost with what I missed? The form compiles fine and loads but it does not seem to be executing.
Imports System.IO.Compression ' Declaring refrences
Imports System.IO ' Declaring refrences
Imports System.Web
Imports System.Net.Mail
Module MainModule1

    Sub Main()

        Dim Directories As List(Of String)

        Directories = CreateDirectoryList("D:\Bootdrv\Alohaqs")
        ZipDirectories(Directories)
        UploadZips(Directories)
        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub

    Function CreateDirectoryList(ABaseDirectory As String) As List(Of String)

        Dim DayOffset As Integer = -1
        Dim Result As List(Of String) = New List(Of String)
        Dim TestDirectory As String
        Dim TestDate As String

        CreateDirectoryList = New List(Of String)
        Do While DayOffset >= -44
            TestDate = Now.AddDays(DayOffset).ToString("yyyyMMdd")
            TestDirectory = ABaseDirectory & TestDate
            If Directory.Exists(TestDirectory) Then
                Result.Add(TestDirectory)
            End If

            DayOffset = DayOffset - 1
        Loop

        If Result.Count = 0 Then
            LogMessage("Nothing to do.")
        End If

        CreateDirectoryList = Result

    End Function

    Private Sub LogMessage(p1 As String)

        ' Your logging code here.

    End Sub

    Sub ZipDirectories(ADirectories As List(Of String))
        Dim startPath1 As String = "D:\Bootdrv\AlohaQs\DATA\" ' Setting the DATA Folder For Zipping
        Dim startPath2 As String = "D:\Bootdrv\AlohaQs\NEWDATA\" ' Setting the NEWDATA Folder for Zipping
        Dim zipPath1 As String = "D:\Bootdrv\AlohaQs\WILL_DATA.zip" ' Nameing the Backup ZIP
        Dim zipPath2 As String = "D:\Bootdrv\AlohaQs\WILL_NEWDATA.ZIP" 'Nameing the Back up ZIP
        ZipFile.CreateFromDirectory(startPath1, zipPath1)
        ZipFile.CreateFromDirectory(startPath2, zipPath2)
        For Each Directory As String In ADirectories
            Dim DirstartPath As String = (Directory)
            Dim Dirzippath As String = (Directory & ".zip")
            ZipFile.CreateFromDirectory(DirstartPath, Dirzippath)
        Next


    End Sub

    Private Sub UploadZips(ADirectories As List(Of String))
        Dim myFTPaddress = "ftp://"
        Dim myFTPuserName = ""
        Dim myFTPuserPassword = ""
        Console.WriteLine("Uploading..")
        For Each Directory As String In ADirectories
            Console.WriteLine("* " & Directory)

            For Each mySelectedFile As String In Directory '// loop thru your files.
                My.Computer.Network.UploadFile(mySelectedFile, myFTPaddress, myFTPuserName, myFTPuserPassword)
            Next
            MessageBox.Show("The upload was successfull!!!")
            Dim Debout1 As StreamWriter
            Debout1 = File.CreateText("D:\Bootdrv\AlohaQS\TMP\FTPBACKUPDebout.txt")
            Debout1.Write("FTP Back Up Successfull")
            Debout1.Close()
        Next Directory

        Dim myFiles1 As String = ("D:\Bootdrv\Alohaqs\WILL_DATA.ZIP, D:\Bootdrv\Alohaqs\WILL_NEWDATA.ZIP")
        For Each mySelectedFile As String In myFiles1 '// loop thru your files.
            My.Computer.Network.UploadFile(mySelectedFile, myFTPaddress, myFTPuserName, myFTPuserPassword)
        Next
        Dim Debout As StreamWriter
        Debout = File.CreateText("D:\Bootdrv\AlohaQS\TMP\FTPBACKUPDebout.txt")
        Debout.Write("FTP Back Up Successfull")
        Debout.Close()


    End Sub

End Module
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

           

    End Sub
End Class

Open in new window

0
ste5anSenior DeveloperCommented:
My code is a console application, not WinForms:

Capture.PNG
0
Fradojr88Author Commented:
When ever I try and compile it I am getting the following errors
 

Imports System.IO.Compression ' Declaring refrences
Imports System.IO ' Declaring refrences
Imports System.Web
Imports System.Net.Mail
Module MainModule1

    Sub Main()

        Dim Directories As List(Of String)

        Directories = CreateDirectoryList("D:\Bootdrv\Alohaqs")
        ZipDirectories(Directories)
        UploadZips(Directories)
        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub

    Function CreateDirectoryList(ABaseDirectory As String) As List(Of String)

        Dim DayOffset As Integer = -1
        Dim Result As List(Of String) = New List(Of String)
        Dim TestDirectory As String
        Dim TestDate As String

        CreateDirectoryList = New List(Of String)
        Do While DayOffset >= -44
            TestDate = Now.AddDays(DayOffset).ToString("yyyyMMdd")
            TestDirectory = ABaseDirectory & TestDate
            If Directory.Exists(TestDirectory) Then
                Result.Add(TestDirectory)
            End If

            DayOffset = DayOffset - 1
        Loop

        If Result.Count = 0 Then
            LogMessage("Nothing to do.")
        End If

        CreateDirectoryList = Result

    End Function

    Private Sub LogMessage(p1 As String)

        ' Your logging code here.

    End Sub

    Sub ZipDirectories(ADirectories As List(Of String))
        Dim startPath1 As String = "D:\Bootdrv\AlohaQs\DATA\" ' Setting the DATA Folder For Zipping
        Dim startPath2 As String = "D:\Bootdrv\AlohaQs\NEWDATA\" ' Setting the NEWDATA Folder for Zipping
        Dim zipPath1 As String = "D:\Bootdrv\AlohaQs\WILL_DATA.zip" ' Nameing the Backup ZIP
        Dim zipPath2 As String = "D:\Bootdrv\AlohaQs\WILL_NEWDATA.ZIP" 'Nameing the Back up ZIP
        ZipFile.CreateFromDirectory(startPath1, zipPath1)
        ZipFile.CreateFromDirectory(startPath2, zipPath2)
        For Each Directory As String In ADirectories
            Dim DirstartPath As String = (Directory)
            Dim Dirzippath As String = (Directory & ".zip")
            ZipFile.CreateFromDirectory(DirstartPath, Dirzippath)
        Next


    End Sub

    Private Sub UploadZips(ADirectories As List(Of String))
        Dim myFTPaddress = "ftp://"
        Dim myFTPuserName = "Username"
        Dim myFTPuserPassword = "PW"
        Console.WriteLine("Uploading..")
        For Each Directory As String In ADirectories
            Console.WriteLine("* " & Directory)

            For Each mySelectedFile As String In Directory '// loop thru your files.
                My.Computer.Network.UploadFile(mySelectedFile, myFTPaddress, myFTPuserName, myFTPuserPassword)
            Next
            MessageBox.Show("The upload was successfull!!!")
            Dim Debout1 As StreamWriter
            Debout1 = File.CreateText("D:\Bootdrv\AlohaQS\TMP\FTPBACKUPDebout.txt")
            Debout1.Write("FTP Back Up Successfull")
            Debout1.Close()
        Next Directory

        Dim myFiles1 As String = ("D:\Bootdrv\Alohaqs\WILL_DATA.ZIP, D:\Bootdrv\Alohaqs\WILL_NEWDATA.ZIP")
        For Each mySelectedFile As String In myFiles1 '// loop thru your files.
            My.Computer.Network.UploadFile(mySelectedFile, myFTPaddress, myFTPuserName, myFTPuserPassword)
        Next
        Dim Debout As StreamWriter
        Debout = File.CreateText("D:\Bootdrv\AlohaQS\TMP\FTPBACKUPDebout.txt")
        Debout.Write("FTP Back Up Successfull")
        Debout.Close()


    End Sub

    Private Function MessageBox() As Object
        Throw New NotImplementedException
    End Function

End Module

Open in new window


The zipping code I was using does not seem to like the console application

This is where I found the code for Zipping. https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx

But does not want to recognize ZipFile.
ss.jpg
0
Fradojr88Author Commented:
Actually, I remembered I forgot to reference the new project.
0
Fradojr88Author Commented:
How would I get it to write the errors generated by the program into the Log section do you have a example of how that can work? Currently running into some issues and finding it hard to troubleshoot what is causing the errors.? I would like it to log the errors in the logging module. It appears to be failing in the upload module how ever not all the files are zipping that are suppose to the first two are creating fine but the -14 dont seem to be zipping.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.