locked
Object cannot be cast from DBNull to other types. RRS feed

  • Question

  • Hi,

     i am trying to multiply datagridview cells value. but when i execute program it show  2 errors

    1. " Operator '*' is not defined for type Dbnull and for type DbNull"

    2. Object cannot be cast from DBNull to other types.

    why these  errors are occurring. Plz help to solve this. here is my code


    Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

    If Me.DataGridView1.Rows.Count > 1 Then For r As Integer = 0 To +DataGridView1.CurrentRow.Index Dim per As Double = Convert.ToDouble(Me.DataGridView1.Rows(r).Cells(5).Value) * Convert.ToDouble(Me.DataGridView1.Rows(r).Cells(6).Value / 100) Me.DataGridView1.Rows(r).Cells(7).Value = Convert.ToDouble(Me.DataGridView1.Rows(r).Cells(5).Value) - per Next End If







    • Edited by kachani_anil Tuesday, September 18, 2018 7:09 PM
    Tuesday, September 18, 2018 7:06 PM

All replies

  • In the future do not double post as if done it will be merged to the original post.

    • You should not be working against cells in the first place, set up a data source for the DataGridView.
    • Use Option Strict On (as mentioned in a reply in your first/duplicate post) e.g. Cells.Value is an Object not a Double.
    • Before doing mathing check for null value e.g. "Is DBNull"
    • Consider, if using a DataTable look at Expression columns.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, September 18, 2018 7:22 PM
  • Hi

    Here is a simple example to show the use of a DataTable and a DataGridView rather than dealing only with a DGV. This example sets up a DataTable and adds columns and some dummy test data. Then adds an Expression to the "Calc" column. With this approach, when the user enters data, or edits the data in the DGV, then the Calc column will be automatically updated. (NOTE: there is no exception handling included - as you will see if you try to edit the Calc column in the DGV). This is a stand alone test example and can be tested in a new Project.

    ' Form1 with default BLANK
    ' DataGridView1
    Option Strict On
    Option Explicit On
    Public Class Form1
      ' declare a DataTable
      Dim dt As New DataTable
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' set columns for DataTable
        ' and add some example data
        With dt
          .Columns.Add("Date", GetType(DateTime))
          .Columns.Add("Integer", GetType(Integer))
          .Columns.Add("Double", GetType(Double))
          .Columns.Add("Calc", GetType(Double))
    
          For i As Integer = 1 To 20
            .Rows.Add(Now.AddDays(i), i, 1 / i)
          Next
        End With
        ' assign the DataTable as the
        ' DataSource for the DataGridView
        DataGridView1.DataSource = dt
    
        ' set up column "Calc" to be the
        ' sum of columnn "Integer" and
        ' column "Double"
        dt.Columns("Calc").Expression = "[Integer]+[Double]"
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Gtripodi Wednesday, September 19, 2018 1:44 AM
    Tuesday, September 18, 2018 8:18 PM
  • Take leshay's advice, it's real good advice.

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

    Wednesday, September 19, 2018 1:44 AM
  • Thanks for reply.

    I tried every ways to solve this problem but unable to resolve it. so i change some code but still problem is same. plz look this and suggest what next i can do.

    Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    
    
    
            If Me.DataGridView1.Rows.Count > 1 Then
    
                With Me.DataGridView1.Rows(e.RowIndex)
                    Dim per As Double = Convert.ToDouble(.Cells(5).Value) * Convert.ToDouble(.Cells(6).Value) / 100
    
                    .Cells(7).Value = Convert.ToDouble(.Cells(5).Value) - per
                End With
    
            End If
    
    
    
    
    
        End Sub
    
    
    Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            If Me.DataGridView1.CurrentCell.ColumnIndex = 5 Or Me.DataGridView1.CurrentCell.ColumnIndex = 6 Or Me.DataGridView1.CurrentCell.ColumnIndex = 8 Or Me.DataGridView1.CurrentCell.ColumnIndex = 10 Then
                Dim txtNum As TextBox = CType(e.Control, TextBox)
                RemoveHandler txtNum.KeyPress, AddressOf keypress_gridviewcell
                AddHandler txtNum.KeyPress, AddressOf keypress_gridviewcell
            Else
                Dim ctrl As Control = e.Control
                If (ctrl.GetType) Is GetType(TextBox) Then
                    Dim txtNum As TextBox = CType(e.Control, TextBox)
                    RemoveHandler txtNum.KeyPress, AddressOf keypress_gridviewcell
                End If
               
            End If
        End Sub

    here is clsfunction code ..

    Public Class clsfunction
        Public con As New SqlClient.SqlConnection(My.Settings.AgrasenConnectionString)
        Public cmd As New SqlClient.SqlCommand
        Dim Numstr As String = "0123456789.-/"
        Public Sub keyPressNumeric(ByVal pflag As Boolean, ByVal e As KeyPressEventArgs)
            If pflag = True Then
                If Not IsNumeric(e.KeyChar.ToString) And Not e.KeyChar = Chr(Keys.Back) Then
                    e.Handled = True
                End If
            ElseIf pflag = False Then
                If IsNumeric(e.KeyChar.ToString) Then e.Handled = True
            End If
    
        End Sub
        Public Sub KeyPressNumeric(ByVal e As KeyPressEventArgs)
            If Not IsNumeric(e.KeyChar.ToString) And Not e.KeyChar = Chr(Keys.Back) Then
                e.Handled = True
    
            End If
        End Sub
    
        Public Sub KeyPressNonNumeric(ByVal e As KeyPressEventArgs)
            If IsNumeric(e.KeyChar.ToString) Then e.Handled = True
    
    
        End Sub
    End Class
    

    Please suggest me where i m wrong also make correct changes on my code

    Wednesday, September 19, 2018 2:43 AM
  • Thanks for your suggestion. but my DGV is bounded to Sql server database.

    how can i use calculation on DGV now which is already connected to database .

    Wednesday, September 19, 2018 2:46 AM
  • Unless I am missing something, or your not supplying all the information, this class is doing absolutly nothing.

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

    Wednesday, September 19, 2018 3:00 AM
  • Thanks for reply.

    I tried every ways to solve this problem but unable to resolve it. so i change some code but still problem is same. plz look this and suggest what next i can do.

    Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    
    
    
            If Me.DataGridView1.Rows.Count > 1 Then
    
                With Me.DataGridView1.Rows(e.RowIndex)
                    Dim per As Double = Convert.ToDouble(.Cells(5).Value) * Convert.ToDouble(.Cells(6).Value) / 100
    
                    .Cells(7).Value = Convert.ToDouble(.Cells(5).Value) - per
                End With
    
            End If
    
    
    
    
    
        End Sub
    
    
    Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            If Me.DataGridView1.CurrentCell.ColumnIndex = 5 Or Me.DataGridView1.CurrentCell.ColumnIndex = 6 Or Me.DataGridView1.CurrentCell.ColumnIndex = 8 Or Me.DataGridView1.CurrentCell.ColumnIndex = 10 Then
                Dim txtNum As TextBox = CType(e.Control, TextBox)
                RemoveHandler txtNum.KeyPress, AddressOf keypress_gridviewcell
                AddHandler txtNum.KeyPress, AddressOf keypress_gridviewcell
            Else
                Dim ctrl As Control = e.Control
                If (ctrl.GetType) Is GetType(TextBox) Then
                    Dim txtNum As TextBox = CType(e.Control, TextBox)
                    RemoveHandler txtNum.KeyPress, AddressOf keypress_gridviewcell
                End If
               
            End If
        End Sub

    here is clsfunction code ..

    Public Class clsfunction
        Public con As New SqlClient.SqlConnection(My.Settings.AgrasenConnectionString)
        Public cmd As New SqlClient.SqlCommand
        Dim Numstr As String = "0123456789.-/"
        Public Sub keyPressNumeric(ByVal pflag As Boolean, ByVal e As KeyPressEventArgs)
            If pflag = True Then
                If Not IsNumeric(e.KeyChar.ToString) And Not e.KeyChar = Chr(Keys.Back) Then
                    e.Handled = True
                End If
            ElseIf pflag = False Then
                If IsNumeric(e.KeyChar.ToString) Then e.Handled = True
            End If
    
        End Sub
        Public Sub KeyPressNumeric(ByVal e As KeyPressEventArgs)
            If Not IsNumeric(e.KeyChar.ToString) And Not e.KeyChar = Chr(Keys.Back) Then
                e.Handled = True
    
            End If
        End Sub
    
        Public Sub KeyPressNonNumeric(ByVal e As KeyPressEventArgs)
            If IsNumeric(e.KeyChar.ToString) Then e.Handled = True
    
    
        End Sub
    End Class

    Please suggest me where i m wrong also make correct changes on my code

    Hi,

    Null values cannot be converted to double types. You should first check if the value is empty.

    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.

    Wednesday, September 19, 2018 3:15 AM
  • Alex, we went down that road in his previous post, his method to fix it turned out to be remove it altogether and hope it goes away

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

    Wednesday, September 19, 2018 12:46 PM