Solved

c# asp.net calendar object-change the highlighted date on click of an asp button

Posted on 2008-10-23
20
2,784 Views
Last Modified: 2013-12-17
Hi,
I am trying to get the date on a dot net calendar object to change when a button on the same aspx page is clicked. Currently the date can be selected by clicking on the actual calendar but i need to have control from  both the calendar and also from a drop down box containing dates and a submit date button.

I cant find what command i need to tell the calendar to change its highlighted date when the submit date button is pressed.

Does anyone know the command i need and where to put it in my code.


aspx file

<div class="innerContentContainer">
                    <div class="innerContentContainerLeftCal">
                    <form id="form1" runat="server">
                    <asp:Calendar id="Calendar1"
                     OnSelectionChanged="Date_Selected"
                     width="429px"
                     height="280px"
                     Font-Name="Verdana"
                     Font-Size="16px"
                     padding-left="5px"
                     NextPrevFormat="ShortMonth"
                     border-style="none"
                     text-decoration="none"
                     runat="server">
                   <TodayDayStyle Font-Bold="True" BackColor="#336633"/>
                   <DayHeaderStyle Font-Bold="True"/>
                   <OtherMonthDayStyle ForeColor="gray"/>
                   <TitleStyle BackColor="#ffffff"
                               ForeColor="#000"
                               Font-Bold="True"/>

                   <SelectedDayStyle BackColor="#ee3f09"
                                     Font-Bold="True"
                                     />
                   <NextPrevStyle ForeColor="#000"
                                  Font-Size="12px"/>
                   <SelectorStyle BackColor="#336633"
                                  ForeColor="navy"
                                  Font-Size="9px"/>
                </asp:Calendar>
    <asp:Label ID="Label1" runat="server" ></asp:Label>
                    </div>
                    <div class="innerContentContainerRightTall">
                        <div class="fixturesTitle">
                            <h1 title="Fixtures">
                                <img src="../images/fixtures_text.gif" alt="Fixtures Diary" /></h1>
                        </div>
                        <div class="innerContentContainerRightFix">
                            <div class="fixturesHeader">
                               
                                <div class="fourColumnContainerFixtures">
                                    <div class="colu1">
                                        <asp:Button ID="select_month" OnClick="SelectMonth" runat="server" Text="Please select date" Width="125px" />
                                    </div>
                                    <div class="colu2">
                                        <asp:DropDownList ID="dateDrop" runat="server" DataValueField="" Width="55px">
                                            <asp:ListItem Value="" Text="Date" Selected="True" />
                                            <asp:ListItem Value="01" Text="01"  />
                                            <asp:ListItem Value="02" Text="02"  />
                                            <asp:ListItem Value="03" Text="03" />
                                            <asp:ListItem Value="04" Text="04"  />
                                            <asp:ListItem Value="05" Text="05"  />
                                            <asp:ListItem Value="06" Text="06" />
                                            <asp:ListItem Value="07" Text="07"  />
                                            <asp:ListItem Value="08" Text="08" />
                                            <asp:ListItem Value="09" Text="09" />
                                            <asp:ListItem Value="10" Text="10" />
                                            <asp:ListItem Value="11" Text="11" />
                                            <asp:ListItem Value="12" Text="12"/>
                                            <asp:ListItem Value="13" Text="13"  />
                                            <asp:ListItem Value="14" Text="14"  />
                                            <asp:ListItem Value="15" Text="15" />
                                            <asp:ListItem Value="16" Text="16"  />
                                            <asp:ListItem Value="17" Text="17"  />
                                            <asp:ListItem Value="18" Text="18" />
                                            <asp:ListItem Value="19" Text="19"  />
                                            <asp:ListItem Value="20" Text="20" />
                                            <asp:ListItem Value="21" Text="21" />
                                            <asp:ListItem Value="22" Text="22" />
                                            <asp:ListItem Value="23" Text="23" />
                                            <asp:ListItem Value="24" Text="24"/>
                                            <asp:ListItem Value="25" Text="25"  />
                                            <asp:ListItem Value="26" Text="26"  />
                                            <asp:ListItem Value="27" Text="27" />
                                            <asp:ListItem Value="28" Text="28"  />
                                            <asp:ListItem Value="29" Text="29"  />
                                            <asp:ListItem Value="30" Text="30" />
                                            <asp:ListItem Value="31" Text="31"  />
                                        </asp:DropDownList>
                                    </div>
                                    <div class="colu3">
                                        <asp:DropDownList ID="monthdrop" runat="server" DataValueField="" Width="95px">
                                            <asp:ListItem Value="" Text="Month" Selected="True" />
                                            <asp:ListItem Value="January" Text="January"  />
                                            <asp:ListItem Value="February" Text="February"  />
                                            <asp:ListItem Value="March" Text="March" />
                                            <asp:ListItem Value="April" Text="April"  />
                                            <asp:ListItem Value="May" Text="May"  />
                                            <asp:ListItem Value="June" Text="June" />
                                            <asp:ListItem Value="July" Text="July"  />
                                            <asp:ListItem Value="August" Text="August" />
                                            <asp:ListItem Value="September" Text="September" />
                                            <asp:ListItem Value="October" Text="October" />
                                            <asp:ListItem Value="November" Text="November" />
                                            <asp:ListItem Value="December" Text="December"/>
                                        </asp:DropDownList>
                                    </div>
                                    <div class="colu4">
                                         <asp:DropDownList ID="year" runat="server" DataValueField="" Width="55px">
                                         </asp:DropDownList>
                                    </div>  
                                   <div class="colu5">
                                         <a href="../index.html">Print all month fixtures</a>
                                    </div>  
                                   
                                </div>
                               <div class="columnC">
                                 <div id="hidden" runat="server">
                                   <p>No data available yet</p>
                                 </div>
                               </div>
                            </div>
                                        <div class="eventBox">
                                      <asp:Repeater ID="RepeaterDay" runat="server">
                                       <ItemTemplate>
                                          <div class="DateDayTitle"><div class="dayTitle"><%# DataBinder.Eval(Container.DataItem,"Day") %></div><div class="dateTitle"><%# DataBinder.Eval(Container.DataItem,"Date") %></div><div class="monthTitle"><%# DataBinder.Eval(Container.DataItem,"Month") %></div></div>
                                           <div class="heading">
                                             <div class="amTeeTitle">AM Tee:</div>
                                             <asp:Repeater ID="RepeaterAM" runat="server">
                                               <ItemTemplate>
                                                <div class="columnHeight"><div class="C2olumn"><span class="empty"></span><span class="emphasis"><%# DataBinder.Eval(Container.DataItem,"Time") %></span><div class="p"><%# DataBinder.Eval(Container.DataItem,"Event") %></div></div></div>
                                               </ItemTemplate>
                                             </asp:Repeater>
                                          </div>
                                          <div class="heading">
                                             <div class="pmTeeTitle">PM Tee:</div>
                                             <asp:Repeater ID="RepeaterPM" runat="server">
                                               <ItemTemplate>
                                                <div class="columnHeight"><div class="C2olumn"><span class="empty"></span><span class="emphasis"><%# DataBinder.Eval(Container.DataItem,"Time") %></span><div class="p"><%# DataBinder.Eval(Container.DataItem,"Event") %></div></div></div>
                                               </ItemTemplate>
                                             </asp:Repeater>
                                          </div>
                                          <div class="heading">
                                             <div class="clubhouseTitle">Clubhouse:</div>
                                             <asp:Repeater ID="RepeaterCL" runat="server">
                                               <ItemTemplate>
                                                <div class="columnHeight"><div class="C2olumn"><span class="empty"></span><span class="emphasis"><%# DataBinder.Eval(Container.DataItem,"Time") %></span><div class="p"><%# DataBinder.Eval(Container.DataItem,"Event") %></div></div></div>
                                               </ItemTemplate>
                                             </asp:Repeater>
                                          </div>
                                          </ItemTemplate>
                                       </asp:Repeater>
                                    </div>
                                   
                               
                           
                            </form>
                        </div>
                       
                    </div>
