Solved

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

Posted on 2016-08-19
10
46 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to sort columns in DataGridView 4 62
c#  FTP ftpwebrequest URI invalid 6 47
What are MicroServices? 4 67
Timeouts during development 3 28
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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