[Solved] ASP.Net Error: Cannot implicitly convert type System.DateTime? to int

Last Reply 21 days ago By dharmendr

Posted 21 days ago

Hi,

I have one stored procudure

CREATE PROCEDURE [dbo].[PayProcess] 
    -- Add the parameters for the stored procedure here
    (@Date Date)
AS
BEGIN
    SELECT 'Procedure Run sucessfully' As 'Procedure'
END
GO

Currently my requirement is that whar ever data i have selected need to display it on grid view

in controller i am getting error 

cannot convert from 'System.DateTime?' to 'int?'

[HttpPost]
public JsonResult getAll1(DateTime? Id5)
{
    var result = db.PayProcess(Id5).ToList();
    var JsonResult = Json(result, JsonRequestBehavior.AllowGet);
    JsonResult.MaxJsonLength = int.MaxValue;
    return JsonResult;
}

Could you please kindly help me

Posted 21 days ago Modified on 21 days ago

Hi mahesh213,

This error is because DateTime? means it's nullable DateTime.

So before assigning it to DateTime you need to check if it contains value and only then assign.

Check if datetime contains value and if not return any other value which you want.

Use the below code.

[HttpPost]
public JsonResult getAll1(DateTime? Id5)
{
    DateTime dt = Id5.HasValue ? Id5.Value : DateTime.Now;
    var result = db.PayProcess(dt).ToList();
            var JsonResult = Json(result, JsonRequestBehavior.AllowGet);
    JsonResult.MaxJsonLength = int.MaxValue;

    return JsonResult;
}

Or change signature of method to remove nullable.

[HttpPost]
public JsonResult getAll1(DateTime Id5)
{
    var result = db.PayProcess(Id5).ToList();
            var JsonResult = Json(result, JsonRequestBehavior.AllowGet);
    JsonResult.MaxJsonLength = int.MaxValue;

    return JsonResult;
}