Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[Error] Specified cast is not valid. 1

Status
Not open for further replies.

coospaa

Programmer
May 1, 2003
33
SG
I wrote a class representing Product Model <ProductInfo.cs>, in which it contains this line in the constructor:
...
this._unitPrice = (values[6]==DBNull.Value)?0:(float)values[6]; // ***
...

Then, i wrote this piece of code:
Product bll = new Product(); // get the Business Logic Layer
ProductInfo productDetails = bll.getProductByID(ProductID);

Then this error come out:
Exception Details: System.InvalidCastException: Specified cast is not valid.
And the source error pointed to *** line.

Can anyone tell me what should I do? Thanks in advance:)

 
The error is caused because you are trying to implicitly cast the object values[6] as a float where no implicit conversion exists. The only types with implicit conversion to float are: char, sbyte, byte, short, ushort, int, unit, long and ulong. Without knowing more of the type you are trying to convert it is tricky to know exactly which conversion to use but if it is a monetary value you may be better off using decimal instead which if there is no implicit conversion for, then you should be able to use the Convert class' static ToDecimal method like this
Code:
this._unitPrice = (values[6]==DBNull.Value)?0:Convert.ToDecimal(values[6]);
Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top