数据表返回的行数多于必要的数量

时间:2015-03-27 15:40:35

标签: c# asp.net

我在DataTable中从数据库返回值。在数据库中,我有5个值,如果我在Management Studio中运行查询,我会得到预期的结果。但是我的问题是当通过C#函数返回它时我得到9个值

protected static DataTable GetData()
{
    using (SqlConnection con = new SqlConnection(Common.ConnectionString))
    {

        string myQuery = "SELECT MESSAGE FROM MYTABLE ORDER BY CREATEDATE DESC";
        using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                con.Open();
                sda.SelectCommand = cmd;
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
        }
    }
}

为了显示这个,我嵌套了asp Repeater,如下所示

 <asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table id="myt">
            <tr>
                <th>Message
                </th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td class="sendbubble">
                <%# DataBinder.Eval(Container.DataItem, "MESSAGE") %>
            </td>
            <asp:Repeater ID="InnerRepeater" runat="server">
                <ItemTemplate>
                    <tr>
                        <td class="receivebubble" style="color: #ffffff;">
                            <%# DataBinder.Eval(Container.DataItem,"MESSAGE") %>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </tr>
    </ItemTemplate>
</asp:Repeater>

我不明白为什么在Repeater1我得到重复的值。在Page_Load我有

Repeater1.DataSource = GetData();
Repeater1.DataBind();

3值来自经理 2来自员工

这些值由Flag列分隔,其中包含manageremployee

的数据

提前感谢您的所有帮助和支持

1 个答案:

答案 0 :(得分:1)

在第一个转发器中,您只需要加载2个标志。 对于每个标志,您有另一个转发器,您可以使用相应的消息创建行。

查看:

<asp:Repeater ID="pageList" runat="server" OnItemDataBound="pageList_ItemDataBound">
                <HeaderTemplate>
                    <table id="myt">
                        <tr>
                            <th>Message
                            </th>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <asp:Repeater ID="itemRepeater" runat="server">
                            <ItemTemplate>
                                <tr>
                                    <td class="receivebubble" style="color: #ffffff;">
                                        <%# DataBinder.Eval(Container.DataItem,"MESSAGE") %>
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:Repeater>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>

代码背后:

protected void Page_Load(object sender, EventArgs e)
        {
            pageList.DataSource = GetFlags();
            pageList.DataBind();
        }

        protected void pageList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            Repeater itemRepeater = (Repeater) e.Item.FindControl("itemRepeater");

            switch (item.ItemType)
            {
                case ListItemType.Header:
                    break;
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    if (itemRepeater != null)
                    {
                        var flagType = (item.DataItem as DataRowView).Row["Flag"].ToString();
                        DataTable repeaterData = GetData(flagType);
                        itemRepeater.DataSource = repeaterData;
                        itemRepeater.DataBind();
                    }
                    break;
                case ListItemType.Footer:
                    break;
            }
        }

商务:

private static DataTable GetData(string flagType)
        {
            using (SqlConnection con = new SqlConnection("..."))
            {

                string myQuery = "SELECT MESSAGE FROM MYTABLE WHERE Flag = " + flagType + " ORDER BY CREATEDATE DESC";
                using (SqlCommand cmd = new SqlCommand(myQuery, con))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        con.Open();
                        sda.SelectCommand = cmd;
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }

        private static DataTable GetFlags()
        {
            using (SqlConnection con = new SqlConnection("..."))
            {

                string myQuery = "SELECT DISTINCT Flag FROM MYTABLE";
                using (SqlCommand cmd = new SqlCommand(myQuery, con))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        con.Open();
                        sda.SelectCommand = cmd;
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }