Solved

calendar object how to not display dates that dont exist

Posted on 2008-10-27
9
224 Views
Last Modified: 2012-06-27
Hi,

I have a calendar object on my aspx page and I also have a drop down list on the same page. Whichever date is selected on the dropdown list then becomes highlighted on the calendar.
My problem is that the dropdown list has dates that arent valid, like the 31st of november can be selected which doesnt exist, but my calendar decides that it will hightlight the 8th of the month instead of not highlighting any day at all which is what i'd like. I dont want to write code to change the numbers shown in the drop down list for the dates according to which month is selected. I am trying to get the date highlighted code in the c# public void SelectMonth to filter out any unvalid dates to then tell the calendar not to display a date if the date is unvalid.

Please help!! Thanks
using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.Globalization;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;
 

public partial class calendar_fixtcalendar : System.Web.UI.Page

{

    public static SqlConnection conn = null;
 

    public static void OpenConnection()

    {

        CloseConnection();

        if (conn == null)

        {

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

        }
 

        conn.Open();

    }
 

    public static void CloseConnection()

    {

        if (conn != null)

            conn.Close();

    }
 

    protected void Page_Load(object sender, EventArgs e)

    {

        hidden.Visible = false;
 

        if (!IsPostBack)

        {

            OpenConnection();
 

            SqlCommand command = new SqlCommand("exec usp_GetValidMonths", conn);
 

            SqlDataReader reader = command.ExecuteReader();
 

            ArrayList yearList = new ArrayList();
 

            while (reader.Read())

            {

                string[] monthYear = SplitMonthYear(reader["month"].ToString());
 

                if (!yearList.Contains(monthYear[1]))

                    yearList.Add(monthYear[1]);

            }
 

            yearList.Sort();
 

            for (int i = 0; i < yearList.Count; i++)

            {

                ListItem item1 = new ListItem();

                item1.Text = (string)"20" + yearList[i];

                item1.Value = (string)yearList[i];

                year.Items.Add(item1);

            }
 
 

            CloseConnection();

        }
 

        if (!IsPostBack)

        {

            DateTimeFormatInfo dfi = new DateTimeFormatInfo();

            dfi.MonthDayPattern = "dd MMMM yy";
 

            DateTime now = DateTime.Now;
 

            string dateNow = now.ToString("m", dfi);
 

            string strDay = dateNow.Split(' ')[0];

            string strMonth = dateNow.Split(' ')[1];

            string strYear = dateNow.Split(' ')[2];
 

            string dateID = strMonth + " " + strYear;

            string todayDate = strDay;
 

            DisplayMonth(dateID , todayDate);
 

            dateDrop.Text = strDay;

            monthdrop.Text = strMonth;

            year.Text = strYear;
 

            DateTime datenowforcalendar = DateTime.Parse(dateNow);
 

            Calendar1.SelectedDate = datenowforcalendar;

            Calendar1.VisibleDate = datenowforcalendar;
 

        }

    }

 

    public void PrintMonth(object sender, EventArgs e)

    {

        Response.Write("print");
 

    }

    

    public void SelectMonth(object sender, EventArgs e)

    {

        hidden.Visible = false;

        string monthID = monthdrop.SelectedValue + " " + year.SelectedValue;

        object result = null;

        string dateSelect = dateDrop.SelectedValue;

        string datehighlight =  dateDrop.SelectedValue + monthdrop.SelectedValue + year.SelectedValue;
 

        DateTime dateforcalendar = DateTime.Parse(datehighlight);
 

       if (dateforcalendar.IsValid)

        {

            Calendar1.SelectedDate = dateforcalendar;

            Calendar1.VisibleDate = dateforcalendar;

      }
 

        else

        {

            Calendar1.SelectedDate = null;

            Calendar1.VisibleDate = null;
 

        }
 

       
 

        OpenConnection();
 

        SqlCommand commandviewfixts = new SqlCommand("exec usp_GetMonthFixtures @monthID, @dateSelect", conn);
 

        commandviewfixts.Parameters.Add("monthID", SqlDbType.NVarChar);

        commandviewfixts.Parameters["monthID"].Value = monthID;

        commandviewfixts.Parameters.Add("dateSelect", SqlDbType.NVarChar);

        commandviewfixts.Parameters["dateSelect"].Value = dateSelect;
 

        

        result = commandviewfixts.ExecuteScalar();

        if (result == null)

        {

            hidden.Visible = true;
 

        }
 

        CloseConnection();
 

        OpenConnection();
 
 

        SqlCommand mySqlSelect = new SqlCommand("exec usp_GetMonthAllFixtures @MonthID , @dateSelect", conn);
 

        mySqlSelect.Parameters.Add("MonthID", SqlDbType.NVarChar);

        mySqlSelect.Parameters["MonthID"].Value = monthID;

        mySqlSelect.Parameters.Add("dateSelect", SqlDbType.NVarChar);

        mySqlSelect.Parameters["dateSelect"].Value = dateSelect;
 
 

        mySqlSelect.CommandType = CommandType.Text;
 

        SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect); DataSet DayDataSet = new DataSet();
 

        mySqlAdapter.Fill(DayDataSet);
 

        RepeaterDay.DataSource = DayDataSet;
 

        RepeaterDay.DataBind();
 

        int k = 0;
 

        foreach (RepeaterItem repeaterItem in RepeaterDay.Items)

        {

            OpenConnection();
 

            string date = DayDataSet.Tables[0].Rows[k]["Date"].ToString();
 
 

            {

            SqlCommand mySqlSelectAM = new SqlCommand("exec usp_GetMonthAMFixtures @dateSelect, @MonthID", conn);
 

                mySqlSelectAM.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                mySqlSelectAM.Parameters["dateSelect"].Value = dateSelect;

                mySqlSelectAM.Parameters.Add("MonthID", SqlDbType.NVarChar);

                mySqlSelectAM.Parameters["MonthID"].Value = monthID;
 

                mySqlSelectAM.CommandType = CommandType.Text;
 

                SqlDataAdapter mySqlAdapterAM = new SqlDataAdapter(mySqlSelectAM);
 

                DataSet DayDataSetAM = new DataSet();
 

                mySqlAdapterAM.Fill(DayDataSetAM);
 

                if (DayDataSetAM.Tables[0].Rows.Count <= 0)

                {

                    DayDataSetAM.Tables[0].Rows.Add(new object[] { "No events", "" });

                }
 

                ((Repeater)(repeaterItem.FindControl("RepeaterAM"))).DataSource = DayDataSetAM;
 

                ((Repeater)(repeaterItem.FindControl("RepeaterAM"))).DataBind();

            }

            {

                SqlCommand mySqlSelectPM = new SqlCommand("exec usp_GetMonthPMFixtures @dateSelect, @MonthID", conn);
 

                mySqlSelectPM.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                mySqlSelectPM.Parameters["dateSelect"].Value = dateSelect;

                mySqlSelectPM.Parameters.Add("MonthID", SqlDbType.NVarChar);

                mySqlSelectPM.Parameters["MonthID"].Value = monthID;
 

                mySqlSelectPM.CommandType = CommandType.Text;
 

                SqlDataAdapter mySqlAdapterPM = new SqlDataAdapter(mySqlSelectPM);
 

                DataSet DayDataSetPM = new DataSet();
 

                mySqlAdapterPM.Fill(DayDataSetPM);
 

                if (DayDataSetPM.Tables[0].Rows.Count <= 0)

                {

                    DayDataSetPM.Tables[0].Rows.Add(new object[] { "No events", "" });

                }
 

                ((Repeater)(repeaterItem.FindControl("RepeaterPM"))).DataSource = DayDataSetPM;
 

                ((Repeater)(repeaterItem.FindControl("RepeaterPM"))).DataBind();
 

            }

            {

                SqlCommand mySqlSelectCL = new SqlCommand("exec usp_GetMonthClubFixtures @dateSelect, @MonthID", conn);
 

                mySqlSelectCL.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                mySqlSelectCL.Parameters["dateSelect"].Value = dateSelect;

                mySqlSelectCL.Parameters.Add("MonthID", SqlDbType.NVarChar);

                mySqlSelectCL.Parameters["MonthID"].Value = @monthID;
 

                mySqlSelectCL.CommandType = CommandType.Text;
 

                SqlDataAdapter mySqlAdapterCL = new SqlDataAdapter(mySqlSelectCL);
 

                DataSet DayDataSetCL = new DataSet();
 

                mySqlAdapterCL.Fill(DayDataSetCL);
 

                if (DayDataSetCL.Tables[0].Rows.Count <= 0)

                {

                    DayDataSetCL.Tables[0].Rows.Add(new object[] { "No events", "" });

                }
 

                ((Repeater)(repeaterItem.FindControl("RepeaterCL"))).DataSource = DayDataSetCL;
 

                ((Repeater)(repeaterItem.FindControl("RepeaterCL"))).DataBind();

            }
 

            k++;
 
 
 

        }

