2016-08-22 8 views
0

私のネストされたグリッドビューの検索を実装し、すべてがうまくいきます。しかし、グリッドビューが読み込まれると、親テーブルに重複した行が表示されます。Selectステートメント不適切な結果を表示する

enter image description here

あなたが写真で見ることができるように、AC107のCourseID下の2冊があります。しかし、私のGridviewはコースの各教科書の行を表示しています。私はこの選択された声明を乱したとにかく、何かが動作するかどうかを調べるためにそれを変更すると、gridview does not load。

 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(); 
     } 
    } 

以下は、私のデータテーブルのレイアウトです。

enter image description here

+0

空のtry-catchを使用しないでください。 – LarsTech

答えて

0

あなたは、必ずあなたのSQLクエリは、あなたに重複行をフェッチしていないですROW_NUMBER()機能を使用すると、それは(あなたがSqlConnectionプロバイダクラスを使用しているので、あなたがSQL Serverを使用していると仮定した場合)のような動作を確認

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; 
+0

私は現在のクエリが引用符で囲まれていますか? – user3487671

+0

はい...正確には、このエラーを受け取る – Rahul

+0

で現在のクエリを置き換えます。 'System.Data.DataRowView'には 'thirteenISBN'という名前のプロパティが含まれていません。 – user3487671

関連する問題