Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
My Code is below
private void label94_Click(object sender, EventArgs e)
 {
     float total = 0;
     int counter;
     for (counter = 0; counter < (dataGridView2.Rows.Count); counter++)
      {
        if (dataGridView2.Rows[counter].Cells["Price"].Value != null)
          {
             if (dataGridView2.Rows[counter].Cells["Price"].Value.ToString().Length != null)               {
                 total += float.Parse(dataGridView2.Rows[counter].Cells["Price"].Value.ToString());
                    }
                }
            }
            textBox1.Text = total.ToString();
        }
this code is running properly but showing an error by holding on the cursor on bold text code.
Error Message :- The result of the expression is always 'true' since a value of type 'int' is never equal to 'null' of type 'int?'.
I don't know why this message is showing. Please someone help.
Posted 17-Jun-13 9:17am
Edited 17-Jun-13 9:21am
Ron Beyer58.5K
v3
Comments
richcb at 17-Jun-13 14:20pm
   
So it is not working properly. You can't have working code with errors. Change null to zero(0). The .Length property is looking to be compared to an int datatype and null is not.
ThePhantomUpvoter at 17-Jun-13 14:23pm
   
It means exactly what it says. The length of a string (int) can never be = null so your if statement is always true.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

.ToString() returns a string.
 
String.Length returns an integer.
 
Value types will never equal null. So its like checking if 0 == null which it never will.
 
If you want to check if the column is null, you have to do it WAY before you call .ToString, since a null value in the field will cause a null reference exception when it tries to call .ToString on a null.
 
if (dataGridView2.Rows[counter].Cells["Price"].Value != null &&
    dataGridView2.Rows[counter].Cells["Price"].Value.ToString().Length > 0)
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 17-Jun-13 19:00pm
   
Sure,. a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 Afzaal Ahmad Zeeshan 159
4 BillWoodruff 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 17 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100