-1

I have database connected to my app in vb.net

if i want to get the value from any row or column i use this code and it's work fine

DataGridView1.Rows(1).Cells(1).Value.ToString

my problem is i have table contain 14 row and i want to make loop to check the first cell in every row if it's contain specific value to do other task

if i use the variable i in counter i got an error

in this code

    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click


Dim see As String

For i As Int32 = 0 To 14

        see = DataGridView1.Rows(i).Cells(1).Value.ToString

        If see = "FLOWRATE" Then

        TextBox11.Text = TextBox11.Text & see & "  "

        End If

Next

End Sub

when i press the button i got this error

An unhandled exception of type 'System.NullReferenceException' occurred in project1.exe

Additional information: Object reference not set to an instance of an object.
Haider
  • 13
  • 4
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Jul 21 '15 at 17:00

2 Answers2

2

I suspect you meant For i As Int32 = 0 To 13, not 14. 14 would imply there are 15 rows since things are zero-based.

You can simply this to not hard code the number of rows, rather do something like this:

For i As Int32 = 0 To DataGridView1.Rows.Count - 1
    'Handle each row
Next
vcsjones
  • 138,677
  • 31
  • 291
  • 286
0

This error is because i referentes row is not exists.

May be rows are indexed from 1 to 14 Or 0 to 13.

You hace to fit loop bounds