Avatar of Brian
Brian
Flag for United States of America asked on

Display multiple entries in Calendar Control using DayRender Event

Hello EE,

I have the following code below that works fine displaying a Calendar's Cell with the Background Color and text. But the problem that I now face is that a user can have multiple entries for any given date. A user can Walk, Run, Swim and Golf for the date 8/11 and I need a way to display those different entries along with each entries color (color coded) for easier viewing.

My code as is follows below. I could really use alot of help on this post. I have had other EE members help me so far but this post was out of the range from my previous post.

I'm using ASP.NET 4.0 C#. I'm NOT using any third party controls. I have to use ASP.NET Calendar Control.

Thanks in advance!!


CODEBEHIND:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ApplicationProcess_Secure_PhysicalActivityLog : System.Web.UI.Page
{
    string[,] palArr = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        hf_AppID.Value = Session["pi_id"].ToString();

        RetrieveCalendarNames();

        if (!IsPostBack)
        {
            getPalDates();
        }
    }

    //GET AVAILABLE DATE AND ITS CORRESPONDING NAME IN AN ARRAY
    protected void getPalDates()
    {
        int pi_id = Convert.ToInt32(Session["pi_id"]);

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessChoice"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "WellnessChoice_RetrieveDatesForCalendar";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        cmd.Parameters.AddWithValue("@pi_id", SqlDbType.Int).Value = pi_id;

        DataTable dtDates = new DataTable();

        SqlDataAdapter adp = new SqlDataAdapter();

        try
        {
            conn.Open();

            adp.SelectCommand = cmd;
            adp.Fill(dtDates);
            palArr = new string[13, 32];
            for (int iCnt = 0; iCnt <= dtDates.Rows.Count - 1; iCnt++)
            {
                Calendar1.SelectedDates.Add(Convert.ToDateTime(dtDates.Rows[0]["pal_date"]));
                DateTime dm = Convert.ToDateTime(dtDates.Rows[iCnt]["pal_date"]);
                palArr[dm.Month, dm.Day] = dtDates.Rows[iCnt]["palv_code"].ToString() + "|" + dtDates.Rows[iCnt]["palv_id"].ToString();
            }
        }

        catch (Exception ex)
        {
            ex.Message.ToString();
        }

        finally
        {
            conn.Close();
        }
    }

    //CUSTOMIZE UR CALENDAR RENDER EVENT NOW
    protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
    {
        // remove if (!IsPostBack) if running into Render errors
        if (!IsPostBack)
        {
            CalendarDay day = (CalendarDay)e.Day;
            TableCell cell = (TableCell)e.Cell;

            // if (!day.IsOtherMonth)
            if (!day.IsOtherMonth && palArr[day.Date.Month, day.Date.Day] != null)
            {
                string palName = palArr[day.Date.Month, day.Date.Day];

                if (palName != null)
                {
                    var p = palName.Split('|');
                    if (p[1].ToString() == "2")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 215);
                    }
                    else if (p[1].ToString() == "3")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 140);
                    }
                    else if (p[1].ToString() == "4")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 181, 138);
                    }
                    else if (p[1].ToString() == "5")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 204, 138);
                    }
                    else if (p[1].ToString() == "6")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 229, 138);
                    }
                    else if (p[1].ToString() == "7")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 254, 138);
                    }
                    else if (p[1].ToString() == "8")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 221, 255, 138);
                    }
                    else if (p[1].ToString() == "9")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 146, 249, 134);
                    }
                    else if (p[1].ToString() == "10")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 138, 238, 255);
                    }
                    else if (p[1].ToString() == "11")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 136, 199, 252);
                    }
                    else if (p[1].ToString() == "12")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 133, 146, 246);
                    }
                    else if (p[1].ToString() == "13")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 195, 131, 242);
                    }

                    Label lbl = new Label();
                    lbl.Text = "<br>" + p[0];
                    e.Cell.Controls.Add(lbl);
                }
            }
        }
    }

    protected void MyCal_MonthChange(object sender, MonthChangedEventArgs e)
    {
        getPalDates();
    }

    protected void MyCal_SelectionChanged(object sender, EventArgs e)
    {
        calendarExt.Show();

        if (!IsPostBack)
        {
            RetrieveCalendarNames();
        }
    }

    protected void RetrieveCalendarNames()
    {
        if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessChoice"].ConnectionString);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "WellnessChoice_TestTest";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                ddl_Name.DataSource = rdr;
                ddl_Name.DataValueField = "palv_id";
                ddl_Name.DataTextField = "palv_names";
                ddl_Name.DataBind();

                rdr.Close();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

    protected void btn_SavePhysicalActivityLog_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessChoice"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "WellnessChoice_InsertPhysicalActivityLog";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        cmd.Parameters.AddWithValue("@pi_id", SqlDbType.Int).Value = hf_AppID.Value;
        cmd.Parameters.AddWithValue("@palv_id", SqlDbType.Int).Value = ddl_Name.SelectedItem.Value;
        cmd.Parameters.AddWithValue("@pal_date", SqlDbType.DateTime).Value = Calendar1.SelectedDate.ToShortDateString();
        //cmd.Parameters.AddWithValue("@pal_id", SqlDbType.Int).Value = hf_pal_id.Value;

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }

        catch (Exception ex)
        {
            // ex.Message.ToString();
            lblSaveError.Text = ("Error on insert: " + ex.Message.ToString());
        }

        finally
        {
            Response.Redirect("PhysicalActivityLog.aspx");
            conn.Close();
        }
    }
}



HTML MARKUP:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PhysicalActivityLog.aspx.cs" Inherits="ApplicationProcess_Secure_PhysicalActivityLog" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Wellness Choice Program - Physical Activity Log</title>
    <link rel="stylesheet" href="../../css/main.css" type="text/css" />
