简体   繁体   中英

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. 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. 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. 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"));
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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