2013-08-16 80 views
0

我想創建一個「聯繫我們」頁面,我有這個頁面上的下拉列表使用SqlDataSource從我的數據庫拉列表,但我的數據庫有重複的entrys,我不想要顯示在列表中,無論如何,我可以刪除重複?我不能爲我的生活弄清楚。從sqldatasource刪除重複

這是我的原始碼,謝謝。

<%@ Page Language="C#" %> 

<!DOCTYPE html> 

<script runat="server"> 

protected void Page_Load(object sender, EventArgs e) 
{ 
    DropDownList2.Visible = false; 
    DropDownList3.Visible = false; 
} 
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int ID = DropDownList1.SelectedIndex +1; 
    string _courseID = ID.ToString(); 
    StringCollection idCollection = new StringCollection(); 
    SqlDataSource2.SelectCommand = "SELECT * FROM TB_Chapter WHERE c_CourseID = '" + _courseID + "' "; 
    DropDownList2.DataBind(); 
    DropDownList2.Visible = true; 
    DropDownList3.Visible = false; 
} 
</script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="m_ModuleName" DataValueField="m_ID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:OpenIT_DBConnectionString3 %>" SelectCommand="SELECT * FROM [TB_modules]"></asp:SqlDataSource> 
    <br /> 
    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="c_ChapterName" DataValueField="c_ChapterID"></asp:DropDownList> 
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:OpenIT_DBConnectionString3 %>" SelectCommand="SELECT * FROM [TB_Chapter] WHERE ([c_CourseID] = @c_CourseID)"> 
     <SelectParameters> 
      <asp:ControlParameter ControlID="DropDownList1" Name="c_CourseID" PropertyName="SelectedValue" Type="String" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 
    <br /> 
    <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource3" DataTextField="c_ChapterTopicName" DataValueField="c_ChapterTopicID"></asp:DropDownList> 
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:OpenIT_DBConnectionString3 %>" SelectCommand="SELECT * FROM [TB_Chapter] WHERE ([c_ChapterID] = @c_ChapterID)"> 
     <SelectParameters> 
      <asp:ControlParameter ControlID="DropDownList2" Name="c_ChapterID" PropertyName="SelectedValue" Type="String" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 
</div> 
</form> 
</body> 
</html> 
+0

使用DISTINCT !!!! –

+0

爲什麼你的數據庫有重複?我可以看到倉庫正常化,但不知怎的,我不認爲你在做什麼。 – Maslow

回答

0

SELECT DISTINCT * FROM TB_Chapter WHERE c_CourseID = '" + _courseID + "' "

0

使用DISTINCT關鍵字。


WITH UniqueChapterxxx AS 
    (
     SELECT m_ID, m_ID,xxx, 
      ROW_NUMBER() OVER(PARTITION BY m_ID ORDER BY m_ID) AS 'RowNum' 
     FROM TB_Chapter WHERE c_CourseID = '" + _courseID + "' " 
    ) 
    SELECT * 
    FROM UniqueChapterxxx 
    WHERE RowNum = 1 

Check This Link