</head>
<body>
    <form id="form1" runat="server" defaultfocus="txtFirstName">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div id="wrapper">
        <div id="header">
            <div id="logo">
                <img src="../../images/logo.png" alt="The Wellness Choice Program Logo" />
            </div>
            <div id="nav">
            </div>    
        </div>
        <div id="content">
            <div id="primary">
                <h1>Physical Activity Log</h1>
                                <asp:Button runat="server" ID="targetBtn" style="display:none;"/>
                <asp:Panel runat="server" style="display:none;" CssClass="modalPopup" ID="calendarPopup" Width="300px" Height="300px">        
                    <!-- your form elements -->
                    <asp:Label ID="lblName" runat="server" CssClass="label" Text="Select Event to Perform: "></asp:Label><br />
                    <asp:DropDownList ID="ddl_Name" CssClass="ddl" runat="server"></asp:DropDownList><br /><br />
                    <br />
                    <asp:HiddenField ID="hf_AppID" runat="server" />
                    <asp:HiddenField ID="hf_pal_id" runat="server" />
                    <asp:Button ID="btn_SavePhysicalActivityLog" runat="server" Text="Save" onclick="btn_SavePhysicalActivityLog_Click" />
                    <asp:Button ID="CancelBtn" Text="Cancel" runat="server" /><br />
                    <asp:Label ID="lblSaveError" runat="server"></asp:Label>
                </asp:Panel>
                <asp:ModalPopupExtender ID="calendarExt"
                            runat="server"
                            TargetControlID="targetBtn"
                            PopupControlID="calendarPopup"
                            CancelControlID="CancelBtn"
                             BackgroundCssClass="modalBackground"
                        />
                <asp:Calendar ID="Calendar1" runat="server" BackColor="White"
                    BorderColor="#CCCCCC" BorderWidth="1px" Font-Names="Verdana" Font-Size="8pt"
                    ForeColor="#333333" Height="400px" Width="520px"
                    OnDayRender="MyCal_DayRender" OnVisibleMonthChanged="MyCal_MonthChange" OnSelectionChanged="MyCal_SelectionChanged" DayNameFormat="Shortest"
                    ShowGridLines="True">
                    <DayHeaderStyle Font-Bold="True" BackColor="#027ABB" ForeColor="White"
                        Height="30px" />
                    <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
                    <OtherMonthDayStyle ForeColor="#CC9966" />
                    <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                    <SelectorStyle BackColor="#FFCC66" />
                    <TitleStyle BackColor="#73BE1E"
                        Font-Bold="True" Font-Size="9pt" ForeColor="White" />
                    <TodayDayStyle BackColor="#CCCCCC" ForeColor="#333333" />
                </asp:Calendar>
                <br />
            </div>
            <div id="secondary">
                <h1></h1>
                <ul>
                    <li><asp:HyperLink ID="hl_HealthRiskAssess" runat="server" NavigateUrl="~/ApplicationProcess/Secure/HealthRiskAssess.aspx">Health Risk Assessment</asp:HyperLink></li>
                    <li><asp:HyperLink ID="hl_AnnualPhysical" runat="server" NavigateUrl="~/ApplicationProcess/Secure/AnnualPhysical.aspx">Annual Physical</asp:HyperLink></li>
                    <li><asp:HyperLink ID="hl_PreventiveScreenings" runat="server" NavigateUrl="~/ApplicationProcess/Secure/PreventiveScreenings.aspx">Preventive Screenings</asp:HyperLink></li>
                    <li><asp:HyperLink ID="hl_GeneralHealthOne" runat="server" NavigateUrl="~/ApplicationProcess/Secure/GeneralHealthAwarnessProgramOne.aspx">General Health Awareness Program 1</asp:HyperLink></li>
                    <li><asp:HyperLink ID="hl_GeneralHealthTwo" runat="server" NavigateUrl="~/ApplicationProcess/Secure/GeneralHealthAwarnessProgramTwo.aspx">General Health Awareness Program 2</asp:HyperLink></li>
                    <li><asp:HyperLink ID="hl_GeneralHealthThree" runat="server" NavigateUrl="~/ApplicationProcess/Secure/GeneralHealthAwarnessProgramThree.aspx">General Health Awareness Program 3</asp:HyperLink></li>
                    <li>Physical Activity Log</li>
                </ul>
            </div>
        </div>
        <div id="footer">  
        </div>
    </div>
    </form>
</body>
</html>
ASP.NET.NET Programming

Avatar of undefined
Last Comment
Brian

8/22/2022 - Mon
libby9284

Hello again!  You can add multiple labels to a cell in your dayrender code.  I've done this on a site I built for someone else but without the color changing aspect.  

My solution to this on that site was to get all events for the month and loop through all of them for each dayrender event, checking if the date of the DB event item matched the dayrender date and then adding the labels to that event if it matched.  A hash or dictionary probably would have been more efficient but it works well enough as it is for that site.

Below is an example, tbl is a DataTable object and t is a DateTime object.  The code runs when the DayRender event fires

if (tbl.Rows.Count != 0 && tbl.Rows[0].ItemArray.Length != 0)
        {
           // each row is an event for the calendar
            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                t = (DateTime)tbl.Rows[i].ItemArray[1];
              
                // check the day of the event from the DB matches the date of the dayrender event
                if (t.Day == e.Day.Date.Day && t.Month == e.Day.Date.Month)
                {
                    // event exists for this day in the calendar
                    myLabel.CssClass = "eventLabel";

                    // set the label text to the title of the event
                    myLabel.Text = "<br/>" + (string)tbl.Rows[i].ItemArray[2];//+ "<br/>";

                    // give the label an ID
                    myLabel.ID = "eventLabel" + e.Day.Date.Month + "_" + e.Day.Date.Day;

                    // add the label
                    e.Cell.Controls.Add(myLabel);

                    // reset label object for the top of the loop
                    myLabel = new Label();
                }
            }
        }

Open in new window

Brian

ASKER
Hello libby9284,

The adding multiple labels to a cell sounds like exactly what i need for this. How can I implemenet what you have above with the code below for the DayRender Event?

//CUSTOMIZE UR CALENDAR RENDER EVENT NOW
    protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
    {
        // remove if (!IsPostBack) if running into Render errors
        if (!IsPostBack)
        {
            CalendarDay day = (CalendarDay)e.Day;
            TableCell cell = (TableCell)e.Cell;

            // if (!day.IsOtherMonth)
            if (!day.IsOtherMonth && palArr[day.Date.Month, day.Date.Day] != null)
            {
                string palName = palArr[day.Date.Month, day.Date.Day];

                if (palName != null)
                {
                    var p = palName.Split('|');
                    if (p[1].ToString() == "2")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 215);
                    }
                    else if (p[1].ToString() == "3")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 140);
                    }
                    else if (p[1].ToString() == "4")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 181, 138);
                    }
                    else if (p[1].ToString() == "5")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 204, 138);
                    }
                    else if (p[1].ToString() == "6")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 229, 138);
                    }
                    else if (p[1].ToString() == "7")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 254, 138);
                    }
                    else if (p[1].ToString() == "8")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 221, 255, 138);
                    }
                    else if (p[1].ToString() == "9")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 146, 249, 134);
                    }
                    else if (p[1].ToString() == "10")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 138, 238, 255);
                    }
                    else if (p[1].ToString() == "11")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 136, 199, 252);
                    }
                    else if (p[1].ToString() == "12")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 133, 146, 246);
                    }
                    else if (p[1].ToString() == "13")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 195, 131, 242);
                    }

                    Label lbl = new Label();
                    lbl.Text = "<br>" + p[0];
                    e.Cell.Controls.Add(lbl);
                }
            }
        }
    }
