1

I have this query, and it should return double (that is in database as Decimal(10,2)):

public products SearchPrice(string nmproduct)
        {
            string hql = "select x.custo from products x where x.nmproduct = ?";
            IQuery query = session.CreateQuery(hql);
            query.SetParameter(0, nmproduct);
            return query.UniqueResult<products>();
        }

I want to get the price in database.

The exception is:

Unable to cast object of type 'System.Decimal' to type 'Administrative.Models.products'.

Najera
  • 2,869
  • 3
  • 28
  • 52
H. Eberhardt
  • 79
  • 10

1 Answers1

0

You are casting a decimal value to your entity, you need to use:

query.UniqueResult<decimal>()

When you represent money, you need to use decimal instead double for not to lose accuracy, read here.

Community
  • 1
  • 1
Najera
  • 2,869
  • 3
  • 28
  • 52
  • Thank you, it solved the problem. Just needed to change to decimal. `public decimal SearchPrice(string nmproduct) { string hql = "select x.custo from products x where x.nmproduct = ?"; IQuery query = session.CreateQuery(hql); query.SetParameter(0, nmproduct); return query.UniqueResult(); }` – H. Eberhardt Jan 31 '16 at 20:33