Avatar of PNKJ
PNKJ

asked on 

Fill drop down dynamically

Hi,

I have a drop down list that needs to list month and year staring Jan 2001

For ex Jan 2008
Jan 2007
Jan 2006 and so on and this value is the input in the form of mmyyyy  012001 for another stored procedure . How can you fill the dropdown at run time starting from current year to 2001 or do we need to store the month and year in a table.
.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
GreymanMSC
Avatar of fanopoe
fanopoe
Flag of United States of America image

put this in your page load event:

        If Not Page.IsPostBack Then
            Dim iMonth As Integer
            For iYear As Integer = 2001 To 2008
                For iMonth = 1 To 12
                    Dim sVal As String
                    Dim sDate As String
                    Select Case iMonth
                        Case 1
                            sVal = "01" & iYear.ToString
                            sDate = "JAN " & iYear.ToString
                        Case 2
                            sVal = "02" & iYear.ToString
                            sDate = "FEB " & iYear.ToString
                        Case 3
                            sVal = "03" & iYear.ToString
                            sDate = "MAR " & iYear.ToString
                        Case 4
                            sVal = "04" & iYear.ToString
                            sDate = "APR " & iYear.ToString
                        Case 5
                            sVal = "05" & iYear.ToString
                            sDate = "MAY " & iYear.ToString
                        Case 6
                            sVal = "06" & iYear.ToString
                            sDate = "JUN " & iYear.ToString
                        Case 7
                            sVal = "07" & iYear.ToString
                            sDate = "JUL " & iYear.ToString
                        Case 8
                            sVal = "08" & iYear.ToString
                            sDate = "AUG " & iYear.ToString
                        Case 9
                            sVal = "09" & iYear.ToString
                            sDate = "SEP " & iYear.ToString
                        Case 10
                            sVal = "10" & iYear.ToString
                            sDate = "OCT " & iYear.ToString
                        Case 11
                            sVal = "11" & iYear.ToString
                            sDate = "NOV " & iYear.ToString
                        Case 12
                            sVal = "12" & iYear.ToString
                            sDate = "DEC " & iYear.ToString
                    End Select
                    Dim li As ListItem = New ListItem(sDate, sVal)
                    Me.YourDDLName.Items.Add(li)
                Next
            Next
        End If
        Me.YourDDLName.DataBind
Avatar of Alok-Agarwal
Alok-Agarwal
Flag of India image

Hi,

I would suggest that rather than  hardcoing the current year it should be retrieved programtically so that program beocmes generic e.g. in C# you would do something like this

int currYear = 0;
currYear = System.DateTime.Today.Year;
The currYear should then be used as upper limit in the For Loop.
ASKER CERTIFIED SOLUTION
Avatar of GreymanMSC
GreymanMSC

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of PNKJ
PNKJ

ASKER

Dim sValue As String = iMonth.ToString.PadLeft(2, "0") & iYear.ToString .

I get error at this line for "0" option strict allows implicit conversion from string to char
Avatar of GreymanMSC
GreymanMSC

If you have the Option Strict On setting, then you will need to explicitly convert all variables.  

It's quite annoying but safer practice, I suppose.

Use the Convert(,) function to do this, or the built in converters such as the CChar( ) function.
Dim sValue As String = iMonth.ToString.PadLeft(2, CChar("0")) & iYear.ToString .

Open in new window

.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo