Ziad Adnan
Ziad Adnan

Reputation: 822

Specified cast is not valid - how to resolve this error?

I created view in Oracle SQL and I need to view the data in an ASP.NET Core MVC view but when I run the view, I got an error

Specified cast is not valid

This is the view script :

CREATE OR REPLACE FORCE VIEW TRNGKAASH.LAB_RESULT_VIEW
(
   HOSPITAL_NO,
   LAB_ORDER_NO,
   PATIENT_NAME_A,
   PATIENT_NAME_E,
   PATIENT_HOSPITAL,
   PATIENT_NO,
   EVENT_NO,
   PATIENT_CATEGORY,
   PRIO_FLAG,
   PATIENT_SOURCE_IND,
   SERV_REQUEST_DATE_G,
   SERV_REQUEST_DATE_H,
   SERV_REQUEST_USER_ID,
   SERV_REQUEST_DOCTOR_NO,
   SERV_REQUEST_DOCTOR_NAME,
   INIT_DIAGNISIS,
   LAB_NO,
   MEDICAL_CHECK,
   SAMPLE_NO,
   SAMPLE_COLLECTED_DATE_G,
   SAMPLE_COLLECTED_DATE_H,
   SAMPLE_COLLECTED_BY,
   SAMPLE_RECEIVED_DATE_G,
   SAMPLE_RECEIVED_DATE_H,
   SAMPLE_RECIEVED_BY,
   SERV_TYPE,
   SERV_NO,
   GROUP_NO,
   END_RESULT,
   CANCEL_DATE_G,
   CANCEL_DATE_H,
   CANCEL_REASON,
   CANCEL_BY,
   SAMPLE_NOTE,
   PROVIDING_RESOURCE,
   HEPARIN_FLAG
)
   BEQUEATH DEFINER
AS
   SELECT H.HOSPITAL_NO,
          H.LAB_ORDER_NO,
          H.PATIENT_NAME_A,
          H.PATIENT_NAME_E,
          H.PATIENT_HOSPITAL,
          H.PATIENT_NO,
          H.EVENT_NO,
          H.PATIENT_CATEGORY,
          H.PRIO_FLAG,
          H.PATIENT_SOURCE_IND,
          H.SERV_REQUEST_DATE_G,
          H.SERV_REQUEST_DATE_H,
          H.SERV_REQUEST_USER_ID,
          H.SERV_REQUEST_DOCTOR_NO,
          H.SERV_REQUEST_DOCTOR_NAME,
          H.INIT_DIAGNISIS,
          H.LAB_NO,
          H.MEDICAL_CHECK,
          S.SAMPLE_NO,
          S.SAMPLE_COLLECTED_DATE_G,
          S.SAMPLE_COLLECTED_DATE_H,
          S.SAMPLE_COLLECTED_BY,
          S.SAMPLE_RECEIVED_DATE_G,
          S.SAMPLE_RECEIVED_DATE_H,
          S.SAMPLE_RECIEVED_BY,
          S.SERV_TYPE,
          S.SERV_NO,
          S.GROUP_NO,
          S.END_RESULT,
          S.CANCEL_DATE_G,
          S.CANCEL_DATE_H,
          S.CANCEL_REASON,
          S.CANCEL_BY,
          S.SAMPLE_NOTE,
          S.PROVIDING_RESOURCE,
          S.HEPARIN_FLAG
     FROM LAB_ORDERS_STS H, LAB_SAMPLE_STS S
    WHERE     H.HOSPITAL_NO = S.HOSPITAL_NO
          AND H.LAB_ORDER_NO = S.LAB_ORDER_NO
          AND H.END_RESULT <> 1;

This is the model class:

using System;
using System.Collections.Generic;

namespace OracleHIS.Models
{
    public partial class LabResultView
    {
        public string HospitalNo { get; set; } = null!;
        public long LabOrderNo { get; set; }
        public string? PatientNameA { get; set; }
        public string? PatientNameE { get; set; }
        public string? PatientHospital { get; set; }
        public long? PatientNo { get; set; }
        public byte? EventNo { get; set; }
        public int? PatientCategory { get; set; }
        public int? PrioFlag { get; set; }
        public int? PatientSourceInd { get; set; }
        public DateTime? ServRequestDateG { get; set; }
        public string? ServRequestDateH { get; set; }
        public string? ServRequestUserId { get; set; }
        public short? ServRequestDoctorNo { get; set; }
        public string? ServRequestDoctorName { get; set; }
        public string? InitDiagnisis { get; set; }
        public int? LabNo { get; set; }
        public bool? MedicalCheck { get; set; }
        public long SampleNo { get; set; }
        public DateTime? SampleCollectedDateG { get; set; }
        public string? SampleCollectedDateH { get; set; }
        public short? SampleCollectedBy { get; set; }
        public DateTime? SampleReceivedDateG { get; set; }
        public string? SampleReceivedDateH { get; set; }
        public string? SampleRecievedBy { get; set; }
        public int? ServType { get; set; }
        public int? ServNo { get; set; }
        public int? GroupNo { get; set; }
        public int? EndResult { get; set; }
        public DateTime? CancelDateG { get; set; }
        public string? CancelDateH { get; set; }
        public int? CancelReason { get; set; }
        public string? CancelBy { get; set; }
        public string? SampleNote { get; set; }
        public int ProvidingResource { get; set; }
        public bool? HeparinFlag { get; set; }
    } 
}

This is the Index controller method:

public ActionResult Index()
{
    var results = labResultsView.List(); 
    return View();
}

and the repository code :

public IList<LabResultView> List()
{
    return db.LabResultViews.ToList();
}

The error appears in the repository code on this line :

   return db.LabResultViews.ToList();

This is the screenshot showing the error:

enter image description here

Please let me know how to solve this error.

These are the tables data types :

LAB_ORDERS_STS table :

enter image description here

LAB_SAMPLE_STS table :

enter image description here

Upvotes: 0

Views: 1169

Answers (1)

Md Farid Uddin Kiron
Md Farid Uddin Kiron

Reputation: 22419

Specified cast is not valid

Based on the error, I think following data types are inconsistent

byte? EventNo should be long,

short? SampleCollectedBy should be int

bool? MedicalCheck should be changed to int if it contains number like 0 or 1 other than oracle number should be deal with decimal

short? SampleCollectedBy better to change to int

bool? HeparinFlag also need to replace in int if it only contains 0 or 1 if contains other then decimal

Note: So far investigated on your shared POCO Class and data entity model based on the error your issue mostly related to defination of asp.net core POCO class which doesn't matached with the database entity defination. You could follow this official Chart for reference:

enter image description here

enter image description here

More details can be founded at official document here

Upvotes: 1

Related Questions