Click here to Skip to main content
14,770,081 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code below I used so when a button is clicked, the contents of a CSV file are loaded into the DataGridView. However, when I click the button, only one line prints and the rest do not print even if I click it again.

I am unsure on what the problem is, some help would be appreciated


What I have tried:

Private Sub btnLoadDGVGeneral_Click(sender As Object, e As EventArgs) Handles btnLoadDGVGeneral.Click
        'New Variable: fname, represents File Path of CSV as String
        Dim fname As String = "E:\SAT\\\bin\Debug\ListofTasks.csv"
        Dim reader As New StreamReader(fname, Encoding.Default)
        Dim sline As String = ""
        Dim colsexpected As Integer = 7
        Dim r As Integer = 0

        sline = reader.ReadLine
            sline = reader.ReadLine
            If sline Is Nothing Then Exit Do
            Dim details() As String = sline.Split(",")
            For i As Integer = 0 To 7
                dataGVGeneral.Rows(r).Cells(i).Value = details(i)
            r = r + 1
    Catch ex As Exception
    End Try
End Sub
Updated 26-Jul-20 4:40am
[no name] 26-Jul-20 7:57am
I assume the first sline = reader.ReadLine before the loop is to skip the column header info in the csv. So the question: Are there really more than 1 line of data in the csv?
Shaheer Rizwan 26-Jul-20 7:59am
yes, there's actually 13 more lines in the CSV file. I've got no idea why it isnt printing the rest
[no name] 26-Jul-20 8:03am
So I suggest to use the debugger and step through the code.
[no name] 26-Jul-20 8:22am
What happens if you comment out the "try catch" stuff?
Shaheer Rizwan 26-Jul-20 8:49am
I ran it without all the try stuff and this came up
[no name] 26-Jul-20 9:04am
So it shows you details() length is 7. Unfortunatelly I don't see on what line. But anyway becuase you hard coded the loop "from 0 to 7" this would be a problem (the line in the csv does not have 8 fields.

Try something like this (I don't know VB)
For i As Integer = 0 To details.Length - 1

1 solution

Almost certainly, it's been hidden by your error handling:
Never "swallow" exceptions:
Catch ex As Exception
End Try
Report them to the user, log them, do something with them, or don't bother with Try ... Catch at all.

When you swallow an exception, you throw away all the information that helps you fix a problem - you even throw away the fact that there was a problem in the first place, so it shows up later as a more serious and harder to identify and fix problem.

And we can't tell what is happening here as a result - and even if we could, we couldn;t test it because we don't have any access to your data.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Shaheer Rizwan 26-Jul-20 8:04am
so... should I try something else except of Try? Im a bit of a noob when it comes to coding and its for an assignment so im stressing so sorry if I wasn't thorough with explaining my problem
[no name] 26-Jul-20 8:05am
Oooh so obvious and I don't see it :blush:. +5
Shaheer Rizwan 26-Jul-20 8:16am
well i ran the debugger and went through it, and i still cant figure it out
OriginalGriff 26-Jul-20 8:28am
It "went through it" because you swallow the exception. If you add a Catch block, that's saying "I know what I'm doing with this error, so don't worry about it" to the system. So it doesn't. Your app doesn't stop working, the debugger doesn't trigger a break for you. The error silently, and mysteriously slides away into the far distance so no one can see it. Your code goes wrong, and doesn't display what you expected it to, but you have no idea why.

So stop swallowing exceptions!

Put a breakpoint at the top of that method and single step your way through it. Almost certainly, it will "jump" from somewhere in that code to the Catch block - so you can look at the Exception object and see why it jumped there (you will already know from where it jumped).

Armed with that information, you should be able to work out why. But without it? Whistling in the dark ...
Shaheer Rizwan 26-Jul-20 8:44am
I ran it by removing the Try statements, and this came up:
Shaheer Rizwan 26-Jul-20 8:51am
Nvm I fixed it. Although I couldn't understand it at first it was actually very helpful. Thanks man
Shaheer Rizwan 26-Jul-20 8:53am
The main problem wasn't actually with the code (semi). First I removed the try statements, then I changed a column which was a combobox to a normal textbox and it worked. I'm just going to validate that column to only accept numbers now.
OriginalGriff 26-Jul-20 8:54am
You're welcome!

And ... stop swallowing exceptions - see how useful they can be? :laugh:
Shaheer Rizwan 26-Jul-20 8:55am
yeah I will :) lol

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900