Results 1 to 2 of 2

Thread: [RESOLVED] Help checking to see repeated data

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    1

    Resolved [RESOLVED] Help checking to see repeated data

    Hi, First off i'm pretty new to this so don't hesitate to explain like i'm some sort of alien.

    I have a form in which a user enters their data compare with the already entered data before storing it in a text file.

    Here is my Code:

    Dim objreadin As New System.IO.StreamReader("C:\results.txt")
    Dim Stats() As String
    Dim Exists As Boolean = False


    Do While objreadin.Peek <> -1
    Stats = Split(objreadin.ReadLine(), ",")

    If Stats(0) = ("Running") And Stats(1) = txtID.Text And Stats(2) = txtWeek.Text Then
    Exists = True
    MsgBox("The training details for this user have already been entered.")
    End If
    Loop
    objreadin.Close()


    I have underlined the "Then" syntax where receive the error which is:

    An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Login System.exe

    Additional information: Index was outside the bounds of the array.



    Thanks if you can help.

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Help checking to see repeated data

    Stats is a string array, populated by the "Split" command.

    In your "If" statement, you are assuming that Stats has three elements; 0,1,2. What if there are only two?, then the index of Stats(2) will be out of range and you get the corresponding exception.

    So you should test for that, see my edits below;
    Code:
    Do While objreadin.Peek <> -1 And Not Exists
        Stats = objreadin.ReadLine.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
        If Stats.Count >2 Then
            If Stats(0) = "Running" And Stats(1) = txtID.Text And Stats(2) = txtWeek.Text Then
                MessageBox.Show("The training details for this user have already been entered.")
                Exists = True
            End If
        End If
    Loop
    objreadin.Close()
    Last edited by Bulldog; Mar 21st, 2015 at 08:02 AM.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width