2012-10-14 2 views
1

클래스 고객이 있습니다. 있는 액세스 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 내 테이블 구조는 다음과 같습니다 :

고객 클래스 구조 아래 지금

enter image description here

내가 그것을 던지고 고객 클래스의 데이터를로드하려고 할 때 오류 :

"지정된 캐스트가 유효하지 않습니다." 클래스 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

+0

'try-catch' 블록은'catch'에서 예외를 다시 던지면 중복됩니다. –

+0

'long'또는 'decimal'을 시도하십시오. – SLaks

+0

우리가 사용하는 유형이 무엇인지 추측하기보다는 실제 유형을 얻고 그에 따라 조정하십시오. 열의'DataType' :'objCustomerList.Columns [ "CID"] .DataType'을 디버그하고 검사하십시오. –

답변

0

로 교체 - row.Field<int>("CID")을 - 또한

CustomerId = Convert.ToInt64(row.Field<int>("CID")); 

이 선 위에 Quick Watch을 적용하여 값을 확인한다. 그것이 not null 인 경우 반환 값이 무엇인지 확인하십시오.

+0

나는 이것이 작동하지 않을 것이라고 생각한다. CustomerId는 여전히 Int32이며 확장 메서드 인 .Field ()에 의해 예외가 throw됩니다. – Kai

0

는 시도이

public class Customer 
{ 
    public Int64 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; } 
} 

CustomerId = row.Field<Int64>("CID") 

나는 신원이 긴 정수라고 생각합니다.

+0

안녕하세요. 답장을 보내 주시면 문제를 해결하는 데 도움이됩니다. 문제는 데이터가 Int16 유형으로 들어오는 것입니다. – Eshwer

관련 문제