none
VS 2015 : DataGridView : Stackoverflow Exception RRS feed

  • Question

  • Hello,

    I am getting a stackoverflow exception error when formatting my DataGridView

    I narrow it down to the Trim function

    With Me.dg_main
     For rowIndex = 0 To .RowCount - 1
      .Rows(rowIndex).Cells("job_status").Value = .Rows(rowIndex).Cells("job_status").Value.ToString.Trim
     Next
    End With

    If I remove the Trim, works fine

    The data comes from a MS SQL Sever 2014. The job_status is a varchar(10)

    There is no nulls, has text or Zero length (LEN(job_status))
    The app has been working fine for years.

    Thank you all

    Friday, November 8, 2019 9:58 PM

Answers

  • Maybe make sure that this code is not called from some event handlers that are related to change notifications, and the changes made by this loop do not imply some recursive infinite notifications in this or other parts.

    Give details about the location of your code. When the exception is detected, investigate the call-stack to view the callers.


    • Edited by Viorel_MVP Saturday, November 9, 2019 9:00 AM
    • Marked as answer by Bre-x Sunday, November 10, 2019 6:18 PM
    Saturday, November 9, 2019 9:00 AM

All replies

  • Hi

    I can not induce the error you mention. Here is the code I used to test. There is an additional check for the New Row index to prevent a NullReferenceException.

    (I assume you have the AllowUserToAddRows Property set to False otherwise you too would encounter a 'NullReferenceException')

    I do not encounter an other exceptions here with the code below.

    ' Form1 with DataGridView named dg_main
    ' Button1
    Option Strict Off
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' add some test data
    		With dg_main
    			.Columns.Add("job_status", "job_status")
    
    			For i As Integer = 0 To 99
    				.Rows.Add("  job status  ")
    			Next
    		End With
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		' perform same operation as per post
    		With dg_main
    			For rowIndex As Integer = 0 To .RowCount - 1
    				If Not rowIndex = .NewRowIndex Then
    					.Rows(rowIndex).Cells("job_status").Value = .Rows(rowIndex).Cells("job_status").Value.ToString.Trim
    				End If
    			Next
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Bre-x Friday, November 8, 2019 10:49 PM
    • Unmarked as answer by Bre-x Friday, November 8, 2019 10:52 PM
    Friday, November 8, 2019 10:41 PM
  • Thank you for answering my post

    For rowIndex = 0 To .RowCount - 1
     If Not rowIndex = .NewRowIndex Then
      .Rows(rowIndex).Cells("job_status").Value = .Rows(rowIndex).Cells("job_status").Value.ToString.Trim
     End If
    Next

    same problem




    • Edited by Bre-x Friday, November 8, 2019 10:56 PM
    Friday, November 8, 2019 10:55 PM
  • Thank you for answering my post

    For rowIndex = 0 To .RowCount - 1
     If Not rowIndex = .NewRowIndex Then
      .Rows(rowIndex).Cells("job_status").Value = .Rows(rowIndex).Cells("job_status").Value.ToString.Trim
     End If
    Next

    same problem




    Hi

    I didn't offer the NewRow Index check as a solution for you, as I said, I assume you have AllowUserToAddRows set to False and that I could not get the StackOverflow exception you reported.

    All in all, I would say that your problem lies elsewhere.


    Regards Les, Livingston, Scotland

    Friday, November 8, 2019 11:15 PM
  • Thank you,

    I agree with you. Either the SQL server is no passing the proper error or VS is reporting something else. Again thank you for answering my post.

    I have never taken any formal training. I am self taught.

    False or True does not make any difference
    • Edited by Bre-x Friday, November 8, 2019 11:57 PM
    Friday, November 8, 2019 11:51 PM
  • Thank you,

    I agree with you. Either the SQL server is no passing the proper error or VS is reporting something else. Again thank you for answering my post.

    I have never taken any formal training. I am self taught.

    False or True does not make any difference

    Hi

    I am guessing here, but I suspoect you have not encountered the NullReferenceException because it never reaches the point where rowIndex = .NewRowIndex due to your other exception.


    Regards Les, Livingston, Scotland

    Saturday, November 9, 2019 12:06 AM
  • Maybe make sure that this code is not called from some event handlers that are related to change notifications, and the changes made by this loop do not imply some recursive infinite notifications in this or other parts.

    Give details about the location of your code. When the exception is detected, investigate the call-stack to view the callers.


    • Edited by Viorel_MVP Saturday, November 9, 2019 9:00 AM
    • Marked as answer by Bre-x Sunday, November 10, 2019 6:18 PM
    Saturday, November 9, 2019 9:00 AM
  • Thank you for answering my post

    That was the problem

    RemoveHandler dg_main.CellValueChanged, AddressOf dg_main_CellValueChanged

    Sunday, November 10, 2019 6:18 PM
  • Thank you
    Tuesday, November 12, 2019 5:09 PM