c# file
 

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 = "MMMM yy";
 

            DateTime now = DateTime.Now;
 

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

            DisplayMonth(dateID);
 

            string strMonth = dateID.Split(' ')[0];

            string strYear = dateID.Split(' ')[1];
 

            monthdrop.Text = strMonth;

            year.Text = strYear;

        }
 
 

    }
 

    public void SelectMonth(object sender, EventArgs e)

    {

        hidden.Visible = false;

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

        object result = null;

        string dateSelect = dateDrop.SelectedValue;
 

        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 dateSelected = dateDrop.SelectedValue;

        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 = dateSelected;
 

        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 @Date, @MonthID", conn);
 

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

                mySqlSelectAM.Parameters["Date"].Value = date;

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

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

                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 @Date, @MonthID", conn);
 

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

                mySqlSelectPM.Parameters["Date"].Value = date;

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

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

                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 @Date, @MonthID", conn);
 

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

                mySqlSelectCL.Parameters["Date"].Value = date;

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

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

                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);

        Response.Write(convertedSelectedYear);

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

        Response.Write(convertedSelectedDate);

    }
 

}

Open in new window

cal.jpg
0
Comment
Question by:CharlieDev
  • 11
  • 9
20 Comments
 
LVL 10

Expert Comment

by:Bane83
ID: 22785898
In your button click event, change the calendar's SelectedDate property to the appropriate date.
0
 

