简体   繁体   English

带数据源的ASP日历与表中不存在的日期没有区别

[英]asp calendar with datasource doesn't difference on dates that are not present on table

So basically it is an asp calendar which shows the number of leave applied by employees and everytime someone applies for leave the slot number goes down. 因此,基本上,它是一个asp日历,显示员工申请的休假次数,每次有人提出请假时,槽位号都会减少。 That much is working fine, but I'm trying to set the available slots from database. 大部分工作正常,但是我正在尝试设置数据库中的可用插槽。 So I have a data column on table setshrinkage where it fetches the headcounts set for some dates and I try to subtract the count of the particular dates from approved table with headcounts. 所以我在表setshrinkage上有一个数据列,它在其中获取某些日期设置的人数,然后尝试从带有人数的已批准表中减去特定日期的计数。 The table for the headcount is as shown below. 人数表如下所示。

在此处输入图片说明

So for example in the calendar on 27-08-2018 I want to subtract 9 minus the count of 27-08-2018 from approved table. 因此,例如在27-08-2018的日历中,我想从批准的表格中减去9减去27-08-2018的计数。 But when I try this I'm getting an error Object reference not set to an instance of an object. 但是,当我尝试这样做时,我收到一个错误,对象引用未设置为对象的实例。 I understand that it is because of the null datas for the remaining dates. 我了解这是因为剩余日期的数据为空。 So how can I get this to work? 那么我怎样才能使它工作呢? Thanks in advance. 提前致谢。

class Sample
{
    public DateTime Date { get; set; }
    public int SlotAvailable { get; set; }
    public int Pending { get; set; }
    public int HeadCount { get; set; }
} 

List<Sample> samples = new List<Sample>();

protected void FillLeaveplannerDataset()
{
    cal2.VisibleDate = cal2.TodaysDate;
    DateTime firstDate = new DateTime(cal2.VisibleDate.Year, cal2.VisibleDate.Month, 1).AddDays(-6);
    DateTime lastDate = new DateTime(cal2.VisibleDate.Date.AddMonths(1).Year, cal2.VisibleDate.Date.AddMonths(1).Month, 1).AddDays(7);
    dsleaveplanner = GetCurrentMonthData(firstDate, lastDate);
}

protected DateTime GetFirstDayOfNextMonth()
{
    int monthNumber, yearNumber;
    if (cal2.VisibleDate.Month == 12)
    {
        monthNumber = 1;
        yearNumber = cal2.VisibleDate.Year + 1;
    }
    else
    {
        monthNumber = cal2.VisibleDate.Month + 1;
        yearNumber = cal2.VisibleDate.Year;
    }
    DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
    return lastDate;
}

protected DataSet GetCurrentMonthData(DateTime firstDate, DateTime lastDate)
{
    DataSet dsMonth = new DataSet();
    MySqlConnection con = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
    string caldate = "Select * From approved Where date >= @firstDate And date <= @lastDate And site=@site And skill=@skill And shift=@shift Group By date";
    MySqlCommand cmd = new MySqlCommand(caldate, con);
    cmd.Parameters.AddWithValue("@firstDate", firstDate);
    cmd.Parameters.AddWithValue("@lastDate", lastDate);
    cmd.Parameters.AddWithValue("@site", lblsite.Text);
    cmd.Parameters.AddWithValue("@skill", lblskill.Text);
    cmd.Parameters.AddWithValue("@shift", lblshift.Text);
    MySqlDataAdapter mysqlDataAdapter = new MySqlDataAdapter(cmd);

    try
    {
         mysqlDataAdapter.Fill(dsMonth);
    }
    catch { }

    return dsMonth;
}

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    foreach (DataRow dr in dsleaveplanner.Tables[0].Rows)
    {
        nextDate = (DateTime)dr["date"];
        MySqlConnection conn = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
        string hc = "SELECT headCount FROM setshrinkage WHERE date = @date";
        MySqlCommand cmd1 = new MySqlCommand(hc, conn);
        cmd1.Parameters.AddWithValue("@date", nextDate);
        conn.Open();
        string hcount = cmd1.ExecuteReader().ToString();
        Int32 hcount1 = Convert.ToInt32(hcount);
        conn.Close();
        string cntdate = "SELECT COUNT(date) FROM approved WHERE date = @date";
        string cntdate2 = "SELECT COUNT(date) FROM pending WHERE date = @date";
        MySqlCommand cmd2 = new MySqlCommand(cntdate, conn);
        MySqlCommand cmd3 = new MySqlCommand(cntdate2, conn);
        cmd2.Parameters.AddWithValue("@date", nextDate);
        cmd3.Parameters.AddWithValue("@date", nextDate);
        conn.Open();
        string count = cmd2.ExecuteScalar().ToString();
        string count2 = cmd3.ExecuteScalar().ToString();
        var slot2 = Convert.ToInt32(count);
        Int32 slot3 = hcount1 - slot2;
        string slot4 = slot3.ToString();
        conn.Close();

        samples.Add(new Sample { Date = nextDate, SlotAvailable = slot4, Pending = count2, HeadCount = hcount1 });
    }

    if (samples.Any(x => x.Date == e.Day.Date))
    {
        e.Cell.BackColor = System.Drawing.Color.Orange;
        Environment.NewLine.ToString();
        e.Cell.ForeColor = System.Drawing.Color.Red;
        e.Cell.Font.Size = 9;
        e.Cell.Controls.Add(new LiteralControl("<p></p>Slot available:"));
        e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().SlotAvailable.ToString()));
        e.Cell.Controls.Add(new LiteralControl("<p></p>Pending:"));
        e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().Pending.ToString()));
    }
    else
    {
        e.Cell.Font.Size = 9;
        e.Cell.Controls.Add(new LiteralControl("<p>Slot available: </p>"));
        e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().HeadCount.ToString()));
        e.Cell.Controls.Add(new LiteralControl("<p></p>Pending: 0"));
    }
}

