Click here to Skip to main content
11,713,864 members (83,932 online)
Rate this: bad
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 8:17am
Edited 17-Jun-13 8:21am
Ron Beyer60.7K
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
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)
Sergey Alexandrovich Kryukov at 17-Jun-13 19:00pm
Sure,. a 5.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 430
1 Maciej Los 229
2 Richard MacCutchan 210
3 CHill60 175
4 OriginalGriff 175
0 Sergey Alexandrovich Kryukov 365
1 CHill60 335
2 OriginalGriff 245
3 Andy Lanng 173
4 Richard MacCutchan 150

Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 17 Jun 2013
Copyright © CodeProject, 1999-2015
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