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