I've found a way to fix the error with the below code, 我找到了一种使用以下代码修复错误的方法,

    protected DataSet GetCurrentMonthData(DateTime firstDate, DateTime lastDate)
    {
        DataSet dsMonth = new DataSet();
        MySqlConnection con = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
        string caldate = "Select * From setshrinkage Where date >= @firstDate And date <= @lastDate Group By date";
        MySqlCommand cmd = new MySqlCommand(caldate, con);

        cmd.Parameters.AddWithValue("@firstDate", firstDate);
        cmd.Parameters.AddWithValue("@lastDate", lastDate);
        MySqlDataAdapter mysqlDataAdapter = new MySqlDataAdapter(cmd);
        try
        {
            mysqlDataAdapter.Fill(dsMonth);
        }
        catch { }
        return dsMonth;
    }

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{

    DateTime nextDate;
    if (dsleaveplanner != null)
    {

            foreach (DataRow dr in dsleaveplanner.Tables[0].Rows)
            {
                nextDate = (DateTime)dr["date"];
                var hcount = (dr["headCount"].ToString());
                Int32 hcount1 = Convert.ToInt32(hcount);

                MySqlConnection conn = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
                string cntdate = "SELECT COUNT(date) FROM approved WHERE date = @date";
                string cntdate2 = "SELECT COUNT(date) FROM pending WHERE date = @date";
                MySqlCommand cmd2 = new MySqlCommand(cntdate, conn);
                MySqlCommand cmd3 = new MySqlCommand(cntdate2, conn);
                cmd2.Parameters.AddWithValue("@date", nextDate);
                cmd3.Parameters.AddWithValue("@date", nextDate);
                conn.Open();
                string count = cmd2.ExecuteScalar().ToString();
                string count2 = cmd3.ExecuteScalar().ToString();
                var slot2 = Convert.ToInt32(count);
                Int32 slot3 = hcount1 - slot2;
                string slot1 = Convert.ToString(slot3);
                string slot4 = slot3.ToString();
                conn.Close();

                samples.Add(new Sample { Date = nextDate, SlotAvailable = slot1, Pending = count2 });
                if (samples.Any(x => x.Date == e.Day.Date))
                {
                    Environment.NewLine.ToString();
                    e.Cell.Font.Size = 11;
                    e.Cell.Controls.Add(new LiteralControl("<p></p>Slot available:"));
                    e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().SlotAvailable.ToString()));
                    e.Cell.Controls.Add(new LiteralControl("<p></p>Pending:"));
                    e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().Pending.ToString()));

                }
                else
                {
                    e.Cell.ForeColor = System.Drawing.Color.Red;
                    e.Cell.Font.Size = 9;
                    e.Cell.Controls.Add(new LiteralControl("<p></p>Slot available:"));
                    e.Cell.Controls.Add(new LiteralControl(slot1));
                    e.Cell.Controls.Add(new LiteralControl("<p></p>Pending:0"));
                }
            }
    }

But I get an entirely different problem with this as shown in the below image 但是我遇到了一个完全不同的问题,如下图所示 在此处输入图片说明

The labels gets repeated as shown above. 标签会重复显示,如上所示。 Can anyone help me fix it? 谁能帮我解决这个问题?

It is fixed by putting the conditions out of the loop. 通过将条件置于循环之外来解决此问题。 The calendar looks neat. 日历看起来很整洁。 Thanks for the support 感谢您的支持

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{

DateTime nextDate;
if (dsleaveplanner != null)
{

        foreach (DataRow dr in dsleaveplanner.Tables[0].Rows)
        {
            nextDate = (DateTime)dr["date"];
            var hcount = (dr["headCount"].ToString());
            Int32 hcount1 = Convert.ToInt32(hcount);

            MySqlConnection conn = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
            string cntdate = "SELECT COUNT(date) FROM approved WHERE date = @date";
            string cntdate2 = "SELECT COUNT(date) FROM pending WHERE date = @date";
            MySqlCommand cmd2 = new MySqlCommand(cntdate, conn);
            MySqlCommand cmd3 = new MySqlCommand(cntdate2, conn);
            cmd2.Parameters.AddWithValue("@date", nextDate);
            cmd3.Parameters.AddWithValue("@date", nextDate);
            conn.Open();
            string count = cmd2.ExecuteScalar().ToString();
            string count2 = cmd3.ExecuteScalar().ToString();
            var slot2 = Convert.ToInt32(count);
            Int32 slot3 = hcount1 - slot2;
            string slot1 = Convert.ToString(slot3);
            string slot4 = slot3.ToString();
            conn.Close();

            samples.Add(new Sample { Date = nextDate, SlotAvailable = slot1, Pending = count2 });

        }
        if (samples.Any(x => x.Date == e.Day.Date))
            {
                Environment.NewLine.ToString();
                e.Cell.Font.Size = 11;
                e.Cell.Controls.Add(new LiteralControl("<p></p>Slot available:"));
                e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().SlotAvailable.ToString()));
                e.Cell.Controls.Add(new LiteralControl("<p></p>Pending:"));
                e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().Pending.ToString()));

            }
            else
            {
                e.Cell.ForeColor = System.Drawing.Color.Red;
                e.Cell.Font.Size = 9;
                e.Cell.Controls.Add(new LiteralControl("<p></p>Slot available:"));
                e.Cell.Controls.Add(new LiteralControl(slot1));
                e.Cell.Controls.Add(new LiteralControl("<p></p>Pending:0"));
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM