如何检索&显示同一标题下多个表的数据?

时间:2017-05-31 20:42:54

标签: c# asp.net sql-server

我正在制作一份员工名册计划程序。 目前,我正在尝试使用转发器以日历格式显示当前的转换。 以下是我目前的输出: snip1 如你所见,星期六有两班倒。有谁知道如何将这两个班次置于一个标题下,所以2017年6月3日星期六只出现一次,而不是两次?

您可以在下面找到我的代码:

HTML:

<div class="col-lg-12">
                <asp:Repeater ID="repSubscription" runat="server" OnItemDataBound="repSubscription_ItemDataBound">
                    <ItemTemplate>
                        <div class="col-lg-2">
                            <div class="panel panel-default">
                                <div class="panel-heading" style="background-color: #3A6EA5; color: white">
                                    <h4 class="panel-title">
                                        <%# Eval("Start_Time", "{0:dddd, dd MMMM yyyy}") %>
                                    </h4>
                                </div>
                                <!--panel-heading-->
                                <div class="panel-body">
                                    <asp:Repeater ID="repShift" runat="server">
                                        <ItemTemplate>
                                            <b><%# Eval("Job_Title") %></b>
                                        </ItemTemplate>
                                    </asp:Repeater>
                                    </br>
                                    <asp:Repeater ID="repEmp" runat="server">
                                        <ItemTemplate>
                                            <%# Eval("Employee_Name") %>
                                        </ItemTemplate>
                                    </asp:Repeater>
                                    <br />
                                    <asp:Repeater ID="repTimes" runat="server">
                                        <ItemTemplate>
                                            <%# Eval("Start_Time", "{00:HH:MM}") %> - <%# Eval("End_Time" , "{00:HH:MM}") %>
                                        </ItemTemplate>
                                    </asp:Repeater>
                                </div>
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>
            </div>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    this.bindRepeater();
}

private void bindRepeater()
{
    conn = new SqlConnection(connectionString);
    conn.Open();
        comm = new SqlCommand("SELECT DISTINCT Start_Date, End_Date FROM My_Subscription WHERE Subscription_Id = 1", conn);
        SqlDataReader reader1 = comm.ExecuteReader();
        while (reader1.Read())
        {
            startDate = Convert.ToDateTime(reader1["Start_Date"]);
            endDate = Convert.ToDateTime(reader1["End_Date"]);

            //lblError.Text += startDate.ToString() + "<br/>" + endDate.ToString();

        }
    conn.Close();
    using (SqlConnection con = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT Start_Time, Emp_ID, Job_ID, Emp_Sch_Id FROM My_Employee_Schedule WHERE Start_Time BETWEEN @startReportDate AND @endReportDate", con))
            {
                cmd.Parameters.AddWithValue("@startReportDate", startDate);
                cmd.Parameters.AddWithValue("@endReportDate", endDate);
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    repSubscription.DataSource = dt;
                    repSubscription.DataBind();
                }
            }
    }
}

protected void repSubscription_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    conn = new SqlConnection(connectionString);
    conn.Open();
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater repBusiness = (Repeater)(e.Item.FindControl("repShift"));
        SqlCommand cmd = new SqlCommand("SELECT Job_Title FROM My_Job_Type WHERE Job_Type_Id=@Job_Type_Id");
        string Group_Id = DataBinder.Eval(e.Item.DataItem, "Job_ID").ToString();
        cmd.Parameters.AddWithValue("@Job_Type_Id", Group_Id);
        //Need to assign the Data in datatable
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        repBusiness.DataSource = dt;
        repBusiness.DataBind();
    }
    conn.Close();


    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater repTimes = (Repeater)(e.Item.FindControl("repTimes"));

        SqlCommand cmd = new SqlCommand("SELECT Start_Time, End_Time FROM My_Employee_Schedule WHERE Emp_Sch_Id=@EmpSchId");
        string Group_Id = DataBinder.Eval(e.Item.DataItem, "Emp_Sch_Id").ToString();
        cmd.Parameters.AddWithValue("@EmpSchId", Group_Id);
        //Need to assign the Data in datatable
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        repTimes.DataSource = dt;
        repTimes.DataBind();
    }


    conn.Open();
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater repEmp = (Repeater)(e.Item.FindControl("repEmp"));

        SqlCommand cmd = new SqlCommand("SELECT Employee_Name FROM My_Employee WHERE Employee_Id=@EmpId");
        string Group_Id = DataBinder.Eval(e.Item.DataItem, "Emp_ID").ToString();
        cmd.Parameters.AddWithValue("@EmpId", Group_Id);
        //Need to assign the Data in datatable
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        repEmp.DataSource = dt;
        repEmp.DataBind();
    }
    conn.Close();
}

,然后我很乐意发布它。 非常感谢您提前提供任何帮助!

0 个答案:

没有答案