none
Operator '*' is not defined for type dbnull RRS feed

  • Question

  • Hii, i am tring to multiply datagridview cells value. but when i execute prograam it show " Operator '*' is not defined for type Dbnull and for type DbNull"

    here are my code

     Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
           
            For row As Integer = 0 To DataGridView1.Rows.Count - 1
                If IsDBNull(DataGridView1.Rows(row).Cells(7).Value) Then
                    Console.WriteLine("Null detected")
                Else
                    Console.WriteLine(DataGridView1.Rows(row).Cells(7).Value)
                    Me.DataGridView1(7, e.RowIndex).Value = Me.DataGridView1(5, e.RowIndex).Value * Me.DataGridView1(6, e.RowIndex).Value
                    'Me.DataGridView1(7, e.RowIndex).Value = purchase
    
                End If
            Next
    
            
        End Sub

    can anyone help to solve this problem.

    Thanks & Regards

    Monday, September 17, 2018 6:14 PM

All replies

  • Try

            For row As Integer = 0 To DataGridView1.Rows.Count - 1
                If DataGridView1.Rows(row).Cells(7).Value IsDbNull.Value Then
                    Console.WriteLine("Null detected")
                Else
                    Console.WriteLine(DataGridView1.Rows(row).Cells(7).Value)
                    Me.DataGridView1(7, e.RowIndex).Value = Me.DataGridView1(5, e.RowIndex).Value * Me.DataGridView1(6, e.RowIndex).Value
                    'Me.DataGridView1(7, e.RowIndex).Value = purchase
    
                End If
            Next
    
            
        End Sub

    also you may wish to turn option strict on. You should be casting or converting your values as numeric type (IE: int32, double, etc)


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Monday, September 17, 2018 6:46 PM
  • Hi,

    modify the code:

       Me.DataGridView1(7, e.RowIndex).Value =Convert.ToDouble( Me.DataGridView1(5, e.RowIndex).Value) * Convert.ToDouble(Me.DataGridView1(6, e.RowIndex).Value)

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 18, 2018 1:23 AM
  • It is probably better practice to use datatable expression for this type of calculation, at which point you can scrap this entire code

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Tuesday, September 18, 2018 12:55 PM