0
votes

im working to show the data in my datagrid,and show the total of items brought in textbox2,but this error "Operator '+' is not defined for type 'Decimal' and type 'DBNull'." what is the problem? please help me i need to finish this so badly. :(

Private Sub dailySales_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con.Connect()
    DataGridView1.DataSource = dtb
    dtb.Columns.Add("Transaction Code")
    dtb.Columns.Add("Transaction Date ")
    dtb.Columns.Add("Item Code")
    dtb.Columns.Add("Item")
    dtb.Columns.Add("Description")
    dtb.Columns.Add("Quantity")
    dtb.Columns.Add("Price")
    dtb.Columns.Add("Total")
    display()
    con.Close()
    total()
    TextBox1.Text = DataGridView1.RowCount()
    Me.DataGridView1.DefaultCellStyle.Font = New Font("Cambria", 10)
    Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font = New Font("Cambria", 12)
    Me.DataGridView1.DefaultCellStyle.SelectionBackColor = Color.FromArgb(129, 207, 224)
    Dim i As Integer
    For i = 0 To DataGridView1.Columns.Count - 1
        DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic
    Next
End Sub
Sub total()
    Dim sum As Decimal = 0
    For x = 0 To DataGridView1.Rows.Count - 1
        sum =sum +  DataGridView1.Rows(x).Cells(7).Value
    Next
    TextBox2.Text = sum
End Sub
Sub display()
    con.Connect()
    Label6.Text = Date.Now.ToString("dd MMMM yyyy")
    Dim da = Label6.Text
    Dim sc = " where lower(tns_date) like lower('%" & da & "%') "
    Dim sql = "select * from tns " & sc & " order by tns_code desc"
    odr = con.Retrieve(sql)
    dtb.Clear()
    While (odr.Read)
        Dim ic = odr.GetOracleValue(0)
        Dim itn = odr.GetOracleValue(1)
        Dim de = odr.GetOracleValue(2)
        Dim ca = odr.GetOracleValue(3)
        Dim pr = odr.GetOracleValue(4)
        Dim st = odr.GetOracleValue(5)
        Dim sst = odr.GetOracleValue(6)
        dtb.Rows.Add(ic, itn, de, ca, pr, st, sst)
    End While
    con.Close()

End Sub
1
One of the column 7 values is null? Surely you can test for that and not do the addition; or change the query (you haven't shown) to use nvl() or coalesce() to return zero instead of null?Alex Poole
@AlexPoole no null in my columns.how to do the test?Santiago Nicholas Mendoza
OR test IsDBnull(DataGridView1.Rows(x).Cells(7).Value) and skip if trueTrevor_G
The error suggests there is a null value. We obviously can't see your data or query but it seems likely there is. If you've recently updated values from null to not-null, maybe you haven't committed the changes? Testing or otherwise handling nulls would be sensible anyway, if the column or expression is nullable.Alex Poole
@Trevor how to do that?sorry im new in doing that testSantiago Nicholas Mendoza

1 Answers

0
votes

Test like this.

Sub total()
    Dim sum As Decimal = 0
    For x = 0 To DataGridView1.Rows.Count - 1
        If Not isDBNull(DataGridView1.Rows(x).Cells(7).Value) then sum =sum +  DataGridView1.Rows(x).Cells(7).Value
    Next
    TextBox2.Text = sum
End Sub