根据每行的转发器控制值执行计数C#ASP.NET

时间:2017-08-05 14:49:32

标签: mysql

道歉,如果这听起来很无用,但我是ASP.NET和C#的新手[编者注:和SQL]。所以基本上,我有这个转发器,我绑定数据库中的约会表数据。我希望看到各个运营商汇总的总工作数(即第1行='约翰史密斯'10个工作岗位,第2行='吉姆贝克'16个工作岗位等)。

我已经设法从数据库进行计数,但它只返回总工作数(即26个工作),并且该值显示在行中的每个操作符旁边(即第1行='John Smith'26个工作,第2行='吉姆贝克'26个工作岗位等)。无论如何,我能做得更好并让它发挥作用吗?

我将再次感谢您的帮助和道歉,我的编程技巧非常基础。

.aspx的:

<div class="container">
        <div class="col-lg-4">
            <asp:DropDownList ID="listWCSummary" runat="server" DataSourceID="SqlDataSource2" DataTextField="ConvertedWC" DataValueField="Id" Width="200" AutoPostBack="true" OnSelectedIndexChanged="listWCSummary_SelectedIndexChanged"></asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:GAUConnectionString1 %>" SelectCommand="select * from JobWeekTable"></asp:SqlDataSource>
        </div>
        <table class="table table-bordered table-condensed table-hover table-responsive">
            <thead>
                <tr>
                    <td class="auto-style1" style="background-color: #428BCA;">
                        <strong>
                            <asp:Label ID="lblWeekNo" runat="server" Text="Label" ForeColor="White"></asp:Label></strong></td>
                    <td class="auto-style1" style="background-color: #428BCA;">
                        <strong>
                            <asp:Label ID="lblDay1" runat="server" Text="Label" ForeColor="White"></asp:Label></strong></td>
                    <td class="auto-style1" style="background-color: #428BCA;">
                        <strong>
                            <asp:Label ID="lblDay2" runat="server" Text="Label" ForeColor="White"></asp:Label></strong></td>
                    <td class="auto-style1" style="background-color: #428BCA;">
                        <strong>
                            <asp:Label ID="lblDay3" runat="server" Text="Label" ForeColor="White"></asp:Label></strong></td>
                    <td class="auto-style1" style="background-color: #428BCA;">
                        <strong>
                            <asp:Label ID="lblDay4" runat="server" Text="Label" ForeColor="White"></asp:Label></strong></td>
                    <td class="auto-style1" style="background-color: #428BCA;">
                        <strong>
                            <asp:Label ID="lblDay5" runat="server" Text="Label" ForeColor="White"></asp:Label></strong></td>
                    <td class="auto-style1" style="background-color: #428BCA;">
                        <strong>
                            <asp:Label ID="lblDay6" runat="server" Text="Label" ForeColor="White"></asp:Label></strong></td>
                    <td class="auto-style1" style="background-color: #428BCA;">
                        <strong>
                            <asp:Label ID="lblDay7" runat="server" Text="Label" ForeColor="White"></asp:Label></strong></td>


                </tr>
            </thead>
            <tbody>
                <asp:Repeater ID="rptrSummaryView" runat="server" OnItemDataBound="rptrSummaryView_ItemDataBound">
                    <ItemTemplate>
                        <tr>
                            <td style="text-align: center;">
                                <div class="panel-heading">
                                    <asp:Label ID="lbOpName" runat="server" Text='<%# Eval("EngineerName") %>'></asp:Label>


                                </div>
                                <td>
                                    <div class="col-lg-4">
                                        <div class="row" style="background-color: forestgreen; text-align: center;">
                                            <asp:Label ID="Label1" runat="server" Text="AM" Font-Bold="True" ForeColor="White"></asp:Label>
                                        </div>
                                        <div class="row" style="text-align: center; color: red;">
                                            <asp:Label ID="lblCountAMDay1" runat="server" Text="Count" ForeColor="Red"></asp:Label>
                                        </div>
                                    </div>
                                    <div class="col-lg-4">
                                        <div class="row" style="background-color: green; text-align: center;">
                                            <asp:Label ID="Label2" runat="server" Text="PM" Font-Bold="True" ForeColor="White"></asp:Label>
                                        </div>
                                        <div class="row" style="text-align: center;">
                                            <asp:Label ID="lblCountPMDay1" runat="server" Text="Count" ForeColor="Red"></asp:Label>
                                        </div>
                                    </div>
                                    <div class="col-lg-4">
                                        <div class="row" style="background-color: darkgreen; text-align: center;">
                                            <asp:Label ID="Label4" runat="server" Text="EVE" Font-Bold="True" ForeColor="White"></asp:Label>
                                        </div>
                                        <div class="row" style="text-align: center;">
                                            <asp:Label ID="lblCountEVEDay1" runat="server" Text="Count" ForeColor="Red"></asp:Label>
                                        </div>
                                    </div>






                                </td>
                    </ItemTemplate>
                </asp:Repeater>
            </tbody>
        </table>

    </div>

