2016-08-22 111 views
0

我实现了对我的嵌套gridview的搜索,并且All运行良好。但是当gridview加载时,它会在父表中显示重复的行。Select statement显示不正确的结果

enter image description here

正如你可以在图片中看到,还有AC107的CourseID下2本书。但是我的Gridview在课程中为每本教科书显示一行。我搞砸了这个选择语句,无论如何我改变它看看是否有任何工作,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(); 
     } 
    } 

下面是我的数据表格的布局。

enter image description here

+0

千万不要使用空的try-catch。 – LarsTech

回答

0

是(因为你正在使用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; 
你确定你的SQL查询不能取你重复的行,使用 ROW_NUMBER()功能,看看它是如何工作的像
+0

我只是把它放在我当前查询所在的报价单中? – user3487671

+0

是的......用你的当前查询替换这个 – Rahul

+0

接收到这个错误:'System.Data.DataRowView'不包含名为'thirteenISBN'的属性。 – user3487671