Hi everybody, its Leo again

I'm having some problems calculating the values for two diferent fiels of the datatable. it return " Object reference not set to an instance of an object"

this is How I feed the datatable

Dim daProd As New SqlDataAdapter
        Dim conProd As New SqlConnection
        conProd = Connect()
        Dim cmdProd, cmdSave As New SqlCommand
        Dim linha As DataRow
        Dim ds As New DataSet
        conProd.CreateCommand()
        conProd.Open()
        cmdProd.Connection = conProd
        cmdProd.CommandText = "SELECT * FROM PRODUCT WHERE CODE='" & Trim(txtProdCode.Text) & "'"
        daProd.SelectCommand = cmdProd
        daProd.Fill(ds, "FILL")

        linha = dt.NewRow
        linha("Code") = ds.Tables("FILL").Rows(0).Item("CODE").ToString
        linha("Description") = ds.Tables("FILL").Rows(0).Item("PRODUCT").ToString
        linha("Qtty") = 1
        linha("Price") = ds.Tables("FILL").Rows(0).Item("PRICE").ToString

        total = Val(linha("P_Unitario")) * Val(linha("Qtde"))

        linha("SubTotal") = total

so far so good. The variables linha and total are declared below the Public Class of the form, where linha as datarow, and total as integer.
My probles is on the Datagrid cellchange event

total = Val(linha("P_Unitario")) * Val(linha("Qtde"))

        Dim sum As Double = 0
        Dim i As Integer = 0
        For i = 0 To dgSales.RowCount - 1
            sum += dgSales.Rows(i).Cells("SubTotal").Value

        Next

        lblTotal.Text = dt.Compute("sum(SubTotal)", String.Empty)

the error comes from this line.

total = Val(linha("P_Unitario")) * Val(linha("Qtde"))

I have to mention that I create the fields on form load event

dt.Columns.Add(New DataColumn("Codigo", GetType(String)))
        dt.Columns.Add(New DataColumn("Descriçao", GetType(String)))
        dt.Columns.Add(New DataColumn("Qtde", GetType(Integer)))
        dt.Columns.Add(New DataColumn("P_Unitario", GetType(Integer)))
        dt.Columns.Add(New DataColumn("SubTotal", GetType(Double)))
        
        dgv.DataSource = dt

hope can help
Thanks in advance
Leo

I may be wrong, but your problems (2) lie with linha
When you are creating the datatable you are declaring linha, which gives it a scope within the sub. You are getting the error because you are trying to use linha outside of the sub that it belongs/exists. I may be wrong, but given the code you've shared its a valid guess.

Second thing about this is the logic you are following. The total = Val(linha("P_Unitario")) * Val(linha("Qtde")) statement works when you are creating the data because you are building only one record at a time. So your statement only works until the record is inserted in your datatable (or as I am guessing your DataViewGrid).
You will need to change this to

Dim sum As Double = 0
        Dim i As Integer = 0
        For i = 0 To dgSales.RowCount - 1
           dgSales.Rows(i).Cells("SubTotal").value =  dgSales.Rows(i).Cells("P_Unitario").value *  dgSales.Rows(i).Cells("Qtde").value 
  sum += dgSales.Rows(i).Cells("SubTotal").Value

        Next

        lblTotal.Text = dt.Compute("sum(SubTotal)", String.Empty)

Tital i double type, so convert the value from dataRow to double (use Convert ot parse method).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.