Friday, September 5, 2014

Object cannot be cast from DBNull to other types


Hi all, now I wanna share something simple about programming in C#. Yesterday, I got an error when using result of stored procedure in C#. The error is "Object cannot be cast from DBNull to other types.". It has been caused of result that produced in stored procedure is null or empty. So I give a condition while result is null, but still getting error. After searching, I just realized that giving condition betweet variable and object is different. And this is the solution of my error:

object[] ret = PXDatabase.Execute("xsREGetTax", param1, param2, param3);

if (ret[0] != DBNull.Value)
           taxRate = Convert.ToDouble(ret[0]);
           taxRate = 0;

As you can see, I execute Stored Procedure named xsREGetTax and save the result in object named ret. When ret is not null or empty, I save the value in taxRate variable.

Hope this article helps you.

Spread The Love, Share Our Article

Related Posts

No Response to "Object cannot be cast from DBNull to other types"

Post a Comment