2012-12-15 5 views
0

안녕하세요, 나는 LINQ TO SQL을 배우려고 노력하고 있으며 POCO 접근 방식을 사용하여 데이터를 매핑하려고하는데 오류가 발생하는 것 같습니다. 내 코드는 다음과 같습니다. 내가 GridView.DataBind()에서 나는이 오류 코드를 실행하면Linq SQL Poco 접근 방식에 지정된 캐스트가 유효하지 않습니다

//Customers class 
    public class Customers 
    { 
     public int ID { get;set; } 
     public string CompanyName { get;set; } 
     public string ContactName { get;set; } 
    } 


     //Customers xxml file 
     <?xml version="1.0" encoding="utf-8" ?> 
     <Database Name="NORTHWND" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007"> 
      <Table Name="dbo.Customers" Member="Customers"> 
       <Type Name="Customers"> 
       <Column Name="CustomerID" Member="ID"/> 
       <Column Name="CompanyName" Member="CompanyName"/> 
       <Column Name="ContactName" Member="ContactName"/> 
       </Type> 
      </Table> 
     </Database> 

    //And this is the code I use to linq the data: 
    if(!IsPostBack) 
    { 
     DataContext ctx = new DataContext(ConfigurationManager.ConnectionStrings["customers"].ConnectionString , 
                XmlMappingSource.FromUrl(Server.MapPath("~/Customers.xml"))); 

     var customers = from c in ctx.GetTable<Customers>() 
           select c; 

     GridView1.DataSource = customers; 
     GridView1.DataBind(); 

    } 

: NORTHWIND 데이터베이스의 고객 테이블은

Specified cast is not valid. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    Exception Details: System.InvalidCastException: Specified cast is not valid. 

은 내가 잘못 여기서 뭐하는 거지? 내가 정확히 기억한다면

+0

어떤 라인이 던져입니까? – Alex

+0

데이터를 GriwView.GridView.DataBind()에 바인딩하면 오류가 발생합니다. –

답변

1

하여 Northwind에서의 CustomerID 열이 NVARCHAR입니다 ...
그래서 당신의 고객 클래스는 변경 :

public string ID { get;set; } 
+0

tryed 이제 작동했습니다. –

+0

편집 된 답변보기 – Alex

+0

감사합니다. –

관련 문제