        CloseConnection();

    }

       

    

    public void DisplayMonth(string dateID , string todayDate)

    {

        hidden.Visible = false;

        object result = null;
 

        OpenConnection();
 

        SqlCommand commandviewfixts = new SqlCommand("exec usp_GetMonthFixtures @monthID, @dateSelect", conn);
 

        commandviewfixts.Parameters.Add("monthID", SqlDbType.NVarChar);

        commandviewfixts.Parameters["monthID"].Value = dateID;

        commandviewfixts.Parameters.Add("dateSelect", SqlDbType.NVarChar);

        commandviewfixts.Parameters["dateSelect"].Value = todayDate;
 
 

        result = commandviewfixts.ExecuteScalar();

        if (result == null)

        {

            hidden.Visible = true;
 

        }
 

        CloseConnection();
 

        OpenConnection();
 
 

        SqlCommand mySqlSelect = new SqlCommand("exec usp_GetMonthAllFixtures @MonthID , @dateSelect", conn);
 

        mySqlSelect.Parameters.Add("MonthID", SqlDbType.NVarChar);

        mySqlSelect.Parameters["MonthID"].Value = dateID;

        mySqlSelect.Parameters.Add("dateSelect", SqlDbType.NVarChar);

        mySqlSelect.Parameters["dateSelect"].Value = todayDate;
 
 

        mySqlSelect.CommandType = CommandType.Text;
 

        SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect); DataSet DayDataSet = new DataSet();
 

        mySqlAdapter.Fill(DayDataSet);
 

        RepeaterDay.DataSource = DayDataSet;
 

        RepeaterDay.DataBind();
 

        int k = 0;
 

        foreach (RepeaterItem repeaterItem in RepeaterDay.Items)

        {

            OpenConnection();
 

            string date = DayDataSet.Tables[0].Rows[k]["Date"].ToString();
 
 

            {

                SqlCommand mySqlSelectAM = new SqlCommand("exec usp_GetMonthAMFixtures @dateSelect, @MonthID", conn);
 

                mySqlSelectAM.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                mySqlSelectAM.Parameters["dateSelect"].Value = dateID;

                mySqlSelectAM.Parameters.Add("MonthID", SqlDbType.NVarChar);

                mySqlSelectAM.Parameters["MonthID"].Value = todayDate;
 

                mySqlSelectAM.CommandType = CommandType.Text;
 

                SqlDataAdapter mySqlAdapterAM = new SqlDataAdapter(mySqlSelectAM);
 

                DataSet DayDataSetAM = new DataSet();
 

                mySqlAdapterAM.Fill(DayDataSetAM);
 

                if (DayDataSetAM.Tables[0].Rows.Count <= 0)

                {

                    DayDataSetAM.Tables[0].Rows.Add(new object[] { "No events", "" });

                }
 

                ((Repeater)(repeaterItem.FindControl("RepeaterAM"))).DataSource = DayDataSetAM;
 

                ((Repeater)(repeaterItem.FindControl("RepeaterAM"))).DataBind();

            }

            {

                SqlCommand mySqlSelectPM = new SqlCommand("exec usp_GetMonthPMFixtures @dateSelect, @MonthID", conn);
 

                mySqlSelectPM.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                mySqlSelectPM.Parameters["dateSelect"].Value = dateID;

                mySqlSelectPM.Parameters.Add("MonthID", SqlDbType.NVarChar);

                mySqlSelectPM.Parameters["MonthID"].Value = todayDate;
 

                mySqlSelectPM.CommandType = CommandType.Text;
 

                SqlDataAdapter mySqlAdapterPM = new SqlDataAdapter(mySqlSelectPM);
 

                DataSet DayDataSetPM = new DataSet();
 

                mySqlAdapterPM.Fill(DayDataSetPM);
 

                if (DayDataSetPM.Tables[0].Rows.Count <= 0)

                {

                    DayDataSetPM.Tables[0].Rows.Add(new object[] { "No events", "" });

                }
 

                ((Repeater)(repeaterItem.FindControl("RepeaterPM"))).DataSource = DayDataSetPM;
 

                ((Repeater)(repeaterItem.FindControl("RepeaterPM"))).DataBind();
 

            }

            {

                SqlCommand mySqlSelectCL = new SqlCommand("exec usp_GetMonthClubFixtures @dateSelect, @MonthID", conn);
 

                mySqlSelectCL.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                mySqlSelectCL.Parameters["dateSelect"].Value = dateID;

                mySqlSelectCL.Parameters.Add("MonthID", SqlDbType.NVarChar);

                mySqlSelectCL.Parameters["MonthID"].Value = todayDate;
 

                mySqlSelectCL.CommandType = CommandType.Text;
 

                SqlDataAdapter mySqlAdapterCL = new SqlDataAdapter(mySqlSelectCL);
 

                DataSet DayDataSetCL = new DataSet();
 

                mySqlAdapterCL.Fill(DayDataSetCL);
 

                if (DayDataSetCL.Tables[0].Rows.Count <= 0)

                {

                    DayDataSetCL.Tables[0].Rows.Add(new object[] { "No events", "" });

                }
 

                ((Repeater)(repeaterItem.FindControl("RepeaterCL"))).DataSource = DayDataSetCL;
 

                ((Repeater)(repeaterItem.FindControl("RepeaterCL"))).DataBind();

            }
 

            k++;
 
 
 

        }

        CloseConnection();

    }
 

    private string[] SplitMonthYear(string StrDate)

    {

        string[] split = StrDate.Split(' ');
 

        return split;

    }
 
 

    protected void Date_Selected(object sender, EventArgs e)

    {

        DateTimeFormatInfo dfi = new DateTimeFormatInfo();

        dfi.MonthDayPattern = "MMMM yy";

        DateTimeFormatInfo dtf = new DateTimeFormatInfo();

        dtf.MonthDayPattern = "dd";

       
 

        DateTime CalendarSelectedYear = Calendar1.SelectedDate;

        DateTime CalendarSelectedDate = Calendar1.SelectedDate;
 

        string convertedSelectedYear = CalendarSelectedYear.ToString("m", dfi);

        string convertedSelectedDate = CalendarSelectedDate.ToString("m", dtf);
 

        string splitdatemonth = convertedSelectedYear.Split(' ')[0];

        string splitdateyear = convertedSelectedYear.Split(' ')[1];
 

        dateDrop.Text = convertedSelectedDate;

        monthdrop.Text = splitdatemonth;

        year.Text = splitdateyear;
 

        hidden.Visible = false;

        object result = null;
 

        OpenConnection();
 

        SqlCommand commandviewfixts = new SqlCommand("exec usp_GetMonthFixtures @monthID, @dateSelect", conn);
 

        commandviewfixts.Parameters.Add("monthID", SqlDbType.NVarChar);

        commandviewfixts.Parameters["monthID"].Value = convertedSelectedYear;

        commandviewfixts.Parameters.Add("dateSelect", SqlDbType.NVarChar);

        commandviewfixts.Parameters["dateSelect"].Value = convertedSelectedDate;
 
 

        result = commandviewfixts.ExecuteScalar();

        if (result == null)

        {

            hidden.Visible = true;
 

        }
 

        CloseConnection();
 

        OpenConnection();
 
 

        SqlCommand mySqlSelect = new SqlCommand("exec usp_GetMonthAllFixtures @MonthID , @dateSelect", conn);
 

        mySqlSelect.Parameters.Add("MonthID", SqlDbType.NVarChar);

        mySqlSelect.Parameters["MonthID"].Value = convertedSelectedYear;

        mySqlSelect.Parameters.Add("dateSelect", SqlDbType.NVarChar);

        mySqlSelect.Parameters["dateSelect"].Value = convertedSelectedDate;
 
 

        mySqlSelect.CommandType = CommandType.Text;
 

        SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect); DataSet DayDataSet = new DataSet();
 

        mySqlAdapter.Fill(DayDataSet);
 

        RepeaterDay.DataSource = DayDataSet;
 

        RepeaterDay.DataBind();
 

        int k = 0;
 

        foreach (RepeaterItem repeaterItem in RepeaterDay.Items)

        {

            OpenConnection();
 

            string date = DayDataSet.Tables[0].Rows[k]["Date"].ToString();
 
 

            {

                SqlCommand mySqlSelectAM = new SqlCommand("exec usp_GetMonthAMFixtures @dateSelect, @MonthID", conn);
 

                mySqlSelectAM.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                mySqlSelectAM.Parameters["dateSelect"].Value = convertedSelectedDate;

                mySqlSelectAM.Parameters.Add("MonthID", SqlDbType.NVarChar);

                mySqlSelectAM.Parameters["MonthID"].Value = convertedSelectedYear;
 

                mySqlSelectAM.CommandType = CommandType.Text;
 

                SqlDataAdapter mySqlAdapterAM = new SqlDataAdapter(mySqlSelectAM);
 

                DataSet DayDataSetAM = new DataSet();
 

                mySqlAdapterAM.Fill(DayDataSetAM);
 

                if (DayDataSetAM.Tables[0].Rows.Count <= 0)

                {

                    DayDataSetAM.Tables[0].Rows.Add(new object[] { "No events", "" });

                }
 

                ((Repeater)(repeaterItem.FindControl("RepeaterAM"))).DataSource = DayDataSetAM;
 

                ((Repeater)(repeaterItem.FindControl("RepeaterAM"))).DataBind();

            }

            {

                SqlCommand mySqlSelectPM = new SqlCommand("exec usp_GetMonthPMFixtures @dateSelect, @MonthID", conn);
 

                mySqlSelectPM.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                mySqlSelectPM.Parameters["dateSelect"].Value = convertedSelectedDate;

                mySqlSelectPM.Parameters.Add("MonthID", SqlDbType.NVarChar);

                mySqlSelectPM.Parameters["MonthID"].Value = convertedSelectedYear;
 

                mySqlSelectPM.CommandType = CommandType.Text;
 

                SqlDataAdapter mySqlAdapterPM = new SqlDataAdapter(mySqlSelectPM);
 

                DataSet DayDataSetPM = new DataSet();
 

                mySqlAdapterPM.Fill(DayDataSetPM);
 

                if (DayDataSetPM.Tables[0].Rows.Count <= 0)

                {

                    DayDataSetPM.Tables[0].Rows.Add(new object[] { "No events", "" });

                }
 

                ((Repeater)(repeaterItem.FindControl("RepeaterPM"))).DataSource = DayDataSetPM;
 

                ((Repeater)(repeaterItem.FindControl("RepeaterPM"))).DataBind();
 

            }

            {

                SqlCommand mySqlSelectCL = new SqlCommand("exec usp_GetMonthClubFixtures @dateSelect, @MonthID", conn);
 

                mySqlSelectCL.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                mySqlSelectCL.Parameters["dateSelect"].Value = convertedSelectedDate;

                mySqlSelectCL.Parameters.Add("MonthID", SqlDbType.NVarChar);

                mySqlSelectCL.Parameters["MonthID"].Value = convertedSelectedYear;
 

                mySqlSelectCL.CommandType = CommandType.Text;
 

                SqlDataAdapter mySqlAdapterCL = new SqlDataAdapter(mySqlSelectCL);
 

                DataSet DayDataSetCL = new DataSet();
 

                mySqlAdapterCL.Fill(DayDataSetCL);
 

                if (DayDataSetCL.Tables[0].Rows.Count <= 0)

                {

                    DayDataSetCL.Tables[0].Rows.Add(new object[] { "No events", "" });

                }
 

                ((Repeater)(repeaterItem.FindControl("RepeaterCL"))).DataSource = DayDataSetCL;
 

                ((Repeater)(repeaterItem.FindControl("RepeaterCL"))).DataBind();

            }
 

            k++;
 
 
 

        }

        CloseConnection();
 
 
 

    }
 

}

