Click here to Skip to main content
13,049,233 members (63,778 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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
Updated 17-Jun-13 8:21am
Ron Beyer65.1K
richcb 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 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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 17 Jun 2013
Copyright © CodeProject, 1999-2017
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