libby9284

you may have to change the way you're storing the data in your app from the DB in the first place.  It looks like the way you have it above that you'll only see the last event for the date render.  

Instead of
palArr[dm.Month, dm.Day] = dtDates.Rows[iCnt]["palv_code"].ToString() + "|" + dtDates.Rows[iCnt]["palv_id"].ToString(); 

Open in new window


in getPalDates(), use a delimiter and concatenate.  I changed your code below to use a comma (if commas are expected for event data, use some other unexpected character).

palArr[dm.Month, dm.Day] += dtDates.Rows[iCnt]["palv_code"].ToString() + "|" + dtDates.Rows[iCnt]["palv_id"].ToString() + ",";


then, in your day render event, change your function as follows:

protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
    {
        // remove if (!IsPostBack) if running into Render errors
        if (!IsPostBack)
        {
            CalendarDay day = (CalendarDay)e.Day;
            TableCell cell = (TableCell)e.Cell;

            // if (!day.IsOtherMonth)
            if (!day.IsOtherMonth && palArr[day.Date.Month, day.Date.Day] != null)
            {
                //string palName = palArr[day.Date.Month, day.Date.Day];
                string[] palEvents = palArr[day.Date.Month, day.Date.Day].Split(',');
                for (int i = 0; i<palEvents.length; i++)
                {
                palName=palEvents[i];
                if (palName != null)
                {
                    var p = palName.Split('|');
                    if (p[1].ToString() == "2")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 215);
                    }
                    else if (p[1].ToString() == "3")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 140);
                    }
                    else if (p[1].ToString() == "4")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 181, 138);
                    }
                    else if (p[1].ToString() == "5")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 204, 138);
                    }
                    else if (p[1].ToString() == "6")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 229, 138);
                    }
                    else if (p[1].ToString() == "7")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 254, 138);
                    }
                    else if (p[1].ToString() == "8")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 221, 255, 138);
                    }
                    else if (p[1].ToString() == "9")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 146, 249, 134);
                    }
                    else if (p[1].ToString() == "10")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 138, 238, 255);
                    }
                    else if (p[1].ToString() == "11")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 136, 199, 252);
                    }
                    else if (p[1].ToString() == "12")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 133, 146, 246);
                    }
                    else if (p[1].ToString() == "13")
                    {
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 195, 131, 242);
                    }

                    Label lbl = new Label();
                    lbl.Text = "<br>" + p[0];
                    e.Cell.Controls.Add(lbl);
                }
            }
            }
        }
    }

Open in new window



if this doesn't help, please post sample data so I can better understand what's happening.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Brian

ASKER
Hi libby9284,

First of all I would like to thank you for your continued support for helping me out. I have never used the Calendar Class before with all it's Events, methods, etc... and I feel dumb because of it, so thank you again for helping me out....

As for the code you posted above, I did make the chagnes you suggested to test out and see what happens but please see the error messages i'm getting with red lines below.

LINE OF CODE:
for (int i = 0; i < palEvents.length; i++)

RED LINE MESSAGE:
'System.Array' does not contain a definition for 'length' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

LINE OF CODE:
palName = palEvents

RED LINE MESSAGE:
The name 'palName' does not exist in the current context.

LINE OF CODE:
if (palName != null)

RED LINE MESSAGE:
The name 'palName' does not exist in the current context.

LINE OF CODE:
var p = palName.Split('|');

RED LINE MESSAGE:
The name 'palName' does not exist in the current context.

>> if this doesn't help, please post sample data so I can better understand what's happening.

I can give you a sample of the data to gets added to the DB for the Calendar Control.

You can see below that pi_id -> 1277 has 4 entries for 8/20/2011 and in the event that a pi_id has more than one entry for a particular day then I need to display them.

pal_id     pi_id    palv_id    pal_date
4000      1277    4              8/20/2011
4001      1277    8              8/20/2011
4002      1277    9              8/20/2011
4003      1277    12            8/20/2011
4004      1277    2              8/26/2011
libby9284

Glad to help!  No reason to feel dumb about it, I spent a long time playing with this class for a site for someone else so its nice to be able to help someone else with it.

Sorry for the two mistakes there, use palEvents.Count instead of palEvents.length.  I'm always doing that in my code :-p.  And, put
string palName;

Open in new window

before the loop.  

Thanks for sending the data too, it reinforces what I thought the problem was.
Brian

ASKER
It just gets frustrating when you need to have something done and don't know where to begin, especially for a Class that I never used. So thank you again for your continued help, patience, and knowledge...

I'm going to supply the code to make sure I added your code above correctly. I now only have one RED LINE. please see below.

LINE OF CODE:
for (int i = 0; i < palEvents.Count; i++)

RED LINE MESSAGE:
'System.Array' does not contain a definition for 'count' and no extension method 'count' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)


//CUSTOMIZE UR CALENDAR RENDER EVENT NOW
    protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
    {
        // remove if (!IsPostBack) if running into Render errors
        if (!IsPostBack)
        {
            CalendarDay day = (CalendarDay)e.Day;
            TableCell cell = (TableCell)e.Cell;

            // if (!day.IsOtherMonth)
            if (!day.IsOtherMonth && palArr[day.Date.Month, day.Date.Day] != null)
            {
                //string palName = palArr[day.Date.Month, day.Date.Day];
                string[] palEvents = palArr[day.Date.Month, day.Date.Day].Split(',');
                string palName;
                for (int i = 0; i < palEvents.Count; i++)
                {
                    palName = palEvents
                    if (palName != null)
                    {
                        var p = palName.Split('|');
                        if (p[1].ToString() == "2")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 215);
                        }
                        else if (p[1].ToString() == "3")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 140);
                        }
                        else if (p[1].ToString() == "4")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 181, 138);
                        }
                        else if (p[1].ToString() == "5")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 204, 138);
                        }
                        else if (p[1].ToString() == "6")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 229, 138);
                        }
                        else if (p[1].ToString() == "7")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 254, 138);
                        }
                        else if (p[1].ToString() == "8")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 221, 255, 138);
                        }
                        else if (p[1].ToString() == "9")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 146, 249, 134);
                        }
                        else if (p[1].ToString() == "10")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 138, 238, 255);
                        }
                        else if (p[1].ToString() == "11")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 136, 199, 252);
                        }
                        else if (p[1].ToString() == "12")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 133, 146, 246);
                        }
                        else if (p[1].ToString() == "13")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 195, 131, 242);
                        }

                        Label lbl = new Label();
                        lbl.Text = "<br>" + p[0];
                        e.Cell.Controls.Add(lbl);
                    }
                }
            }
        }
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
libby9284

use palEvents.Length, a typo caused the error the first time, count is for something else (sorry for the confusion)
Brian

ASKER
Ok, palEvents.Length worked.

When I run the page i get the following error message below

LINE OF CODE:
if (p[1].ToString() == "2")

ERROR MESSAGE:
Index was outside the bounds of the array.
libby9284

change palName = palEvents to
palName = palEvents[i]

Open in new window


This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Brian

ASKER
That is what I had already. For some reason I cannot upload the code to you with the bracket witht he i in it. When I try add that symbol I get the following error messgae below from EE. That is why I had to strip it out of the above code.

All tags must have a closing tag.
Brian

ASKER
Below is the code that gives me the error message below.

LINE OF CODE:
if (p[1].ToString() == "2")

ERROR MESSAGE:
Index was outside the bounds of the array.
//CUSTOMIZE UR CALENDAR RENDER EVENT NOW
    protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
    {
        // remove if (!IsPostBack) if running into Render errors
        if (!IsPostBack)
        {
            CalendarDay day = (CalendarDay)e.Day;
            TableCell cell = (TableCell)e.Cell;

            // if (!day.IsOtherMonth)
            if (!day.IsOtherMonth && palArr[day.Date.Month, day.Date.Day] != null)
            {
                //string palName = palArr[day.Date.Month, day.Date.Day];
                string[] palEvents = palArr[day.Date.Month, day.Date.Day].Split(',');
                string palName;
                for (int i = 0; i < palEvents.Length; i++)
                {
                    palName = palEvents[i];
                    if (palName != null)
                    {
                        var p = palName.Split('|');
                        if (p[1].ToString() == "2")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 215);
                        }
                        else if (p[1].ToString() == "3")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 140);
                        }
                        else if (p[1].ToString() == "4")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 181, 138);
                        }
                        else if (p[1].ToString() == "5")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 204, 138);
                        }
                        else if (p[1].ToString() == "6")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 229, 138);
                        }
                        else if (p[1].ToString() == "7")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 254, 138);
                        }
                        else if (p[1].ToString() == "8")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 221, 255, 138);
                        }
                        else if (p[1].ToString() == "9")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 146, 249, 134);
                        }
                        else if (p[1].ToString() == "10")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 138, 238, 255);
                        }
                        else if (p[1].ToString() == "11")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 136, 199, 252);
                        }
                        else if (p[1].ToString() == "12")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 133, 146, 246);
                        }
                        else if (p[1].ToString() == "13")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 195, 131, 242);
                        }

                        Label lbl = new Label();
                        lbl.Text = "<br>" + p[0];
                        e.Cell.Controls.Add(lbl);
                    }
                }
            }
        }

Open in new window

libby9284

ok.  Perhaps the issue is empty entries from the split?

if so, there's 2 options:

- change the split on the ',' character so that empty entries are removed (requires adding / changing the following)
... the rest of your code before the for loop
char[] delim = {','};
for (int i = 0; i < palEvents.Length; i++)
                {
                    palName = palEvents[i];
... the rest of your code as it already is

Open in new window


- changing the if statement to ((palName != null) && (p.Length > 1))
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Brian

ASKER
Hi libby9284,

Please see the following code below and the error message that I receive when first loading the page. I'm not sure if I placed what you mentioned above in the correct spot.

Also, when I tried performing your second option the if statement to ((palName != null) && (p.Length > 1)) . I got a red line with the following message below on this same line.

Red Line message:
The name 'p' does not exist in this current context.

Line of Code:
if (p[1].ToString() == "2")

Error Message:
Index was outside the bounds of the array.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ApplicationProcess_Secure_PhysicalActivityLog : System.Web.UI.Page
{
    string[,] palArr = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        hf_AppID.Value = Session["pi_id"].ToString();

        RetrieveCalendarNames();

        if (!IsPostBack)
        {
            getPalDates();
        }
    }

    //GET AVAILABLE DATE AND ITS CORRESPONDING NAME IN AN ARRAY
    protected void getPalDates()
    {
        int pi_id = Convert.ToInt32(Session["pi_id"]);

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessChoice"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "WellnessChoice_RetrieveDatesForCalendar";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        cmd.Parameters.AddWithValue("@pi_id", SqlDbType.Int).Value = pi_id;

        DataTable dtDates = new DataTable();

        SqlDataAdapter adp = new SqlDataAdapter();

        try
        {
            conn.Open();

            adp.SelectCommand = cmd;
            adp.Fill(dtDates);
            palArr = new string[13, 32];
            for (int iCnt = 0; iCnt <= dtDates.Rows.Count - 1; iCnt++)
            {
                Calendar1.SelectedDates.Add(Convert.ToDateTime(dtDates.Rows[0]["pal_date"]));
                DateTime dm = Convert.ToDateTime(dtDates.Rows[iCnt]["pal_date"]);
                // palArr[dm.Month, dm.Day] = dtDates.Rows[iCnt]["palv_code"].ToString() + "|" + dtDates.Rows[iCnt]["palv_id"].ToString();
                palArr[dm.Month, dm.Day] += dtDates.Rows[iCnt]["palv_code"].ToString() + "|" + dtDates.Rows[iCnt]["palv_id"].ToString() + ",";
            }
        }

        catch (Exception ex)
        {
            ex.Message.ToString();
        }

        finally
        {
            conn.Close();
        }
    }

    //CUSTOMIZE UR CALENDAR RENDER EVENT NOW
    protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
    {
        // remove if (!IsPostBack) if running into Render errors
        if (!IsPostBack)
        {
            CalendarDay day = (CalendarDay)e.Day;
            TableCell cell = (TableCell)e.Cell;

            // if (!day.IsOtherMonth)
            if (!day.IsOtherMonth && palArr[day.Date.Month, day.Date.Day] != null)
            {
                //string palName = palArr[day.Date.Month, day.Date.Day];
                string[] palEvents = palArr[day.Date.Month, day.Date.Day].Split(',');
                string palName;

                // the rest of your code before the for loop
                char[] delim = { ',' };
                for (int i = 0; i < palEvents.Length; i++)
                {
                    palName = palEvents[i];
                    // the rest of your code as it already is

                    //for (int i = 0; i < palEvents.Length; i++)
                    {
                        palName = palEvents[i];
                        if (palName != null)
                        {
                            var p = palName.Split('|');
                            if (p[1].ToString() == "2")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 215);
                            }
                            else if (p[1].ToString() == "3")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 140);
                            }
                            else if (p[1].ToString() == "4")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 181, 138);
                            }
                            else if (p[1].ToString() == "5")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 204, 138);
                            }
                            else if (p[1].ToString() == "6")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 229, 138);
                            }
                            else if (p[1].ToString() == "7")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 254, 138);
                            }
                            else if (p[1].ToString() == "8")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 221, 255, 138);
                            }
                            else if (p[1].ToString() == "9")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 146, 249, 134);
                            }
                            else if (p[1].ToString() == "10")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 138, 238, 255);
                            }
                            else if (p[1].ToString() == "11")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 136, 199, 252);
                            }
                            else if (p[1].ToString() == "12")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 133, 146, 246);
                            }
                            else if (p[1].ToString() == "13")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 195, 131, 242);
                            }

                            Label lbl = new Label();
                            lbl.Text = "<br>" + p[0];
                            e.Cell.Controls.Add(lbl);
                        }
                    }
                }
            }
        }
    }

    protected void MyCal_MonthChange(object sender, MonthChangedEventArgs e)
    {
        getPalDates();
    }

    protected void MyCal_SelectionChanged(object sender, EventArgs e)
    {
        calendarExt.Show();

        if (!IsPostBack)
        {
            RetrieveCalendarNames();
        }
    }

    protected void RetrieveCalendarNames()
    {
        if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessChoice"].ConnectionString);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "WellnessChoice_TestTest";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                ddl_Name.DataSource = rdr;
                ddl_Name.DataValueField = "palv_id";
                ddl_Name.DataTextField = "palv_names";
                ddl_Name.DataBind();

                rdr.Close();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

    protected void btn_SavePhysicalActivityLog_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessChoice"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "WellnessChoice_InsertPhysicalActivityLog";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        cmd.Parameters.AddWithValue("@pi_id", SqlDbType.Int).Value = hf_AppID.Value;
        cmd.Parameters.AddWithValue("@palv_id", SqlDbType.Int).Value = ddl_Name.SelectedItem.Value;
        cmd.Parameters.AddWithValue("@pal_date", SqlDbType.DateTime).Value = Calendar1.SelectedDate.ToShortDateString();
        //cmd.Parameters.AddWithValue("@pal_id", SqlDbType.Int).Value = hf_pal_id.Value;

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }

        catch (Exception ex)
        {
            // ex.Message.ToString();
            lblSaveError.Text = ("Error on insert: " + ex.Message.ToString());
        }

        finally
        {
            Response.Redirect("PhysicalActivityLog.aspx");
            conn.Close();
        }
    }
}

