0

I am trying to compare the value of a row esmcost with psmcost of table wsmtbl but esmcost cost value is null due to which I am getting the following error:

Operator '<>' is not defined for type 'DBNull' and type 'DBNull'

Is there any way to change the null value to zero?

This is the C# code

ds.Tables["wsmtbl"].Rows[0]["esmcost"] <> ds.Tables["wsmtbl"].Rows[0]["psmcost"]
4
  • 1
    What is "<>"? What are you trying to do in this line of code, do you mean "!="? Commented Jan 22, 2018 at 13:07
  • yes @IrishChieftain Commented Jan 23, 2018 at 2:45
  • What types are ermcost and psmcost in the DB? Commented Jan 23, 2018 at 3:41
  • money with nullable Commented Jan 23, 2018 at 7:56

2 Answers 2

1

Try the following.

Decimal esmcost;
if (ds.Tables["wsmtbl"].Rows[0]["esmcost"] == DBNull.Value)
    esmcost = 0.00;
else
    esmcost = Convert.ToDecimal(ds.Tables["wsmtbl"].Rows[0]["esmcost"]);

Decimal psmcost;
if (ds.Tables["wsmtbl"].Rows[0]["psmcost"] == DBNull.Value)
    psmcost = 0.00;
else
    psmcost = Convert.ToDecimal(ds.Tables["wsmtbl"].Rows[0]["psmcost"]);

if (esmcost != psmcost)
{
    ...
}

One could use the ternary operator syntax but I chose the above for readability reasons. For example:

Decimal esmcost = ds.Tables["wsmtbl"].Rows[0]["esmcost"]
    == DBNull.Value ? 0.00 : Convert.ToDecimal(ds.Tables["wsmtbl"].Rows[0]["esmcost"]);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this,

if(ds.Tables["wsmtbl"].Rows[0]["esmcost"] == DBNull.Value)
{
   ds.Tables["wsmtbl"].Rows[0]["esmcost"] = 0;
}

 ds.Tables["wsmtbl"].Rows[0]["esmcost"] != ds.Tables["wsmtbl"].Rows[0]["psmcost"];

1 Comment

You assume 0 and NULL are the same thing... if that were the case, surely the column would have been NOT NULL anyway and we would never have come to this...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.