Open in new window

0
Comment
Question by:CharlieDev
  • 4
  • 3
  • 2
9 Comments
 
LVL 11

Expert Comment

by:jgordos
ID: 22812798
I think, fundamentally, your approach is busted.  I won't go off topic and discuss the code with you, but you should not display dates in the dropdown that are "unvalid".

That being said, though, I think your actual question is

How do I not "highlight" any date on the calendar, when they pick incorrect dates?

Right now, you're highlighting it (I think) by this

    Calendar1.SelectedDate = datenowforcalendar;

And you're "clearing" the highlight by setting the date to null

   Calendar1.SelectedDate = null;

But the selectedDate property you're referencing is a date object used by the control.  It shouldn't be nullable by you.

Instead, try playing with the style of the selected object...  Use one style for when a valid selected date is set, and use a different style when it is not set.

Or, use a date that you know means "not valid"...  a sentinel value.   I think it just needs to be a valid date, outside the range of what you're trying to manipulate.  Depending on your application, it might be 1/1/1901, or whatever.

-john

0
 

Author Comment

by:CharlieDev
ID: 22813502
Thanks for the advice. My biggest problem at the moment is
 line 119           if (dateforcalendar.IsValid)

I get the error that IsValid cant be used with a datetime variable, do you have any thoughts on what I could use instead to test if the date is valid or not?