Open in new window

libby9284

Try this change.  The logic that you need is the following and appears in the modified code snippet below:

- for each day, split all the events for that day into separate entries
- for each entry, split the entry to get the data you want and make a new label, adding it to the cal. cell

At this point its not really calendar class logic, its array and string manipulation.

//CUSTOMIZE UR CALENDAR RENDER EVENT NOW
    protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
    {
         char[] delim1 = { ',' };
         char[] delim2 = {'|'};
         string[] palEvents;
         string palName;
        if (!IsPostBack)
        {
            CalendarDay day = (CalendarDay)e.Day;
            TableCell cell = (TableCell)e.Cell;

            if (!day.IsOtherMonth && palArr[day.Date.Month, day.Date.Day] != null)
            {
                // split concatenated events into array of events
                palEvents = palArr[day.Date.Month, day.Date.Day].Split(delim1, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < palEvents.Length; i++)
                {
                        // enumerate through events for given date
                        palName = palEvents[i]; 

                        // if the current event isn't null AND current event is in the assumed format
                        if ((palName != null) && palName.Contains("|"))
                        {
                            // split event data into different parts
                            var p = palName.Split(delim2, StringSplitOptions.RemoveEmptyEntries);
                            if (p[1].ToString() == "2")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 215);
                            }
                            else if (p[1].ToString() == "3")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 140);
                            }
                            else if (p[1].ToString() == "4")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 181, 138);
                            }
                            else if (p[1].ToString() == "5")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 204, 138);
                            }
                            else if (p[1].ToString() == "6")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 229, 138);
                            }
                            else if (p[1].ToString() == "7")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 254, 138);
                            }
                            else if (p[1].ToString() == "8")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 221, 255, 138);
                            }
                            else if (p[1].ToString() == "9")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 146, 249, 134);
                            }
                            else if (p[1].ToString() == "10")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 138, 238, 255);
                            }
                            else if (p[1].ToString() == "11")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 136, 199, 252);
                            }
                            else if (p[1].ToString() == "12")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 133, 146, 246);
                            }
                            else if (p[1].ToString() == "13")
                            {
                                e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 195, 131, 242);
                            }

                            Label lbl = new Label();
                            lbl.Text = "<br>" + p[0];
                            e.Cell.Controls.Add(lbl);
                        }
                    }
                }
            }
        }
    }

Open in new window

Brian

ASKER
Ok, when I run the page with the following code below I run into the same error message.

LINE OF CODE WITH ERROR:
if (p[1].ToString() == "2")

ERROR MESSAGE:
Index was outside the bounds of the array.
//CUSTOMIZE UR CALENDAR RENDER EVENT NOW
    protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
    {
        char[] delim1 = { ',' };
        char[] delim2 = { '|' };
        string[] palEvents;
        string palName;

        // remove if (!IsPostBack) if running into Render errors
        if (!IsPostBack)
        {
            CalendarDay day = (CalendarDay)e.Day;
            TableCell cell = (TableCell)e.Cell;

            if (!day.IsOtherMonth && palArr[day.Date.Month, day.Date.Day] != null)
            {
                // split concatenated events into array of events
                palEvents = palArr[day.Date.Month, day.Date.Day].Split(delim1, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < palEvents.Length; i++)
                {
                    // enumerate through events for given date
                    palName = palEvents[i];

                    // if the current event isn't null AND current event is in the assumed format
                    if ((palName != null) && palName.Contains("|"))
                    {
                        // split event data into different parts
                        var p = palName.Split(delim2, StringSplitOptions.RemoveEmptyEntries);
                        if (p[1].ToString() == "2")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 215);
                        }
                        else if (p[1].ToString() == "3")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 140);
                        }
                        else if (p[1].ToString() == "4")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 181, 138);
                        }
                        else if (p[1].ToString() == "5")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 204, 138);
                        }
                        else if (p[1].ToString() == "6")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 229, 138);
                        }
                        else if (p[1].ToString() == "7")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 254, 138);
                        }
                        else if (p[1].ToString() == "8")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 221, 255, 138);
                        }
                        else if (p[1].ToString() == "9")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 146, 249, 134);
                        }
                        else if (p[1].ToString() == "10")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 138, 238, 255);
                        }
                        else if (p[1].ToString() == "11")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 136, 199, 252);
                        }
                        else if (p[1].ToString() == "12")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 133, 146, 246);
                        }
                        else if (p[1].ToString() == "13")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 195, 131, 242);
                        }

                        Label lbl = new Label();
                        lbl.Text = "<br>" + p[0];
                        e.Cell.Controls.Add(lbl);
                    }
                }
            }
        }
    }

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
libby9284