Author Comment

by:CharlieDev
ID: 22786347
Cool, how do I then convert the string thats in the value of the dropdown list to a datetime and assign the Calendar1.SelectedDate to that value?

Can I just add all the three dropdown lists together like this:

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

and then do something like this:

DateTime dateforcalendar = datehighlight.Convert.ToDateTime;

Calendar1.SelectedDate = dateforcalendar;


I have an error about converting string to datetime

Thanks :)
0
 

Author Comment

by:CharlieDev
ID: 22786458
string datehighlight =  dateDrop.SelectedValue + monthdrop.SelectedValue + year.SelectedValue;

        DateTime dateforcalendar = DateTime.Parse(datehighlight);

        Calendar1.SelectedDate = dateforcalendar;

has worked

So think thats problem solved pretty much


Thanks
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22786464
Like this:
string date = dateDrop.SelectedValue + "/" + monthdrop.SelectedValue + "/" + year.SelectedValue;
 

DateTime? dateValue = null;
 

CultureInfo enUS = new CultureInfo("en-US");

DateTime.TryParseExact(date, "dd/MM/yyyy", enUS, DateTimeStyles.None, out dateValue);
 

if (dateValue.HasValue)

{

    Calendar1.SelectedDate = dateValue.Value;

}

else

{

    // Show error

}

Open in new window

0
 
LVL 10

Expert Comment

by:Bane83
ID: 22786512
Your method will work, but it can be tricky since DateTime.Parse will use the regional settings of the server to parse the date and this may not always be in the format you want.  Take 05/09/2008 as an example.  You may be expecting it to be May 9th, 2008, but the server could be set to use dates at dd/MM/yyyy and you would instead get September 5th, 2008.  TryParseExact will give you the date as you want it, but keep in mind, the date the user enters may not always be valid which is why I do an extra check.  The user could try and enter September 31st (only 30 in September) or February 29th on a non-leap year and TryParseExact will catch these and not return a value.
0
 

Author Comment

by:CharlieDev
ID: 22786540
Only problem is that when i change the month on the drop down list it doesnt change the calendar image to be that month it stays on the original month with no date highlighted.

Any ideas?

Thanks
0
 

Author Comment

by:CharlieDev
ID: 22786569
I didnt see your last comment before i posted my last one!  I will try the TryParseExact

Cheers
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22786648
Ah, right, add this too:

Calendar1.VisibleDate = dateValue.Value;
0
 

Author Comment

by:CharlieDev
ID: 22786777
Sorry I'm being stupid, how do i get the TryParseExact to work, i'm trying the below:

