Object cannot be cast from DBNull to other types - thoriqaziz.com

thoriqaziz.com

Do your hobby

Object cannot be cast from DBNull to other types

Share This
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]);
else
           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.

No comments:

Post a Comment

Pages