Ok, here's the problem.  something must be going wrong either while concatenating the data or while splitting it.  You're going to need to run your application in debug mode after placing breakpoints.

If I were you, I'd try placing breakpoints at MyCal_DayRender and getPalDates() and then stepping through the code to see:

1 - what happens when the concatenation takes place
2 - what happens when the split on ',' takes place
3 - what happens when the split on '|' takes place

Looking at all of those things will help determine why its not splitting palName as you expect it to.
Brian

ASKER
ok, so I setup breakpoints at the following lines below along with the values that where retrieved.

MyCal_DayRender Event:

char[] delim1 = { ',' };

Name        Value       Type              
delim1          null      char[]
delim2          null      char[]
palEvents   null      string[]
palName          null      string


char[] delim2 = { '|' };

Name        Value       Type
delim1       {char[1]}   char[]
   [0]          44','           char
delim2       null            char[]
palEvents   null      string[]
palName          null      string


When I added a breakpoint to the line below and tried stepping through the code it just kept skipping this line without displaying anything.

palEvents = palArr[day.Date.Month, day.Date.Day].Split(delim1, StringSplitOptions.RemoveEmptyEntries);


When I added a breakpoint to the line below and tried stepping through the code it just kept skipping this line without displaying anything.

if ((palName != null) && palName.Contains("|"))
libby9284

Ok.

My guess is that commas are not a valid delimiter, they must be used somewhere in the data?  You should try a different character (maybe a semicolon; you'll have to change the concatenation line and delim1)

One problem I think is that you're not using the debugger to its fullest potential.  Since the breakpoints I suggested didn't seem to reveal much, I'd suggest you place a breakpoint at the top of the function (right next to the open bracket) and then step through the whole function.  This way, you can trace your code and see exactly what is going on.  There's two ways to see the data during runtime - using a watch or using the quickwatch utility.  Right clicking on a variable while debugging gives you both options, quickwatch is my personal preference.  

Additionally, you're going to have to step through the function multiple times because I don't think you have an event for every day of the month.

If its just skipping the if statement block completely  (after if (!day.IsOtherMonth...)) then there's no data for that day (this means that when you're debugging it would skip the rest of the function entirely).  

Try what I suggested and look at the contents of palArr[day.Date.Month, day.Date.Day], palEvents (look @ several indices) and palName.  Data from these fields should help indicate what's wrong, along with keeping an eye on what day is being rendered too to check for consistency.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Brian

ASKER
Hi libby9284,

Ok, I think I may have found what you are looking for even though to me it's another language from another planet :(

I'm attaching a .txt file to keep the formatting because I'm worried about the formatting breaking if I add it here.

>> One problem I think is that you're not using the debugger to its fullest potential
Correct, I have not used the debugger much and need to learn more about it and its featrues.

Also, I'm gonig to display my table structure below again with actuall data that I have in there now.

pal_id             pl_id          palv_id          pal_date
4027              1293          2                   8/12/2011
4028              1293          4                   8/12/2011
4029              1293          6                   8/12/2011
4030              1293         11                  8/13/2011
4031              1293          9                   8/13/2011
EE-Debug.txt
Brian

ASKER
Hi libby9284,

I was just playing around with different characters for the following line below and interestingly enough if I loaded the page for the first time using char[] delim1 = { ',' }; then it displays one entry which is normal now, but if I change the character below then the entry changes to a newer entry but keeping the same BG color for the Cell.

Not sure if this helps but I'm trying different things and so far I found that. Hopefully that may help in figuring out this issue.

char[] delim1 = { '/' };
libby9284


I just tried your code out in my workspace (w/ the data you sent me) and can't find a problem with that function.  Your event data is formatted like this, from what you sent me:

B/S|6
Z|9

The data was split correctly based on the '|' character and on the ',' character.