DateTime dateforcalendar = DateTime.TryParseExact(datehighlight);

Thanks very much
0
 

Author Comment

by:CharlieDev
ID: 22786858
yep, defineately being stupid, i didnt see your code snippet, sorry.
So ignore the comment before this!!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:CharlieDev
ID: 22787204
I'm getting an error message i cant resolve:

CS1502: The best overloaded method match for 'System.DateTime.TryParseExact(string, string, System.IFormatProvider, System.Globalization.DateTimeStyles, out System.DateTime)' has some invalid arguments

from the line of code:

DateTime.TryParseExact(datehighlight, "dd/MM/yyyy", enUK, DateTimeStyles.None, out dateValue);

Cheers
 string datehighlight = dateDrop.SelectedValue + "/" + monthdrop.SelectedValue + "/" + year.SelectedValue;
 

        DateTime? dateValue = null;
 

        CultureInfo enUK = new CultureInfo("en-UK");

        DateTime.TryParseExact(datehighlight, "dd/MM/yyyy", enUK,      DateTimeStyles.None, out dateValue);
 

        if (dateValue.HasValue)

        {

            Calendar1.SelectedDate = dateValue.Value;

            Calendar1.VisibleDate = dateValue.Value;

        }

        else

        {

            // Show error

        }

Open in new window

0
 
LVL 10

Accepted Solution

by:
Bane83 earned 500 total points
ID: 22787324
Ah, sorry, change this as below:

Also note that I changed en-UK to en-GB.  I got a "not supported" error with en-UK and a quick bit of research says that ISO specifications state to use en-GB over en-UK.
string datehighlight = dateDrop.SelectedValue + "/" + monthdrop.SelectedValue + "/" + year.SelectedValue;

 

        DateTime dateValue;

 

        CultureInfo enUK = new CultureInfo("en-GB");

 

        if (DateTime.TryParseExact(datehighlight, "dd/MM/yyyy", enUK, DateTimeStyles.None, out dateValue))

        {

            Calendar1.SelectedDate = dateValue.Value;

            Calendar1.VisibleDate = dateValue.Value;

        }

        else

        {

            // Show error

        }

Open in new window

0
 

Author Comment

by:CharlieDev
ID: 22787551
Thanks for that but its still not quite working

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

from code line:
Calendar1.SelectedDate = dateValue.Value;

Any other ideas?
Thanks
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22787582
Yup, just get rid of the .Value; on dateValue.  Forgot to remove those when I switch it to a non-nullable datetime.
0
 

Author Comment

by:CharlieDev
ID: 22787743
Sorry to say this but the date is now not being highlighted!!
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22787847
That should be doing it.  Are you using an UpdatePanel on the page?
Also please add some sort of error handling so that we can know whether or not the parse is failing.
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22787858
By error handling, I mean in the else part of the if, just set some label text or something.
0
 

Author Comment

by:CharlieDev
ID: 22787998
I'm not sure what an updatepanel is? Seeing as I have now got my other code working ,I think I'll stick with that:
string datehighlight =  dateDrop.SelectedValue + monthdrop.SelectedValue + year.SelectedValue;

        DateTime dateforcalendar = DateTime.Parse(datehighlight);

        Calendar1.SelectedDate = dateforcalendar;
        Calendar1.VisibleDate = dateforcalendar;

I hope I'm not going to regret it !!! We'll see hey.

Thanks very much for your help
0
 

Author Closing Comment

by:CharlieDev
ID: 31509295
Thanks for your help on this, sorry i bailed out on trying to get the better version of the code working. I am really new to this and I really didnt understand the more complicated code. So hopefully I can get away with my code.
Cheers :)
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22788051
Alright, I hope that works for you.  I'll give you a bit of research now regarding UpdatePanels and MS AJAX.

http://www.asp.net/ajax/
http://www.asp.net/ajax/documentation/live/overview/UpdatePanelOverview.aspx
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

17 Experts available now in Live!

Get 1:1 Help Now