Faiz Shaikh
Faiz Shaikh

Reputation: 3

I have an error in my Api regarding convert type decimal

I get an error

Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)

I am getting an error on this Line of code

result = Convert.ToDecimal(row[0]);

This is my API

[HttpGet]
[Route("api/boq/getlatestrate/{id}/{id}")]
public decimal GetLatestRate(DateTime wefDate, long ProductId)
{
        var result = 1;
        var dsBoq = SqlHelper.ExecuteDataset(AppDatabaseConnection, CommandType.StoredProcedure, 0, "GetLatestRate", new SqlParameter("@wefDate", wefDate), new SqlParameter("@ProductId", ProductId));

        if (dsBoq.Tables[0].Rows.Count > 0)
        {
            DataRow row = dsBoq.Tables[0].Rows[0];               
            result = Convert.ToDecimal(row[0]);
        }

        return result;
}

This is my stored procedure:

ALTER PROCEDURE [dbo].[GetLatestRate]
    @wefDate DATE,
    @ProductId BIGINT
AS
    SELECT TOP 1 wefDate, RateDetails.Amount
    FROM Rate 
    INNER JOIN RateDetails ON Rate.Id = RateDetails.RateId
    WHERE wefDate <= @wefDate 
      AND RateDetails.ProductId = @ProductId
    ORDER BY wefDate DESC

This is result that comes after executing the stored procedure:

    execute GetLatestRate '2019-07-29', 4
wefDate         Amount
----------------------
2019-07-29      10.00

Upvotes: 0

Views: 125

Answers (2)

Shivani
Shivani

Reputation: 151

Use like below. So even when the order of the column changes, you no need to modify your code.

result = Convert.ToDecimal(row[“Amount”]);

Also make sure Amount column doesn’t return null value.

select Top 1 wefDate, ISNULL(RateDetails.Amount,0) Amount
from Rate inner join RateDetails

Upvotes: 0

Yavendra Narayan
Yavendra Narayan

Reputation: 11

Hey i think you should use "double" rather than "decimal". Please try it.

Upvotes: 1

Related Questions