What I had suggested before would have yielded data like this (let's pretend that the data on 8/12 and the data on 8/13 are all for 8/12)
the part of your code going to the DB for data would have ended up storing this:
palArr[8,12] = "B/S|6,Z|9";

Then, after the split based on comma palEvents will have 2 items,
palEvents[0] = "B/S|6"
palEvents[1] = "Z|9"

Then, your original code will split each of those based on '|', creating array p twice
first version of array p
p[0] = "B/S"
p[1] = 6

second version of array p
p[0] = "Z"
p[1] = "9"

Am I misunderstanding the scheme for your data?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Brian

ASKER
Hi libby9284

I'm not sure what all that means, I just took a look at the way the Date is stored in the DB and i'm not sure if this matters or not but it's stored differently then what i mentioned above.

As you will see I need to display 3 different labels for 8/12 and 2 different labels for 8/13. please forgive me for the pal_date format. I also apologize that i do not understand what you are explaining with B/S|6 and Z|9 :(

pal_id      pi_id      palv_id      pal_date
4027      1294      2      2011-08-12 00:00:00.000
4028      1294      4      2011-08-12 00:00:00.000
4029      1294      6      2011-08-12 00:00:00.000
4030      1294      11      2011-08-13 00:00:00.000
4031      1294      9      2011-08-13 00:00:00.000
Brian

ASKER
Also, when I run the code I DO NOT recieve any errors but I'm only able to see one entry for each of those dates (8/12 and 8/13) instead of seeing all entries for each of those two dates.
libby9284

i'm not sure what i'm saying that is confusing, please let me know what you're not understanding and i'll be happy to explain further.

the letters are coming from this part of your code, in getPalDates():
palArr[dm.Month, dm.Day] += dtDates.Rows[iCnt]["palv_code"].ToString() + "|" + dtDates.Rows[iCnt]["palv_id"].ToString() + ",";

They're coming from the DB under column palv_code.  So, for 8/12, there's a DB entry that looks like this
pal_id        pi_id        palv_code        palv_id        pal_date
4029         1294        B/S                   6                2011-08-12 00:00:00.000

when the following runs
palArr[dm.Month, dm.Day] += dtDates.Rows[iCnt]["palv_code"].ToString() + "|" + dtDates.Rows[iCnt]["palv_id"].ToString() + ",";

Open in new window

it should take the palv_code (B/S), the '|', the palv_id (6), and a ',' and turn them into one string like this:
"B/S|6,"

That being said, it looks like the issue may actually be with your stored procedure.  You should double check the procedure and see what exactly it is retrieving (perhaps you should run it manually in a database client to verify).  
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Brian

ASKER
Hi libby9284,

sorry, I understand now what you mean by the letters/characters such as B/S|6. Please see my stored procedure below. When I execute it on the Server I get the following results back, so it seems that my SP is fine, at least from what i can tell.

Also, is there a reason why you are trying to return B/S|6 instead of just B/S?

So for example this is what I need it to look like inside the CELL assuming the date is 8-12-2011.

W
B/K
B/S
Results from SP execution:

pi_id	palv_id	pal_date	          palv_names	palv_code
1294	2	2011-08-12	Walking	          W
1294	4	2011-08-12	Boxing/Karate	B/K
1294	6	2011-08-12	Biking/Spinning	B/S
1294	11	2011-08-13	Aerobics/Pilates	A/P
1294	9	2011-08-13 	Zumba	          Z

Open in new window

STORED PROCEDURE:

ALTER PROCEDURE [dbo].[WellnessChoice_RetrieveDatesForCalendar]

(
@pi_id int
)

AS

SELECT pal.pi_id, pal.palv_id, pal.pal_date, palv.palv_names, palv.palv_code
FROM WellnessChoice_PhysicalActivityLog AS pal
INNER JOIN WellnessChoice_PhysicalActivityLogValues AS palv
ON pal.palv_id = palv.palv_id
WHERE pi_id = @pi_id

Open in new window

libby9284

There's only two other things I can think of.

First idea (maybe data is getting lost b/w calls since websites are stateless):
- in your function getPalDates, in the finally block, save palArr to the Session
- in MyCal_DayRender, set palArr from the session

// example for retrieving multildimensional array
myMultiDimArr = (string[,])Session["myMultiDimArrKey"]

Open in new window


Second idea:
something is going wrong while iterating through the data in getPalDates; set a breakpoint at the line starting with
palArr[dm.Month, dm.Day] += 

Open in new window

and debug, seeing what is stored
Brian

ASKER
I was not sure how to save palArr to the Session and or set palArr from the Session.

I did set a breakpoint and I'm attaching the data that was collected based on the breakpoint you mentioned above.


EECode.txt
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Brian

ASKER
Note: You may notice that i removed the character "/" from B/K, B/S, and A/P just in case you where wondering. I removed it in the DB to see if maybe that was causing it but I get the same results.
libby9284

For trying the first idea:

its the same as how you're already using the session in the code.  Session is a key based collection so, make a new key.  For example, if I had a string array called myArray and I wanted to put it in the session object (saving the data in its current state) i could do this after I've finished putting my data into the array:
Session["myArray_saved"] = myArray

Open in new window


Then later on in another function, I can retrieve my array like this in order to use it again.
string[] myArray = (string[])Session["myArray_saved"]

Open in new window


you're storing the right data in the array (according to the txt file you posted) but the data isn't going to just stay there across callbacks to the server, its a quirk of web development.  If you're going to need something later in another function, save it to Session or Viewstate (Session is more efficient, ViewState is more secure but can slow things down greatly).
Brian

ASKER
Yeah, it's frustrating because like you said the .txt file is retrieving the correct data.

Which Event and where in the Event do I need to add the following code below to?

Session["myArray_saved"] = myArray

string[] myArray = (string[])Session["myArray_saved"]?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
libby9284

I wasn't giving you the explicit code in the last post, I was giving you an example to extrapolate from.  

Before getPalDates() exits, you need to save your array.

Before using palArr in MyCal_DayRender, you need to retrieve your array.

You can find more examples of using Session to save your data between callbacks here, the link explains its purpose and how to use it
Brian

ASKER
Ok, I'm just very confused at this point. The last thing I want to do is create another post in regards to storing getPalDates to Session but I will so that we can hopefully find the calprit to this mystery. I have stored data to Sessions before but not to this extent.
libby9284

its no different, its the same idea.   the only difference is the cast when you retrieve it, the cast has to be the same type as palArr (a two dimensional array of type string).
Your help has saved me hundreds of hours of internet surfing.
fblack61
Brian

ASKER
Hi libby9284,

I'm going to create another post with what you are asking since I'm not sure how to implement the array into session. Once I hear back from them I will notify you. Hopefully by doing this is resolves the problem. also, you said that you tested this out on your end it was fine, what did you do differently? Also, i don't need to have the palv_code next to the palv_id. I only need to display "W", "B/S", "Z" etc...

so if user has multiple entries for one date then it only needs to show the palv_code values.
Brian

ASKER
Hi libby9284,

Ok, I was able to get some help storing what you asked in Session. Please see the code below. Also, once I added the code and ran the page I did NOT notice anything differently than before. I'm still not understanding how you got your example to work on your end.
Brian

ASKER
Sorry, fogot to upload code as you requested for storing the Array in Session.
//GET AVAILABLE DATE AND ITS CORRESPONDING NAME IN AN ARRAY
    protected void getPalDates()
    {
        int pi_id = Convert.ToInt32(Session["pi_id"]);

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessChoice"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "WellnessChoice_RetrieveDatesForCalendar";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        cmd.Parameters.AddWithValue("@pi_id", SqlDbType.Int).Value = pi_id;

        DataTable dtDates = new DataTable();

        SqlDataAdapter adp = new SqlDataAdapter();

        try
        {
            conn.Open();

            adp.SelectCommand = cmd;
            adp.Fill(dtDates);
            palArr = new string[13, 32];

            for (int iCnt = 0; iCnt <= dtDates.Rows.Count - 1; iCnt++)
            {
                Calendar1.SelectedDates.Add(Convert.ToDateTime(dtDates.Rows[0]["pal_date"]));
                DateTime dm = Convert.ToDateTime(dtDates.Rows[iCnt]["pal_date"]);
                palArr[dm.Month, dm.Day] = dtDates.Rows[iCnt]["palv_code"].ToString() + "|" + dtDates.Rows[iCnt]["palv_id"].ToString();
            }
            
            Session["myArray_saved"] = palArr;
        }

        catch (Exception ex)
        {
            ex.Message.ToString();
        }

        finally
        {
            conn.Close();
        }
    }

    //CUSTOMIZE UR CALENDAR RENDER EVENT NOW
    protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
    {
        char[] delim1 = { ',' };
        char[] delim2 = { '|' };
        string[] palEvents;
        string palName;
        
        palArr = (string[,])Session["myArray_saved"];

        // remove if (!IsPostBack) if running into Render errors
        if (!IsPostBack)
        {
            CalendarDay day = (CalendarDay)e.Day;
            TableCell cell = (TableCell)e.Cell;

            if (!day.IsOtherMonth && palArr[day.Date.Month, day.Date.Day] != null)
            {
                // split concatenated events into array of events
                palEvents = palArr[day.Date.Month, day.Date.Day].Split(delim1, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < palEvents.Length; i++)
                {
                    // enumerate through events for given date
                    palName = palEvents[i];

                    // if the current event isn't null AND current event is in the assumed format
                    if ((palName != null) && palName.Contains("|"))
                    {
                        // split event data into different parts
                        var p = palName.Split(delim2, StringSplitOptions.RemoveEmptyEntries);
                        if (p[1].ToString() == "2")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 215);
                        }
                        else if (p[1].ToString() == "3")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 138, 140);
                        }
                        else if (p[1].ToString() == "4")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 181, 138);
                        }
                        else if (p[1].ToString() == "5")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 204, 138);
                        }
                        else if (p[1].ToString() == "6")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 229, 138);
                        }
                        else if (p[1].ToString() == "7")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 255, 254, 138);
                        }
                        else if (p[1].ToString() == "8")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 221, 255, 138);
                        }
                        else if (p[1].ToString() == "9")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 146, 249, 134);
                        }
                        else if (p[1].ToString() == "10")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 138, 238, 255);
                        }
                        else if (p[1].ToString() == "11")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 136, 199, 252);
                        }
                        else if (p[1].ToString() == "12")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 133, 146, 246);
                        }
                        else if (p[1].ToString() == "13")
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(120, 195, 131, 242);
                        }

                        Label lbl = new Label();
                        lbl.Text = "<br>" + p[0];
                        e.Cell.Controls.Add(lbl);
                    }
                }
            }
        }
    }

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
libby9284

