0

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 ?

Meyka Jograt
  • 314
  • 2
  • 4
  • 19
  • You are setting something to nothing. Walk the debugger. – Drew Jul 29 '16 at 06:29
  • Whats wrong with my code? – Meyka Jograt Jul 29 '16 at 07:24
  • For one thing you are not stating on which line it dies, or are you? You have at your fingertips the best development environment on Earth. Run the debugger. – Drew Jul 29 '16 at 07:25
  • Writing code like this at the moment is not helping you to debug things so well either: `dgvStocks.Rows(index).Cells(3).Value = Convert.ToString(cmd.ExecuteScalar())` – Drew Jul 29 '16 at 07:29
  • `dgvStocks.Rows(index).Cells(3).Value = Convert.ToString(cmd.ExecuteScalar())` or `itemid = CStr(row.Cells(0).FormattedValue)` im not sure i tried debugging it without the try catch it just stop the solution without saying any errors – Meyka Jograt Jul 29 '16 at 07:33
  • You could set a breakpoint above your assumed area of problems and keep doing Step Into. I am not ignoring you if I don't respond anymore. It is 4am and I need some sleep :p – Drew Jul 29 '16 at 07:46

0 Answers0