?
Solved

How to dynamically fetch closest selected value on dropdown formfields.

Posted on 2012-03-19
19
Medium Priority
?
368 Views
Last Modified: 2012-03-26
Hello, I have a form with two dropdown form fields. The user wants to have the selectedvalue prepolated: DDLFrom needs to be preselected to yesterday's value, the DDLTo needs to be preselected to today's date. Here is the catch: the dropdown fields are dynamically loaded from a table and only displays the dates where an issue was logged.
See attached screenshot. So my question is for the DDLFrom I need the closest date to yesterday and for DDLTo needs to be the most recent date.

This code throws an error since there was no today's date nor was there a date for yesterday since it was the weekend and so no activities.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Me.ddlFrom.SelectedValue = DateTime.Now.AddDays(-1).ToString("MM/dd/yyyy")
            Me.ddlTo.SelectedValue = DateTime.Now.ToString("MM/dd/yyyy")
        End If
    End Sub

Open in new window

SS.jpg
0
Comment
Question by:JessyRobinson1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
19 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37737623
dim dt as String

dt = Datetime.Now.AddDays(-1).Tostring("mm/dd/yyyy")

while not ddlFrom.Items.Contains(dt)
   dt = DateTime.Parse(dt).AddDays(-1).Tostring("mm/dd/yyyy")
end while



If the combobox is ordered:

ddlTo.Selectedindex = ddlTo.Items.Count -1

if it's not ordered you can do a loop.
0
 

Author Comment

by:JessyRobinson1234
ID: 37737649
Thank you, I get the error on While Not ddlFrom.Items.Contains(dt):

Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.ListItem'.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37737760
try:
Contains (New ListItem(dt))
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:JessyRobinson1234
ID: 37737771
Error on: dt = DateTime.Parse(dt).AddDays(-1).ToString("mm/dd/yyyy")

String was not recognized as a valid DateTime.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37738219
mm should be MM
0
 

Author Comment

by:JessyRobinson1234
ID: 37738430
New Error on dt = DateTime.Parse(dt).AddDays(-1).ToString("MM/dd/yyyy")

The added or subtracted value results in an un-representable DateTime.
Parameter name: value
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37738753
Needs to be done in both places:

dt = Datetime.Now.AddDays(-1).Tostring("MM/dd/yyyy")

while not ddlFrom.Items.Contains(dt)
   dt = DateTime.Parse(dt).AddDays(-1).Tostring("MM/dd/yyyy")
end while
0
 

Author Comment

by:JessyRobinson1234
ID: 37739253
I actually did and still get the error.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37739691
Rework:

dim days as integer = 2
dt = Datetime.Now.AddDays(-1).Tostring("MM/dd/yyyy")



while not ddlFrom.Items.Contains(dt)
   dt = DateTime.Now.AddDays(-1 * days).Tostring("MM/dd/yyyy")
  days += 1  'keep going backward in time.
end while
0
 

Author Comment

by:JessyRobinson1234
ID: 37744533
Hi I am following the logic. However, it goes in an infinite loop and throws this error:

The added or subtracted value results in an un-representable DateTime.
Parameter name: value
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37747282
It means it's not finding the date.

How is your combobox populated?
0
 

Author Comment

by:JessyRobinson1234
ID: 37747831
ASP Tag:

<asp:DropDownList ID="ddlFrom" runat="server" DataSourceID="sdsWE" DataTextField="MyDate" DataValueField="MyDate" CssClass="form" AutoPostBack="True" ></asp:DropDownList>                        &nbsp;
     <asp:SqlDataSource ID="sdsWE" runat="server" ConnectionString="<%$ ConnectionStrings:BusDevConnectionString %>" SelectCommand="View_WE_DD" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

Open in new window


Stored Procedure:
SELECT distinct convert(varchar(10),Ship_Date,101) 'MyDate',
ship_date 'ResolvedDate'
--	convert(varchar(10),Resolved_Date,101) 'MyDate'

      FROM Lear_Shipping
where convert(varchar(10),Ship_Date,101) IS NOT NULL  AND Ship_Date > '12/01/2011'
Order By 'ResolvedDate' DESC

Open in new window

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37748631
Try this:

while not ddlFrom.Items.Contains(new ListItem(dt,dt))
   dt = DateTime.Now.AddDays(-1 * days).Tostring("MM/dd/yyyy")
  days += 1  'keep going backward in time.
end while


ddlFrom.SelectedValue = dt
0
 

Author Comment

by:JessyRobinson1234
ID: 37751722
Error:

The added or subtracted value results in an un-representable DateTime. Parameter name: value
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37752432
are there actual values in the ddlFrom?

If so, can you send a screen shot?  Is it finding the date or still overflowing?
0
 

Author Comment

by:JessyRobinson1234
ID: 37752475
Thanks. That didn't work. For some odd reason the selected value of ddlFrom remains ""
0
 

Author Comment

by:JessyRobinson1234
ID: 37752509
I think it may have something to do with the fact that I am running this code on page_load and therefore no databound yet. When I add ddlFrom.databind() prior to the while loop it works.
0
 

Author Comment

by:JessyRobinson1234
ID: 37752579
This works, but can i wirte it all in one and the same while loop?

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

            Dim dtFrom As String
            Dim dtTo As String
            Dim days As Integer = 2

            ddlFrom.DataBind()
            ddlTo.DataBind()

            dtFrom = DateTime.Now.AddDays(-1).ToString("MM/dd/yyyy")
            dtTo = DateTime.Now.ToString("MM/dd/yyyy")

            While Not ddlFrom.Items.Contains(New ListItem(dtFrom, dtFrom))
                dtFrom = DateTime.Now.AddDays(-1 * days).ToString("MM/dd/yyyy")
                days += 1  'keep going backward in time.
            End While

            While Not ddlTo.Items.Contains(New ListItem(dtTo, dtTo))
                dtTo = DateTime.Now.AddDays(-1 * days).ToString("MM/dd/yyyy")
                days += 1  'keep going backward in time.
            End While

            ddlFrom.SelectedValue = dtFrom
            ddlTo.SelectedValue = dtTo

        End If
    End Sub

Open in new window

0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 37753912
Only if dtFrom and To are populate and ordered in the same exact way.

if that's the case you can eliminate the second loop.

If they're different, or can be different, use 2 loops.

In terms of efficiencies you should only have a couple of executions max . . . it'll still be fast.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

777 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