C#

 protected void rptrSummaryView_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        string cs = ConfigurationManager.ConnectionStrings["GAUConnectionString1"].ConnectionString;

        using (SqlConnection con = new SqlConnection(cs))
        {


            con.Open();
        Label lblAM1 = (Label)e.Item.FindControl("lblCountAMDay1");
        Label lblPM1 = (Label)e.Item.FindControl("lblCountPMDay1");
        Label lblEV1 = (Label)e.Item.FindControl("lblCountEVEDay1");
        string Day1 = "select Day1 from JobWeekTable where Id='" + listWCSummary.SelectedValue + "'";
        SqlCommand GetFName = new SqlCommand(Day1, con);
        string Firstday = GetFName.ExecuteScalar().ToString().Replace("00:00:00", "");

        }

        //Day1

        {

            Label lblAM1 = (Label)e.Item.FindControl("lblCountAMDay1");
            Label lblPM1 = (Label)e.Item.FindControl("lblCountPMDay1");
            Label lblEV1 = (Label)e.Item.FindControl("lblCountEVEDay1");



            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                string Day1 = "select Day1 from JobWeekTable where Id='" + listWCSummary.SelectedValue + "'";

                SqlCommand GetFName = new SqlCommand(Day1, con);
                string Firstday = GetFName.ExecuteScalar().ToString().Replace("00:00:00", "");

                SqlCommand cmdDay1AM = new SqlCommand("select EngineerTable.EngineerName, Count(AppointmentTable.CustomerID) As CountAM from EngineerTable inner join AppointmentTable on EngineerTable.OpId=AppointmentTable.OpId where TimebandId = '1' and Dateconverted = '" + Firstday + "' group by EngineerName, TimebandId", con);
            using (SqlDataReader rd = cmdDay1AM.ExecuteReader())
            {
                while (rd.Read())
                {
                    lblAM1.Text = rd["CountAM"].ToString();
                    if (Convert.ToInt32(lblAM1.Text) < 2)
                    {
                        lblAM1.ForeColor = System.Drawing.Color.Red;

                    }
                    else
                    {
                        lblAM1.ForeColor = System.Drawing.Color.Green;
                    }
                }
            }
            }
        }


    }


}

protected void listWCSummary_SelectedIndexChanged(object sender, EventArgs e)
{
    BindSummaryViewRptr();
    string WeekId = listWCSummary.SelectedValue;
    string cs = ConfigurationManager.ConnectionStrings["GAUConnectionString1"].ConnectionString;

    using (SqlConnection con = new SqlConnection(cs))

    {
        SqlCommand cmd = new SqlCommand("select * from JobWeekTable where Id=@ID", con);
        SqlParameter parameter = new SqlParameter("@ID", WeekId);
        cmd.Parameters.Add(parameter);
        con.Open();
        string Day1 = "select Day1 from JobWeekTable where Id='" + listWCSummary.SelectedValue + "'";
        SqlCommand GetFName = new SqlCommand(Day1, con);
        string FirstName = GetFName.ExecuteScalar().ToString().Replace("00:00:00", "");
    }


}

private void BindSummaryViewRptr()
{
    string WorkWeekId = listWCSummary.SelectedValue;
    string CustomerId = Request.QueryString["Id"];
    string cs = ConfigurationManager.ConnectionStrings["GAUConnectionString1"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand("select distinct EngineerTable.EngineerName from EngineerTable inner join AppointmentTable on EngineerTable.OpId=AppointmentTable.OpId where AppointmentTable.WorkWeek = @WorkWeekId", con);
        SqlParameter parameter = new SqlParameter("@WorkWeekId", WorkWeekId);
        cmd.Parameters.Add(parameter);

        con.Open();
        using (SqlDataAdapter sda = new SqlDataAdapter(cmd))

        {

            DataTable dtBrands = new DataTable();
            sda.Fill(dtBrands);
            rptrSummaryView.DataSource = dtBrands;
            rptrSummaryView.DataBind();

        }


    }
}

2 个答案:

答案 0 :(得分:0)

SELECT jobs AS bakerjobs FROM appointmentments WHERE name ='jim baker'

SELECT jobs AS johnjobs FROM appointmentments WHERE name ='John Smith'

这些查询将根据名称字段返回相应的作业nos。

答案 1 :(得分:0)

所以我解决了自己的问题。我使用<hiddenfield>来存储EngineerId,然后修改了运行OnItemBound的SQL查询并解决了它。见下面的代码。得到here

的主要帮助
protected void rptrSummaryView_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   If (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {

            string OpId = (e.Item.FindControl("hfCustomerId") as HiddenField).Value;
            Label lblAM1 = (Label)e.Item.FindControl("lblCountAMDay1");
            Label lblPM1 = (Label)e.Item.FindControl("lblCountPMDay1");
            Label lblEV1 = (Label)e.Item.FindControl("lblCountEVEDay1");

SQL命令是:

SqlCommand cmdDay1AM = new SqlCommand("select Count(AppointmentTable.TimebandId) as CountAM from AppointmentTable where Dateconverted= '" + FirstName + "' and TimebandId='1' and AppointmentTable.OpId = '"+OpId+"'", con);