麥克斯做個不宅的工程師

Maxi’s idiotic programming.

How To Cast ExecuteScalar Return Value

SqlCommand.ExecuteScalar(). I believe many of you use this handy method quite often.
But it is what it returns causing the trouble. This method returns an object.

If you try to cast that with (Type), this will almost always return you an invalid cast exception.
It is okay to use (Int32) or (string). But for (double), it needs to be done in another way.
Always use Convert.ToInt32() and Convert.ToDouble() for these situation.
This will save you a day.

June 19, 2009 , Friday - Posted by maxi326 | Programming | , , | No Comments Yet

No comments yet.

Leave a comment

You must be logged in to post a comment.