Hi all,
On my website ASPNET C# a dropdownlist ddlmonth with the nominative months is available
private void mtmonthbind()
{
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
int currentMonth = DateTime.Now.Month;
for (int i = 1; i < 13; i++)
{
bool isMonthInPast = ((i + 1) < currentMonth) || (i + 1 == currentMonth && DateTime.Now.Day > 20);
if (!isMonthInPast)
ddlmonth.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
}
}
I need to check that the value of the month selected in the dropdownlist is not different from the current month -1.
E.g.: if the value of current month is 10 (october) and I select on dropdownlist 12 (december) value it is not possible to continue. Instead if select on dropdownlist 11 (november) value the selection is valid.
This is my code for check
DateTime d = DateTime.Now;
d = d.AddMonths(-1);
string month = d.ToString("MMMM");
DateTime dt = new DateTime(1, Int32.Parse(ddlmonth.SelectedValue), 1);
if (d.Month.ToString() != ddlmonth.SelectedValue.ToString())
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Stop!The selected month " + dt.ToString("MMMM").ToUpper()
+ "is different from the month of " + month.ToString().ToUpper() + "');", true);
}
else
{
// to be continued
}
But I have a problem... this check must not work when the current month is July 7... right now if I select august value 8, in the dropdownlist the system stops me...
I have tried using
if (d.Month.ToString() != ddlmonth.SelectedValue.ToString() &&
Convert.ToInt32(ddlmonth.SelectedValue) != 8)
But when select october value 10, in the dropdownlist the system reacts with
Stop! The selected month September is different from the month of July
Any suggestion, please?