Solved

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

Posted on 2016-08-19
10
51 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
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!

 
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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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