0

我的 asp.net 项目中有一个页面,我想在其中显示员工的出勤情况。应在中继器中显示在场时和在节假日P时缺席时。现在在我的网页上,我有 2 个文本框,我可以通过它们输入年份和月份,并且该月我想获得出勤率。我的数据库表如下。AH

(1) 压延机

CalederID  Year  Month     WorkingDays
1          2013  January   1111001111100111110011111001111
2          2013  February  1001111100111110011111001111

等等 。这里 1 代表当月的工作日,0 代表星期六和星期日,我正在使用这种模式,因为在我的一个页面上,复选框被选中(星期六和星期日)而其他人没有选中,所以我知道这些是假期

(2) 考勤表

AttendanceID    EmpID PresentDays CalenderID  LeaveDate
1                1      Null         1        2013-01-14
2                1      Null         2        2013-02-15
3                1      Null         4        2013-04-11
4                3      Null         6        2013-06-26

(3) EmpInfo 表

EmpID  EmpName  and so on
1       Joe
2      Sandra              

现在,当我输入年份和月份时,我的网页上出现了问题,我想将带有标题的转发器显示为代表该月日期的日期编号。现在,如果该月有 30 天,则显示 30 个数字。使用另一个中继器,它必须以 P、A、H 格式显示出勤率,如上所述

我的中继器看起来像这样

<table class="table1" >
        <asp:Repeater ID="Repeater1" runat="server">
           <HeaderTemplate>
               <tr>
                    <td>Employee ID</td>
           </HeaderTemplate>
           <ItemTemplate>
                    <td><asp:Label ID="lbldates" runat="server" Text='<%# Eval("Dates") %>' ></asp:Label></td>
            </ItemTemplate>
            <FooterTemplate>
                    <td>TOTAL</td></tr>
               <tr>
           </FooterTemplate>           
       </asp:Repeater>
           <asp:Repeater id="rptAttendance" runat="server" OnItemDataBound="rptAttendance_ItemDataBound">
        <ItemTemplate>
            <tr>
                <td><asp:Label ID="lblEmpName" runat="server" /></td>
                <asp:Repeater ID="rptAttendanceCode" runat="server" OnItemDataBound="rptAttendanceCode_ItemDataBound" >
                    <ItemTemplate><td><asp:Label ID="lblAttendanceCode" runat="server" /></td></ItemTemplate>
                </asp:Repeater>
                </tr>
        </ItemTemplate>
    </asp:Repeater>
        </table>

后面的代码是

