当填充DataGridView时,我想得到All ItemId并创建一个公共子,它可以汇总7个表中的所有量化并需要一个ItemId。
本守则是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
现在我收到了错误Object reference not set to an instance of an object.
导致此错误的原因是什么?如何在没有该错误的情况下运行此子程序并使其正常工作?