Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I was trying to run this code:

VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

       If dataGridView1.RowCount > 0 Then

           Dim RowcCount As [String] = ""
           Dim Startuppath As String = Application.StartupPath + "/"
           'File Extension as your requirement .dat or .txt
           Dim Destinationpath As String = Startuppath & "" & DateTime.Now.ToString("dd-MMM-yyy") & ".txt"
           Using Streamwrite As StreamWriter = File.CreateText(Destinationpath)

               For i As Integer = 0 To dataGridView1.Rows.Count - 1
                   RowcCount = ""
                   For j As Integer = 0 To dataGridView1.Columns.Count - 1
                       If RowcCount.Length > 0 Then
                           RowcCount = RowcCount & "," & DataGridView1.Rows(i).Cells(j).Value.ToString()
                       Else
                           RowcCount = dataGridView1.Rows(i).Cells(j).Value.ToString()
                       End If
                   Next
                   Streamwrite.WriteLine(RowcCount)
               Next
               Streamwrite.WriteLine(Streamwrite.NewLine)
               Streamwrite.Close()
               MessageBox.Show("File Created Successfully")

           End Using

       Else
       End If
   End Sub



There is an error this line when the program is being exceuted.

RowcCount = dataGridView1.Rows(i).Cells(j).Value.ToString()


There error is

NullReferenceException was unhandled.
An unhandled exception of type 'System.NullReferenceException' occurred in AllTrialApplication.exe
Addition information: object reference not set to an instance of an object
Posted
Updated 8-Sep-14 14:41pm
v2

dataGridView1.Rows(i).Cells(j).Value.ToString() will throw an error if you are trying to typecast a null.
Thus check if dataGridView1.Rows(i).Cells(j).Value is null before typecasting to string.
Or elae, use Convert.ToString();[^].
 
Share this answer
 
dataGridView1.Rows(i).Cells(j).Value.ToString() will throw an error if you are trying to typecast a null.
Thus check if dataGridView1.Rows(i).Cells(j).Value is null before typecasting to string.
Or elae, use Convert.ToString()[^].
 
Share this answer
 
Comments
NekoNao 8-Sep-14 5:32am    
There is data in the last row but still gets the error.
NekoNao 8-Sep-14 20:44pm    
It always has errors in the LAST ROW. that it said it is NULL but there is data
The only solution which can help you is that you put break point and debug code line by line.
And trace what the exactly value you are showing in datagirdview and what you are trying to get.
and for helping check following links.

Debugging Your Visual Basic Application

if any issue the let me know.

-> M.U
 
Share this answer
 
The only solution I got is that i changed

For i As Integer = 0 To dataGridView1.Rows.Count - 1

to

For i As Integer = 0 To dataGridView1.Rows.Count - 2


I do not know why but it works!
 
Share this answer
 

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