protected void Page_Load(object sender, EventArgs e)
        {


        }

        public void search(object sender, EventArgs e)
        {
            string cnnString = "Server=localhost;Port=3307;Database=leavesystem;Uid=root;Pwd=ashish";
            MySqlConnection cnx = new MySqlConnection(cnnString);
            cnx.Open();
            string cmdText1 = "SELECT DAY(LAST_DAY(CAST(CONCAT('" + year.Value + "', '-', MONTH(STR_TO_DATE('" + month.Value + "', '%M')), '-', 1) AS DATE))) ";
            MySqlCommand cmd1 = new MySqlCommand(cmdText1, cnx);
            MySqlDataAdapter adapter1 = new MySqlDataAdapter();
            DataSet ds1 = new DataSet();
            adapter1.SelectCommand = cmd1;
            adapter1.Fill(ds1);
            DataRow dr;
            dr = ds1.Tables[0].Rows[0];
            string value = dr[0].ToString();

            string cmdText2 = "SELECT Dates from temp where Dates <= " + value + "  ";
            MySqlCommand cmd2 = new MySqlCommand(cmdText2, cnx);
            MySqlDataAdapter adapter2 = new MySqlDataAdapter();
            DataSet ds2 = new DataSet();
            adapter2.SelectCommand = cmd2;
            adapter2.Fill(ds2);
            DataTable dt = ds2.Tables[0];
            Repeater1.DataSource = dt;
            Repeater1.DataBind();


            string cmdText3 = "SELECT CalenderID, WorkingDays from calender where Year = '" + year.Value + "' and Month = '" + month.Value + "'   ";
            MySqlCommand cmd3 = new MySqlCommand(cmdText3, cnx);
            MySqlDataAdapter adapter3 = new MySqlDataAdapter();
            DataSet ds3 = new DataSet();
            adapter3.SelectCommand = cmd3;
            adapter3.Fill(ds3);
            DataTable calender = ds3.Tables[0];
            DataRow dr3;
            dr3 = ds3.Tables[0].Rows[0];
            string CalenderID = dr3[0].ToString();

            string cmdText4 = "SELECT EmpID,EmpName from empinfo ";
            MySqlCommand cmd4 = new MySqlCommand(cmdText4, cnx);
            MySqlDataAdapter adapter4 = new MySqlDataAdapter();
            DataSet ds4 = new DataSet();
            adapter4.SelectCommand = cmd4;
            adapter4.Fill(ds4);
            DataTable employees = ds4.Tables[0];

            string cmdText5 = "SELECT EmpID,DAY(LeaveDate) AS LeaveDayOfMonth from attendance where CalenderID = '" + CalenderID + "'   ";
            MySqlCommand cmd5 = new MySqlCommand(cmdText5, cnx);
            MySqlDataAdapter adapter5 = new MySqlDataAdapter();
            DataSet ds5 = new DataSet();
            adapter5.SelectCommand = cmd5;
            adapter5.Fill(ds5);
            DataTable attendance = ds5.Tables[0];

            List<Tuple<string, string[]>> info = new List<Tuple<string, string[]>>();
            int calendarID = calender.Rows[0].Field<int>("CalenderID");
            string days = calender.Rows[0].Field<string>("WorkingDays");
            days = days.Replace("1", "P");
            days = days.Replace("0", "H");
            string[] daysList = days.Select(d => d.ToString()).ToArray();
            int present = 0;
        int holidays = 0;

        for (int i = 0; i < daysList.Length; ++i)
        {
            if (daysList[i] == "P")
                present++;

            if (daysList[i] == "H")
                holidays++;

        }

        int working = (monthdays - holidays);
        string total = (present + "/" + working);



        Tuple<string, string[],string> employeeAttendance = null;
        foreach (DataRow employee in employees.Rows)
        {
            employeeAttendance = new Tuple<string, string[],string>(employee.Field<string>("EmpName"), daysList,total);


            foreach (DataRow absentDay in attendance.AsEnumerable().Where(a => a.Field<int>("EmpID") == employee.Field<int>("EmpID")))
            {
                var leaveDay = absentDay.Field<Int64>("LeaveDayOfMonth");
                int leaveDay1 = Convert.ToInt16(leaveDay);
                leaveDay1 = leaveDay1 - 1;
                employeeAttendance.Item2[leaveDay1] = "A";
            }


            info.Add(employeeAttendance);
        }
        this.rptAttendance.DataSource = info;
        this.rptAttendance.DataBind();
        }


    protected void rptAttendance_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Tuple<string, string[],string> info = (Tuple<string, string[],string>)e.Item.DataItem;
            ((Label)e.Item.FindControl("lblEmpName")).Text = info.Item1;
            ((Label)e.Item.FindControl("lbltotal")).Text = info.Item3;
            Repeater attendanceCode = (Repeater)e.Item.FindControl("rptAttendanceCode");
            attendanceCode.DataSource = info.Item2;
            attendanceCode.DataBind();
        }
    }

    protected void rptAttendanceCode_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ((Label)e.Item.FindControl("lblAttendanceCode")).Text = e.Item.DataItem.ToString();
        }
    }

在后面的代码中,我得到该月的天数,然后将其与具有 31 个日期的表格进行比较,然后根据该逻辑,我显示我的数字,并在左下角使用中继器 2 显示我的 EmpID,现在在它们旁边的日期数字下方我想显示出席情况。有人可以告诉我如何做到这一点。我的出勤表中的 PresendtDays 列是空的,但我不知道如何使用它。请帮助我,我已经尝试了很多小时,这就是为什么我发布了我的完整代码,以便有人可以帮助我。寻求早期回应。提前致谢 !!

4

1 回答 1

1

由于您的数据库的非常规设计,我必须进行一些主要的数据操作才能完成这项工作。话虽如此,这是我提出的解决方案。

"SELECT EmpID from empinfo "您将需要执行三个附加查询,而不是 SQL 语句:

  1. Calendar表中检索信息:

    • SELECT CalendarID, WorkingDays FROM Calendar WHERE Year = [YEAR] and Month = [MONTH]
    • 这将返回一个如下所示的表:

    在此处输入图像描述

  2. 使用 CalendarID 从日历表中检索信息:

    • 从员工中选择 EmpID、EmpName
    • 这将返回一个如下所示的表:

    在此处输入图像描述

  3. 使用第一个查询中的 CalendarID 从出勤表中检索信息。

    • SELECT EmpID, DAY(LeaveDate) AS LeaveDayOfMonth 从出勤 WHERE CalendarID = [CALENDAR ID]
    • 这将返回一个如下所示的表:

    在此处输入图像描述