I think if I can sort that bit I will be able to sortout the rest

Cheers
0
 
LVL 11

Accepted Solution

by:
jgordos earned 450 total points
ID: 22813888
I don't think it's as simple as you want it to be...

DateTime is really just a 64-bit integer value... it's the number of "ticks" since midnight, Jan 1 of the year 0001.

A tick is a tenth of a second.

So, simply put, when you set a dateTime value, you simply do the math to multiply the number of years * the number of ticks in a year, plus the number of ticks since the current year began... that would be the value that DateTime.Now is returning.

So... any date that is parseable is valid to be assigned to the variable.   Parseable does not mean "correct"; it simply means did the routine that knows how to do all that math do some math and return a number.  If not, it will throw an exception... but that exception just indicates that the FORMAT of the string was unparsable... not that it has an invalid date....

I think you'll need to write a routine that does this validation for you.  So instead of

if( dateforcalendar.IsValid )

You'll need something like

if( myDateValidator( dateforcalendar ) == true )

This might be brilliance... it might not.... but my thinking is this...

you know what the format string was that you initially tried to parse...

ie "2008/11/31"

Which we know is invalid, for instance.

So, you save that string, parse it into a dateTime, then turn it right back around to a different string.  Compare them.  If the two strings don't match, then you know that it's an invalid date.

