Pregunta

I am trying to avoid database null values with 0 and if it is not null then get the original values.

but I am having some issues .

Here is my Sample Code:

    int Value = 0;
    for(int i = 0; i < tblValue.Rows.Count; i++)
    {
      if (tblValue.Rows[i][""]== DBNull.Value)//Here it always returns true.. even there are values at Position 1 and 
      {
         Value += 0;                        
      }
      else
      {
         Value += Convert.ToInt32(tblValue.Rows[i][""]);
      }

   }

Any Idea?

Am I checking DBNUll Value the wrong way?

¿Fue útil?

Solución

Yes. Try (DBNull.Value.Equals(tblValue.Rows[i][""]);

http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx

Otros consejos

Try this:

int Value = 0;
for(int i = 0; i < tblValue.Rows.Count; i++)
{
    if (!IsDBNull(tblValue.Rows[i][""])) 
    {
        Value += Convert.ToInt32(tblValue.Rows[i][""]);
    }
}

This only increments the Value variable if the database value is NOT NULL.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top