Solved

"System.NullReferenceException was unhandled" error after last valid row.

Posted on 2016-08-19
10
41 Views
Last Modified: 2016-08-19
Hi, I'm using Visual Studio 2010 C#. I have a DataGridView on one of my Forms that is connected to SQL database table. That part works. I'm also using the dataGridView1_RowEnter Event to populate the selected information into different Textboxes as I scroll down the grid. That works until I get past the last row.

As soon as I go beyond the length of the table (past the last row) and enter the first NULL row, I get the following error.  

ERROR: >> System.NullReferenceException was unhandled

Why is the  "if (e.RowIndex >= 0)" logic statement not preventing the error and how do I solve this problem?

private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
            if (e.RowIndex >= 0)
            {                
                textBox1.Text = row.Cells[1].Value.ToString();
                textBox2.Text = row.Cells[2].Value.ToString();
                textBox3.Text = row.Cells[3].Value.ToString();
                textBox4.Text = row.Cells[4].Value.ToString();
            }

Open in new window

0
Comment
Question by:Mr_Fulano
  • 4
  • 3
  • 3
10 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41762930
>>Why is the  "if (e.RowIndex >= 0)" logic statement not preventing the error and how do I solve this problem?

What is the value of e.RowIndex when it fails.  You are apparently assuming it is less than zero.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 41762934
I suspect you want something like:

if (!row.IsNewRow)   //if (e.RowIndex >= 0)


https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.isnewrow(v=vs.110).aspx
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41762939
This statement needs to be in the if statement, first statement inside if statement.

 DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41762962
@Fernando  
Why does that assignment need to be inside the if block?
0
 

Author Comment

by:Mr_Fulano
ID: 41763034
Hi Fernando, I had it that way and retried your suggestion just to make sure, but that still fails.
0
 

Author Comment

by:Mr_Fulano
ID: 41763036
Hi Andy, yes...your suggestion works. The last row is negative and keeps increasing each time I enter it (i.e. -1. -2. -3...etc).

Is there a way to not show that last row at all?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41763052
Hi Andy, I was thinking that if this was happening when he passed the last row it had a -1 as the index and therefore would be an out of bounds index or no row. Was not near a computer to test and wanted him to have something to test for the moment.
0
 

Author Comment

by:Mr_Fulano
ID: 41763055
Thank you both...!!!
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41763059
Hi Fernando, I could understand why you suggested that for some of the other possible events but was puzzled why for the RowEnter event.  Thanks for clarifying.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41763065
Not a problem Andy, have a great day.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question