hi guzs im trying to read a file and open it when click on the button when i run.I don't know why the file didn't show up on my screen.here the code

Public Partial Class MainForm   
    'structure of products
    Structure products
        Public nameproduct As String
        Public catalogcode As String
        Public wholesade as Integer
        Public saleprice As Integer
    End Structure

    Dim listproduct(100) As products



    Public Sub New()
        ' The Me.InitializeComponent call is required for Windows Forms designer support.
        Me.InitializeComponent()

        '
        ' TODO : Add constructor code after InitializeComponents
        '
    End Sub


    Sub Button5Click(ByVal sender As Object,ByVal e As system.EventArgs)
        me.Close()
    End Sub


    Private Sub ShowproductClick(ByVal sender As system.Object,ByVal e As system.EventArgs)
        Dim srestreamreader As IO.StreamReader
        Dim strline As String
        Dim stringfield(3) As String 'strore extra field value in the code
        Dim numrecord As Integer 'number of record
        Dim stringrecord As String
        Dim result As DialogResult 'what did the user choose in the open dialog (open or cancel)
        dim record as String


     Try



        'then print the file if file exist
        If IO.File.Exists("E:/products.txt ") Then
            srestreamreader = io.File.OpenText("E:/products.txt ")

        Else
            messagebox.Show("File does not exist","Message file ",messageboxbuttons.OK,messageboxicon.Information)

        End If

        'read value that indicate the # of record in the file
        strline = srestreamreader.ReadLine()
        numrecord = convert.toint32(strline)

        Dim countnum As Integer 
        For countnum = 0 To numrecord - 1 Step 1
            strline = srestreamreader.ReadLine()

            stringfield =stringrecord .Split(",") 'fields seperated by commas

            'now i will store into listproduct
             listproduct(numrecord).nameproduct = stringfield(0)
             listproduct(numrecord).catalogcode = stringfield(1)
             listproduct(numrecord).wholesade = stringfield(2)
             listproduct(numrecord).saleprice = stringfield(3)

        Next

        'close the streamreader
        srestreamreader.close()

     Catch ex As Exception
        'handle any error in the designe
        messagebox.Show(ex.Message,"Error loading the file.",messageboxbuttons.ok,messageboxicon.information)
     End Try



    End Sub

End Class

the error was object reference not set to an intance of an object

i fix this error and add some more code and i get this error please help:

Public Partial Class MainForm   

    Public Sub New()
        ' The Me.InitializeComponent call is required for Windows Forms designer support.
        Me.InitializeComponent()

        '
        ' TODO : Add constructor code after InitializeComponents
        '
    End Sub

   'structure of products
    Structure products
        Public namePRoduct As String
        Public cataLOgcode As String
        Public wholesade as Integer
        Public saleprice As Integer 
    End Structure

    Dim listproduct(100) As products
    Dim numrecord As Integer 'number of record

    Sub Button5Click(ByVal sender As Object,ByVal e As system.EventArgs)
        me.Close()
    End Sub


    Private Sub ShowproductClick(ByVal sender As system.Object,ByVal e As system.EventArgs)
        Dim srestreamreader As IO.StreamReader 
        Dim strline As String
        Dim stringfield(3) As String 'strore extra field value in the code
        Dim number as Integer' count the number of integer in the file
        Dim stringrecord As String = 0
        Dim namePR as String
        dim cataLO as String
        'dim record as String



     Try


        'then print the file if file exist
        If IO.File.Exists("F:/products.txt ") Then
            srestreamreader = io.File.OpenText("F:/products.txt ")
            'read value that indicate the # of record in the file

        strline = srestreamreader.ReadLine()
        numrecord = convert.toint32(strline)

        Dim countnum As Integer 
        For countnum = 0 To numrecord - 1 Step 1
            strline = srestreamreader.ReadLine()

            stringfield = stringrecord .Split(",") 'fields seperated by commas

            'now i will store into listproduct
             listproduct(numrecord).nameproduct = stringfield(0)
             listproduct(numrecord).catalogcode = stringfield(1)
             listproduct(numrecord).wholesade = stringfield(2)
             listproduct(numrecord).saleprice = stringfield(3)

        Next 

        'format title
        Dim strCol1Title As String = "Product name"
        Dim strCol2Title As String = "Code"

        strCol1Title = strCol1Title.PadRight(25, " ")
        strCol2Title = strCol2Title.PadRight(4, " ")

        'print title
        displaylabel.Text = strCol1Title & strCol2Title & ControlChars.NewLine & ControlChars.NewLine

        'all students id in lblS
        For number = 0 To numrecord - 1 Step 1

            'format pname & pcode
            namePR = listproduct(number).namePRoduct
            cataLO = listproduct(number).cataLOgcode

            namePR = namePR.PadRight(25, " ")
            cataLO = cataLO.PadLeft(4, " ")

            displaylabel.Text = displaylabel.Text & _
            namePR & _
            cataLO & _
            ControlChars.NewLine

        Next
        'close the streamreader
        srestreamreader.close()

        Else
            messagebox.Show("File does not exist","Message file ",messageboxbuttons.OK,messageboxicon.Information)

        End If

       Catch ex As Exception
        'handle any error in the designe
        messagebox.Show(ex.Message,"Error loading the file.",messageboxbuttons.ok,messageboxicon.information)
     End Try



End Sub

End Class

the error was: index was outside the bounds of the array

You haven't fixed your original problem. In your first post you do the following (line 34):

Dim stringrecord As String

The next time you reference "stringrecord" is in line 60:

stringfield =stringrecord .Split(",")

"stringrecord" is null because it hasn't been set to any value.

It should be:

stringfield =strline.Split(",")

Additionally, in lines 63-65, numrecord should be countnum

'now i will store into listproduct
listproduct(countnum).nameproduct = stringfield(0)
listproduct(countnum).catalogcode = stringfield(1)
listproduct(countnum).wholesade = stringfield(2)
listproduct(countnum).saleprice = stringfield(3)

Comment out your "Try-Catch" statements and you will be able to see where your code is throwing errors (in the debugger).

oh ok thanks i have this errorr in line 82 it say Object reference not set to an instance of an object i assigne it to value 0 but still the same error

the variable namePR is NULL ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.