0

我正在尝试创建一个“联系我们”页面,我在此页面上有一个下拉列表,使用 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>
4

2 回答 2

0

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

于 2013-08-16T14:11:33.340 回答
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

检查此链接


于 2013-08-16T14:30:02.397 回答