When the DataGridView got populated I want to get All ItemId and create a public sub that can sum all the quantiy in 7 tables and required an ItemId.
This Code is the Public Sub
For Each row As DataGridViewRow In dgvStocks.Rows
For index As Integer = 0 To dgvStocks.RowCount - 1
itemid = CStr(row.Cells(0).FormattedValue)
With cmd
.Connection = con
.CommandText = cmdString
'string query that can sum all the qty from another tables that required all ItemId
.Parameters.Clear()
.Parameters.AddWithValue("@ItemId", itemid)
End With
dgvStocks.Rows(index).Cells(3).Value = Convert.ToString(cmd.ExecuteScalar())
Next
Next
Now I got an error Object reference not set to an instance of an object.
What causing this error ? How can I run this sub without that error and make it work ?