클래스 고객이 있습니다. 있는 액세스 db 데이터베이스에서 데이터를로드하려고합니다.db에서 클래스 객체로 데이터를로드 할 때 오류가 발생합니다.
public class Customer
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string CustAddress { get; set; }
public string PnoneNo { get; set; }
public string MobileNo { get; set; }
public string CstNo { get; set; }
public string DlNo { get; set; }
public decimal BalAmt { get; set; }
}
및 DB 내 테이블 구조는 다음과 같습니다 :
고객 클래스 구조 아래 지금
내가 그것을 던지고 고객 클래스의 데이터를로드하려고 할 때 오류 :
"지정된 캐스트가 유효하지 않습니다." 클래스 I는 이하의 코드를 사용하고있는 데이터로드
: 그것은 잘 작동되는이 광고 댓글 때
public static List<Customer> LoadListItems(string strTable, string strOrderBy)
{
List<Customer> lstCustomer=null;
try
{
DataUtility objDataUtility = new DataUtility();
DataTable objCustomerList = objDataUtility.LoadCustomerInfo(strTable, strOrderBy);
lstCustomer= objCustomerList.AsEnumerable().Select(row =>
new Customer
{
CustomerId = row.Field<int>("CID"), //throwing error for this line
CustomerName = row.Field<string>("salPNm"),
CustAddress = row.Field<string>("cadd"),
MobileNo = row.Field<string>("cmbl"),
PnoneNo = row.Field<string>("cph"),
DlNo = row.Field<string>("cDlN"),
CstNo = row.Field<string>("cTin"),
BalAmt = row.Field<decimal>("cobal")
}).ToList();
}
catch (Exception ex)
{
throw ex;
}
return lstCustomer;
}
상기 방법 CustomerId = row.Field<int>("CID"),
에서 오류 사촌 던지고. iennumrable 목록에서 int 값을 얻을 수있는 방법을 알려주십시오.
미리 감사드립니다. Eshwer
'try-catch' 블록은'catch'에서 예외를 다시 던지면 중복됩니다. –
'long'또는 'decimal'을 시도하십시오. – SLaks
우리가 사용하는 유형이 무엇인지 추측하기보다는 실제 유형을 얻고 그에 따라 조정하십시오. 열의'DataType' :'objCustomerList.Columns [ "CID"] .DataType'을 디버그하고 검사하십시오. –