完成此操作后,将您的第二个中继器 ( Repeater2) 替换为以下两个中继器。第一个中继器 (rptAttendance) 列出每个员工,第二个中继器 (rptAttendanceCode) 列出该员工每月的每一天。(注意,我们正在连接转发器的OnItemDataBound事件,稍后会详细介绍):

    <asp:Repeater id="rptAttendance" runat="server" OnItemDataBound="rptAttendance_ItemDataBound">
        <ItemTemplate>
            <tr>
                <td><asp:Label ID="lblEmpName" runat="server" /></td>
                <asp:Repeater ID="rptAttendanceCode" runat="server" OnItemDataBound="rptAttendanceCode_ItemDataBound" >
                    <ItemTemplate><td><asp:Label ID="lblAttendanceCode" runat="server" /></td></ItemTemplate>
                </asp:Repeater>
                </tr>
        </ItemTemplate>
    </asp:Repeater>

现在,这就是乐趣的开始!

首先,您需要创建一个数据结构来保存员工姓名和他/她每月每一天的出勤率。我们将使用该WorkingDays字段作为我们的基线并将其附加到每个员工的出勤(取自出勤表)中。

//This list (info) holds the employee's name in the first string, and their attendance in the string array
List<Tuple<string, string[]>> info = new List<Tuple<string, string[]>>();
int calendarID = calendar.Rows[0].Field<int>("CalendarID");
string days = calendar.Rows[0].Field<string>("WorkingDays");

将日期类型代码替换为相应的字母

days = days.Replace("1", "P");
days = days.Replace("0", "H");

将其转换为一个数组,以便我们可以对其进行迭代

string[] daysList = days.Select(d => d.ToString()).ToArray();

现在我们将遍历每个员工记录并为每个员工创建一个数据结构。然后我们将遍历每个员工的休息日,并用他们不工作的日子更新他们的日程表集合。

foreach (DataRow employee in employees.Rows)
{
    employeeAttendance = new Tuple<string, string[]>(employee.Field<string>("EmpName"), daysList);
    foreach (DataRow absentDay in attendance.AsEnumerable().Where(a => a.Field<int>("EmpID") == employee.Field<int>("EmpID")))
{
        employeeAttendance.Item2[absentDay.Field<int>("LeaveDayOfMonth") - 1] = "A";
}
    info.Add(employeeAttendance);
}

以下是每个转发器的 ItemDataBound 事件处理程序:

protected void rptAttendance_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Tuple<string, string[]> info = (Tuple<string, string[]>)e.Item.DataItem;
        ((Label)e.Item.FindControl("lblEmpName")).Text = info.Item1;
        Repeater attendanceCode = (Repeater)e.Item.FindControl("rptAttendanceCode");
        attendanceCode.DataSource = info.Item2;
        attendanceCode.DataBind();
    }
}

protected void rptAttendanceCode_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        ((Label)e.Item.FindControl("lblAttendanceCode")).Text = e.Item.DataItem.ToString();
    }
}

这是完整的代码隐藏:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            List<Tuple<string, string[]>> info = new List<Tuple<string, string[]>>();
            int calendarID = calendar.Rows[0].Field<int>("CalendarID");
            string days = calendar.Rows[0].Field<string>("WorkingDays");
            days = days.Replace("1", "P");
            days = days.Replace("0", "H");
            string[] daysList = days.Select(d => d.ToString()).ToArray();
            Tuple<string, string[]> employeeAttendance = null;
            foreach (DataRow employee in employees.Rows)
            {
                employeeAttendance = new Tuple<string, string[]>(employee.Field<string>("EmpName"), daysList);
                foreach (DataRow absentDay in attendance.AsEnumerable().Where(a => a.Field<int>("EmpID") == employee.Field<int>("EmpID")))
                {
                    employeeAttendance.Item2[absentDay.Field<int>("LeaveDayOfMonth") - 1] = "A";

                }
                info.Add(employeeAttendance);
            }
            this.rptAttendance.DataSource = info;
            this.rptAttendance.DataBind();
        }
    }

    protected void rptAttendance_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Tuple<string, string[]> info = (Tuple<string, string[]>)e.Item.DataItem;
            ((Label)e.Item.FindControl("lblEmpName")).Text = info.Item1;
            Repeater attendanceCode = (Repeater)e.Item.FindControl("rptAttendanceCode");
            attendanceCode.DataSource = info.Item2;
            attendanceCode.DataBind();
        }
    }

    protected void rptAttendanceCode_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ((Label)e.Item.FindControl("lblAttendanceCode")).Text = e.Item.DataItem.ToString();
        }
    }
于 2013-07-02T22:08:02.490 回答