Validate DropDownList selected month with current month using C# in ASP.Net

comunidadmexicanaroma
 
on Aug 30, 2021 11:07 PM
Sample_409749.zip
557 Views

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?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 31, 2021 06:27 AM

Hi comunidadmexi...,

Refer below code.

HTML

<asp:DropDownList runat="server" ID="ddlmonth" AutoPostBack="true" OnSelectedIndexChanged="ddlmonth_SelectedIndexChanged">
</asp:DropDownList>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        mtmonthbind();
    }
}

private void mtmonthbind()
{
    System.Globalization.DateTimeFormatInfo info = System.Globalization.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()));
    }
}

protected void ddlmonth_SelectedIndexChanged(object sender, EventArgs e)
{
    DateTime currentDate = DateTime.Now.AddMonths(2);
    string month = currentDate.ToString("MMMM");
    DateTime dt = new DateTime(1, Int32.Parse(ddlmonth.SelectedValue), 1);
    if (currentDate.Month <= Convert.ToInt32(ddlmonth.SelectedValue))
    {
        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);
    }
}

VB.Net

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        mtmonthbind()
    End If
End Sub

Private Sub mtmonthbind()
    Dim info As System.Globalization.DateTimeFormatInfo = System.Globalization.DateTimeFormatInfo.GetInstance(Nothing)
    Dim currentMonth As Integer = DateTime.Now.Month

    For i As Integer = 1 To 13 - 1
        Dim isMonthInPast As Boolean = ((i + 1) < currentMonth) OrElse (i + 1 = currentMonth AndAlso DateTime.Now.Day > 20)
        If Not isMonthInPast Then ddlmonth.Items.Add(New ListItem(info.GetMonthName(i), i.ToString()))
    Next
End Sub

Protected Sub ddlmonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim currentDate As DateTime = DateTime.Now.AddMonths(2)
    Dim month As String = currentDate.ToString("MMMM")
    Dim dt As DateTime = New DateTime(1, Int32.Parse(ddlmonth.SelectedValue), 1)

    If currentDate.Month <= Convert.ToInt32(ddlmonth.SelectedValue) Then
        Page.ClientScript.RegisterStartupScript(Me.[GetType](), "Msg", "alert('Stop! The selected month " & dt.ToString("MMMM").ToUpper() & " is different from the month of " & month.ToString().ToUpper() & "');", True)
    End If
End Sub