2016-08-22 2 views
0

내 중첩 된 gridview 검색을 구현했으며 모두 잘 작동합니다. 그러나 gridview로드 할 때 부모 테이블에 중복 행을 표시합니다. Select 문 잘못된 결과 표시

enter image description here

당신이 그림에서 볼 수 있듯이

는 AC107의 CourseID에서이 개 책이 있습니다. 그러나 내 Gridview 과정에서 각 교과서에 대한 행을 표시합니다. 나는이 select 문을 엉망으로 만들었고 어쨌든 어떤 것이 작동하는지, gridview가로드되지 않는지보기 위해 바꾼다.

 protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      //i'm using a datatable for storing all the data 
      DataTable dt = new DataTable(); 
      string query = "select * from Course inner join textBooks on textBooks.CourseID = Course.CourseID"; 

      //wrapping in 'using' means the connection is closed an disposed when done 
      using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HUTDMSConnectionString"].ToString())) 
      using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection)) 
      { 
       try 
       { 
        //fill the datatable with the contents from the database 
        adapter.Fill(dt); 
       } 
       catch 
       { 
       } 
      } 

      //save the datatable into a viewstate for later use 
      ViewState["allBooks"] = dt; 

      GridView1.DataSource = dt; 
      GridView1.DataBind(); 
     } 
    } 

다음은 내 데이터 테이블의 레이아웃입니다.

SELECT * FROM (
select Course.*, 
ROW_NUMBER() OVER(PARTITION BY Course.CourseID ORDER BY Course.CourseID) AS rn 
from Course 
inner join textBooks 
on textBooks.CourseID = Course.CourseID) xxx 
WHERE rn = 1; 
(당신이 SqlConnection 바이더 클래스를 사용하고 있기 때문에 당신이 SQL Server를 사용하는 가정)

enter image description here

+0

빈 try-catch를 절대로 사용하지 마십시오. – LarsTech

답변

0

는 확실히 당신에게 중복 행을 가져 오는 아닌 SQL 쿼리, ROW_NUMBER() 기능을 사용하고 같이 작동하는 방법을 볼 수 있습니까
+0

내 현재 쿼리가있는 따옴표에 그냥 던지시겠습니까? – user3487671

+0

예 ... 현재 쿼리를이 오류가있는 – Rahul

+0

으로 바꿉니다. 'System.Data.DataRowView'에는 'thirteenISBN'이라는 이름의 속성이 없습니다. – user3487671