Hi
I have a datagrid with 2 columns (Scores(Int) index 0 and Comment(varchar) index 1)
Comment columnvalue depends on score columnvalue.

This code works well in my cellvaluechanged event

If e.ColumnIndex = 0  Then
            If Not IsDBNull(Me.dgvStudDisc.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Then
                If Me.dgvStudDisc.Rows(e.RowIndex).Cells(e.ColumnIndex).Value >= 0 And
                    Me.dgvStudDisc.Rows(e.RowIndex).Cells(e.ColumnIndex).Value <= 5 Then
                    Me.dgvStudDisc.Rows(e.RowIndex).Cells(1).Value = "Very Poor"
                Else
                    Me.dgvStudDisc.Rows(e.RowIndex).Cells(1).Value = DBNull.Value
                End If
            End If
 End If

PROBLEM: when I enter a value in score column and want to delete it I get the above exception.
Can someone help me modify the code to handle this problem?
gbhs

'score' column hold integers
'comment' column hold text i.e. varchar (all in SQL database)
when I enter an integer in score column and then delete it I get the exception 'Input string was not in a correct format'

Adding the code line in else clause has not solved it

If e.ColumnIndex = 0 Then
    If Not IsDBNull(Me.dgvStudDisc.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Then
    If Me.dgvStudDisc.Rows(e.RowIndex).Cells(0).Value >= 0 And
    Me.dgvStudDisc.Rows(e.RowIndex).Cells(0).Value <= 5 Then
    Me.dgvStudDisc.Rows(e.RowIndex).Cells(1).Value = "Very Poor"
    Else
    Me.dgvStudDisc.Rows(e.RowIndex).Cells(1).Value = DBNull.Value
    Me.dgvStudDisc.Rows(e.RowIndex).Cells(0).Value = DBNull.Value 'Adding this line doesnot solve it
    End If
    End If
    End If

Why did you assume that DBNull is a valid value when accessing the information?
For example, perhaps it's valid to set the value to DBNull, but it's obviously not the correct format if you display it.

So, you could check if a column is NULL before taking action on it (IsDBNull()), or you could set the value to something valid in the first place.

Also, it might help if you add some try { } catch { }, as you are presently unable to identify the exact line of code which is causing you problems.
This tells me that your issue is not captured by the debugger in real time (Setting the value to DBNull does not cause the problem)

That means that your program crashes at a later time, in code you do not display here.

Edited 3 Years Ago by Unimportant

I have modified the code as below

Thanks

With this code I get no error yet when I delete a score, the corresponding comment is not deleted.
How can I get this done?

 Private Sub dgvStudDisc_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvStudDisc.CellValueChanged
             Try
                If e.ColumnIndex = 0 Then

                    If Not IsDBNull(Me.dgvStudDisc.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Then
                        If CStr(Me.dgvStudDisc.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString) >= 0 And
                            CStr(Me.dgvStudDisc.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString) <= 5 Then
                            Me.dgvStudDisc.Rows(e.RowIndex).Cells(1).Value = "Very Poor"
                        End If
                    End If
                End If
            Catch ex As Exception

            End Try
        End Sub

i don't know what are you try to make, but if i were you, i will loop it.

maybe something like this :

Try
            For i As Integer = 0 To dgvStudDisc.Rows.Count - 1
                If dgvStudDisc.Rows(i).Cells(0).Value >= 0 And dgvStudDisc.Rows(i).Cells(0).Value <= 5 Then
                    dgvStudDisc.Rows(i).Cells(1).Value = "Very Poor"
                Else
                    dgvStudDisc.Rows(i).Cells(1).Value = DBNull.Value
                End If
            Next
Catch ex As Exception
            MsgBox(ex.Message)
End Try

Edited 3 Years Ago by artemix22

It will probably do what you expect if you get rid of calls to ToString(), because that value is an integer right?

With your sample code above I get the following exception in line 6

An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll
What can I do in this case
thanks in advance

line 6, change into like this :

dgvStudDisc.Rows(i).Cells(1).Value = Nothing

PS : is there any data came from database? if yes, which one? column 1 or column 2?

Edited 3 Years Ago by artemix22

I have changed it and still get exception.

An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

Yes I have sql table that I want to store these values.

column 1 is called 'Scores' of type int      (it is cells(0) in datagrid)
Column 2 is called 'Comment'of type varchar  (it is cells(1) in datagrid)

so far, here is my test with your problem, but i use MySQL to do this..

i create table with 2 columns

  1. column 1, integer type
  2. column 2, varchar type

i use this code below to handle datagridview cellvaluechanged, insert and load :

Imports MySql.Data.MySqlClient
Public Class Form1

    Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        Try
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                If DataGridView1.Rows(i).Cells(0).Value >= 0 And DataGridView1.Rows(i).Cells(0).Value <= 5 Then
                    DataGridView1.Rows(i).Cells(1).Value = "Very Poor"
                Else
                    DataGridView1.Rows(i).Cells(1).Value = DBNull.Value
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                If DataGridView1.Rows(i).Cells(0).Value = Nothing Then
                    DataGridView1.Rows(i).Cells(0).Value = 0
                    Using conn As New MySqlConnection("MY CONNECTION STRING")
                        conn.Open()
                        Dim command As New MySqlCommand("insert into MY_TABLE values ('" & DataGridView1.Rows(i).Cells(0).Value & "', '" & DataGridView1.Rows(i).Cells(1).Value & "')", conn)
                        command.ExecuteNonQuery()
                        command.Dispose()
                        conn.Close()
                    End Using
                End If
            Next
            MsgBox("Saved!")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Try
            Using conn As New MySqlConnection("MY CONNECTION STRING")
                conn.Open()
                Dim command As New MySqlCommand("select * from MY_TABLE", conn)
                Dim adapter As New MySqlDataAdapter(command)
                Dim dt As New DataTable
                adapter.Fill(dt)
                DataGridView2.DataSource = dt
                adapter.Dispose()
                command.Dispose()
                conn.Close()
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

and here is the result :

RESULT

as you can see, after i insert that 4 data to database and load it back, it not give any exception

Edited 3 Years Ago by artemix22

artemix22
Thanks I will try it in my context and get to you.
Seen the output which is what I want.
If you delete 1 (for example) does the corresponding 'very poor' get deleted?
Well I will try the code and get to you via private message.
Thanks for the effort and time

Just tried the code which works.
delete from value and you get error 'operator >= not defined for dbnull and type integer'

Hi
After a whole day I finally got a way out which works like charm

Private Sub dgvStudDisc_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvStudDisc.CellValueChanged

        If (e.ColumnIndex = 0)  Then
            Dim cellData = dgvStudDisc.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
            Dim cellData1 = dgvStudDisc.Rows(e.RowIndex).Cells(1).Value
            If Not IsDBNull(cellData1) Then
                If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
                    cellData1 = DBNull.Value
                    dgvStudDisc.Rows(e.RowIndex).Cells(1).Value = cellData1
                    Exit Sub
                End If
            End If
        End If


    End Sub


Regards to all who helped
This question has already been answered. Start a new discussion instead.