Reputation: 287
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?
Upvotes: 2
Views: 1037
Reputation: 34846
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
.
Upvotes: 1
Reputation: 176
Yes. Try (DBNull.Value.Equals(tblValue.Rows[i][""]);
http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx
Upvotes: 1