For Instance:
String myDateToCheck = "2008/11/31";
DateTime myDateTimeValidator = DateTime.parse( myDateToCheck );
String myValidatorString = myDateTimeValidator.ToString( The right format string here that i don't have time to look up );

if( myValidatorString.equals( myDateToCheck ) )
{
// then the date was valid
}


My thinking is this...

The parser will set the datetime # of ticks when parsing 2008/11/31 to the # of ticks in november plus the number of ticks in 31 days...

Which, is really, December 1, 2008....

So the format string returned will be

2008/12/01...

And they won't match.  If they do match it was all beautiful.

-john



0
 

Author Comment

by:CharlieDev
ID: 22813956
Wow, thanks. I will give that a go :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 50 total points
ID: 22816334
Your problem is here:

        string datehighlight =  dateDrop.SelectedValue + monthdrop.SelectedValue + year.SelectedValue;

This way you can construct invalid date strings.
To avoid that, do this:

DateTime datehighlight = new DateTime(
  (int)year.SelectedValue,
  (int)monthdrop.SelectedValue,
  (int)dateDrop.SelectedValue);

/gustav
0
 

Author Comment

by:CharlieDev
ID: 22820343
Hi John,

Thanks for the genuis idea, the only problem is that when the date of say 31st november is changed into datetime format it throws an error that the date is not valid! Which has made it easy for me to just find if a date is valid or not.
 I used the following code:
 public void SelectMonth(object sender, EventArgs e)

    {

        hidden.Visible = false;

        object result = null;

        string datehighlight = dateDrop.SelectedValue + monthdrop.SelectedValue + year.SelectedValue;

        String myDateToCheck = "20" + year.SelectedValue + "-" + monthdrop.SelectedValue + "-" + dateDrop.SelectedValue;
 

        DateTime myDateTimeValidator = DateTime.Now;

        bool isValid = DateTime.TryParse(myDateToCheck, out myDateTimeValidator);
 

        Calendar1.SelectedDate = myDateTimeValidator;

        Calendar1.VisibleDate = myDateTimeValidator;
 

        if (isValid)

        {
 

            string monthID = myDateTimeValidator.ToString("MMMM yy");

            string dateSelect = myDateTimeValidator.ToString("dd");
 
 
 
 

            OpenConnection();
 

            SqlCommand commandviewfixts = new SqlCommand("exec usp_GetMonthFixtures @monthID, @dateSelect", conn);
 

            commandviewfixts.Parameters.Add("monthID", SqlDbType.NVarChar);

            commandviewfixts.Parameters["monthID"].Value = monthID;

            commandviewfixts.Parameters.Add("dateSelect", SqlDbType.NVarChar);

            commandviewfixts.Parameters["dateSelect"].Value = dateSelect;
 
 

            result = commandviewfixts.ExecuteScalar();

            if (result == null)

            {

                hidden.Visible = true;
 

            }
 

            CloseConnection();
 

            OpenConnection();
 
 

            SqlCommand mySqlSelect = new SqlCommand("exec usp_GetMonthAllFixtures @MonthID , @dateSelect", conn);
 

            mySqlSelect.Parameters.Add("MonthID", SqlDbType.NVarChar);

            mySqlSelect.Parameters["MonthID"].Value = monthID;

            mySqlSelect.Parameters.Add("dateSelect", SqlDbType.NVarChar);

            mySqlSelect.Parameters["dateSelect"].Value = dateSelect;
 
 

            mySqlSelect.CommandType = CommandType.Text;
 

            SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect); DataSet DayDataSet = new DataSet();
 

            mySqlAdapter.Fill(DayDataSet);
 

            RepeaterDay.DataSource = DayDataSet;
 

            RepeaterDay.DataBind();
 

            int k = 0;
 

            foreach (RepeaterItem repeaterItem in RepeaterDay.Items)

            {

                OpenConnection();
 

                string date = DayDataSet.Tables[0].Rows[k]["Date"].ToString();
 
 

                {

                    SqlCommand mySqlSelectAM = new SqlCommand("exec usp_GetMonthAMFixtures @dateSelect, @MonthID", conn);
 

                    mySqlSelectAM.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                    mySqlSelectAM.Parameters["dateSelect"].Value = dateSelect;

                    mySqlSelectAM.Parameters.Add("MonthID", SqlDbType.NVarChar);

                    mySqlSelectAM.Parameters["MonthID"].Value = monthID;
 

                    mySqlSelectAM.CommandType = CommandType.Text;
 

                    SqlDataAdapter mySqlAdapterAM = new SqlDataAdapter(mySqlSelectAM);
 

                    DataSet DayDataSetAM = new DataSet();
 

                    mySqlAdapterAM.Fill(DayDataSetAM);
 

                    if (DayDataSetAM.Tables[0].Rows.Count <= 0)

                    {

                        DayDataSetAM.Tables[0].Rows.Add(new object[] { "No events", "" });

                    }
 

                    ((Repeater)(repeaterItem.FindControl("RepeaterAM"))).DataSource = DayDataSetAM;
 

                    ((Repeater)(repeaterItem.FindControl("RepeaterAM"))).DataBind();

                }

                {

                    SqlCommand mySqlSelectPM = new SqlCommand("exec usp_GetMonthPMFixtures @dateSelect, @MonthID", conn);
 

                    mySqlSelectPM.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                    mySqlSelectPM.Parameters["dateSelect"].Value = dateSelect;

                    mySqlSelectPM.Parameters.Add("MonthID", SqlDbType.NVarChar);

                    mySqlSelectPM.Parameters["MonthID"].Value = monthID;
 

                    mySqlSelectPM.CommandType = CommandType.Text;
 

                    SqlDataAdapter mySqlAdapterPM = new SqlDataAdapter(mySqlSelectPM);
 

                    DataSet DayDataSetPM = new DataSet();
 

                    mySqlAdapterPM.Fill(DayDataSetPM);
 

                    if (DayDataSetPM.Tables[0].Rows.Count <= 0)

                    {

                        DayDataSetPM.Tables[0].Rows.Add(new object[] { "No events", "" });

                    }
 

                    ((Repeater)(repeaterItem.FindControl("RepeaterPM"))).DataSource = DayDataSetPM;
 

                    ((Repeater)(repeaterItem.FindControl("RepeaterPM"))).DataBind();
 

                }

                {

                    SqlCommand mySqlSelectCL = new SqlCommand("exec usp_GetMonthClubFixtures @dateSelect, @MonthID", conn);
 

                    mySqlSelectCL.Parameters.Add("dateSelect", SqlDbType.NVarChar);

                    mySqlSelectCL.Parameters["dateSelect"].Value = dateSelect;

                    mySqlSelectCL.Parameters.Add("MonthID", SqlDbType.NVarChar);

                    mySqlSelectCL.Parameters["MonthID"].Value = @monthID;
 

                    mySqlSelectCL.CommandType = CommandType.Text;
 

                    SqlDataAdapter mySqlAdapterCL = new SqlDataAdapter(mySqlSelectCL);
 

                    DataSet DayDataSetCL = new DataSet();
 

                    mySqlAdapterCL.Fill(DayDataSetCL);
 

                    if (DayDataSetCL.Tables[0].Rows.Count <= 0)

                    {

                        DayDataSetCL.Tables[0].Rows.Add(new object[] { "No events", "" });

                    }
 

                    ((Repeater)(repeaterItem.FindControl("RepeaterCL"))).DataSource = DayDataSetCL;
 

                    ((Repeater)(repeaterItem.FindControl("RepeaterCL"))).DataBind();

                }
 

                k++;
 
 
 

            }

            CloseConnection();

        }

        else

        {

            error.Visible = true;

            RepeaterDay.Visible = false;

        }

    }

Open in new window

0
 

Author Closing Comment

by:CharlieDev
ID: 31510253
Thanks for your time and help :)
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 22820417
You are welcome!

/gustav
0
 
LVL 11

Expert Comment

by:jgordos
ID: 22832956
Cool... Glad I could help.

I actually ddn't realize there was a TryParse...  This one will go in the goodiebag...

-john
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now