Solved

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

Posted on 2016-08-19
10
29 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 62

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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 62

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 62

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now