Change line 31 above to use The += operator instead of = ( I.e.  palArr[dm.Month, dm.Day] += ...)

  In your other posts you had +=, which would have concatenated entries.  

What you have in this post will overwrite data.  Each time you get an event for a given date it will overwrite the data stored in that index of the array.  Changing  the operator should fix it.
Brian

ASKER
ok, made the change below as you mentioned and this time it displayed a different single value to a date. Still does not dislpay all entries for a particular date :(

palArr[dm.Month, dm.Day] += dtDates.Rows[iCnt]["palv_code"].ToString() + "|" + dtDates.Rows[iCnt]["palv_id"].ToString();
Brian

ASKER
Hi libby9284,

This will give you an idea as to what I would like the multiple entries to look like when all differrent values to be dislayed for a given date if more than one entry per date exist.

If you read Part 2 and run the demo at the bottom then you will see what I mean.

http://www.4guysfromrolla.com/articles/041603-1.aspx
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
libby9284

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Brian

ASKER
Hi libby9284,

Ok, I'm glad to let you know that your last post worked :) Thank you so VERY MUCH!!!! libby9284 for your time, patience, understanding, knowledge, and detication to help others. I wish that one day I can be as good as you!!!!

There are a couple minor issues that I found but I'm sure to you they are prob nothing. The problem that i noticed is that if I where to select another month by clicking on the "Next" and "Previous" links then the data does not get displayed for the other months. If i'm at the other month and trying going back to view what was first loaded then nothing displays either.

It only displays the values on Page_Load and after that they disappear.
libby9284

I'm glad I could help :) .  For the current issue, you should look at your dayrender function.  You're using a check of !IsPostback before the bulk of the function's code - this means that that code will only run during the initial pageload.  

Frequently its useful to only do certain things once when the page first loads (like, for efficiency, for changing the way the page displays for different user types, etc).  But, for the calendar dayrender event, doing the bulk of the dayrender code only during the initial page load is kind of shooting yourself in the foot.  Can you see why?
Brian

ASKER
Yes, that makes sense and yes that worked very well now :) Thanks!!!

Now for the million dollar question :)

Now that you created me a miracle is there anyway to do exactly what we did but with 12 Calendar Controls instead of just one? So instead of showing just one control per page we can show 12 Calendar Controls one calendar for every month starting with this month?

If so, then I would like to work with you on it if you would be willing and I can create another post for that as well.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
libby9284

Yes, there should be, but I haven't done it before and the way I'm thinking it could be done may have a few latency issues.  Could you give me a better idea of what your background is so I have a better idea of how to help you?
Brian

ASKER
What type of latency issues?

Do you mean the background as to what I would like to do, or my background?
Brian

ASKER
I would like to display 12 calendars like the tutorial below but keep everything we did the same but to expand to the other controls.

http://weblogs.sqlteam.com/jhermiz/archive/2007/12/10/Cool-Tricks-With-The-ASP.net-Calendar.aspx
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
libby9284

possible source of latency with 12 calendar controls:
day render will have to run about 12 times as often as it currently does (1 set of about 31 days for each of 12 calendars, totaling about 372 round trip calls to the server each time the calendars on the page render)

But, looking at the link you sent me, it looks like its been done before without complaint.  Which means this should be fairly straightforward.  It should simply be a matter of wiring the additional calendars' dayrender event to the other calendar controls as well and modifying your getPalDates function to accept the month as a parameter.  Otherwise, you should be able to extrapolate from the tutorial you already found.

and by background, i was trying to understand what you're background experience is so I could help you more efficiently
Brian

ASKER
Ok, if I would create another post would you be willing to help me?

My background is 1yr. using C#. 4yrs. with ASP.NET using mostly (CRUD) create, retrieve, update, and delete operations using ADO.NET.
libby9284

Ok.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Brian

ASKER
Hi libby9284,

Thanks, please let me know once you recieve this so i can close this post. Once again thank you for ALL YOUR HELP...


https://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27255568.html
libby9284

ok got it
Brian

ASKER
Thank you again VERY much libby for all your time, patience, and knowledge.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy