?
Solved

Select date using 3 combo boxes for dd/mm/yyyy

Posted on 2009-04-26
21
Medium Priority
?
563 Views
Last Modified: 2012-05-06
Hello Guys, For some reason, my manager is reluctant to use the calendar control to select the date. He wants me to use 3 combo boxes one each for dd/mm/yyyy. What i'm planniing is to wtrite the selected date to a text box and then to the database. I need your help as to how to go about it. Thanks in advance
0
Comment
Question by:preethamonline
  • 9
  • 7
  • 5
21 Comments
 

Author Comment

by:preethamonline
ID: 24238446
Guys could you atleast suggest a good free control that could allow me to select date from a combo box
0
 
LVL 9

Expert Comment

by:AsishRaj
ID: 24238608
Refer Below

'desing '
<fieldset> 
<legend>Application Date: DD:MM:YYYY</legend>
   <asp:DropDownList ID="DDL_ApplicationDay" runat="server" CssClass="GlobalText" Height="18px">
   </asp:DropDownList>
   :<asp:DropDownList ID="DDL_ApplicationMonth" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDL_ApplicationMonth_SelectedIndexChanged" CssClass="GlobalText" Height="18px">
    </asp:DropDownList>
    :<asp:DropDownList ID="DDL_ApplicationYear" runat="server" OnSelectedIndexChanged="DDL_ApplicationYear_SelectedIndexChanged" 
CssClass="GlobalText" Height="18px">
    </asp:DropDownList>
</fieldset>
 
'Code Behind'
 
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim tnow As DateTime
            tnow = DateTime.Now
            Dim Year As Integer
            Dim Month As Integer
            Dim AlYear As New ArrayList
            Dim i As Integer
            For i = tnow.Year - 100 To tnow.Year
                AlYear.Add(i)
            Next
            Dim AlMonth As New ArrayList
            For i = 1 To 12
                AlMonth.Add(i)
            Next
            DDL_ApplicationYear.DataSource = AlYear
            DDL_ApplicationYear.DataBind()
            DDL_ApplicationYear.SelectedValue = tnow.Year.ToString
 
            DDL_ApplicationMonth.DataSource = AlMonth
            DDL_ApplicationMonth.DataBind()
            DDL_ApplicationMonth.SelectedValue = tnow.Month.ToString()
 
            Year = Int32.Parse(DDL_ApplicationYear.SelectedValue)
            Month = Int32.Parse(DDL_ApplicationMonth.SelectedValue)
 
            BindApplicationDays(Year, Month)
 
            DDL_ApplicationDay.SelectedValue = tnow.Day.ToString()
 
        End If
    End Sub
 
Protected Sub DDL_ApplicationYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDL_ApplicationYear.SelectedIndexChanged
        Dim Year As Integer
        Dim Month As Integer
        Year = Int32.Parse(DDL_ApplicationYear.SelectedValue)
        Month = Int32.Parse(DDL_ApplicationMonth.SelectedValue)
        BindApplicationDays(Year, Month)
    End Sub
 
Protected Sub DDL_ApplicationMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDL_ApplicationMonth.SelectedIndexChanged
        Dim Year As Integer
        Dim Month As Integer
        Year = Int32.Parse(DDL_ApplicationYear.SelectedValue)
        Month = Int32.Parse(DDL_ApplicationMonth.SelectedValue)
        BindApplicationDays(Year, Month)
    End Sub
 
Private Function CheckLeap(ByVal year As Integer) As Boolean
        If (year Mod 4 = 0) And (year Mod 100 <> 0) Or (year Mod 400 = 0) Then
            Return True
        Else
            Return False
        End If
    End Function
 
Private Sub BindApplicationDays(ByVal year As Integer, ByVal month As Integer)
 
        Dim i As Integer
        Dim AlDay As New ArrayList
        Select Case month
            Case 1, 3, 5, 7, 8, 10, 12
                For i = 1 To 31
                    AlDay.Add(i)
                Next
            Case 2
                If CheckLeap(year) Then
                    For i = 1 To 29
                        AlDay.Add(i)
                    Next
                Else
                    For i = 1 To 28
                        AlDay.Add(i)
                    Next
                End If
            Case 4, 6, 9, 11
                For i = 1 To 30
                    AlDay.Add(i)
                Next
        End Select
        DDL_ApplicationDay.DataSource = AlDay
        DDL_ApplicationDay.DataBind()
    End Sub

Open in new window

0
 
LVL 3

Expert Comment

by:RobbP
ID: 24239847
Heres a reusable solution (attached), you can determine what the ranges should be as well as change the implementations at runtime. Try it out
USER CONTROL MARKUP
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="YearMonthDayDropdown.ascx.cs" Inherits="Controls_UI_Functions_YearMonthDayDropdown" %>
 
<link href="../../../Wizard/Wizard.css" rel="Stylesheet" type="text/css" />
<table>
    <tr>
        <td>
            <asp:DropDownList ID="ddl_Years" runat="server" CssClass="list" Height="19px" 
                Width="75px">
                <asp:ListItem>Years</asp:ListItem>
            </asp:DropDownList>
        </td>
        <td>
             <asp:DropDownList ID="ddl_Months" runat="server" CssClass="list" Width="75px" 
                Height="19px">
                <asp:ListItem Selected="True">Months</asp:ListItem>
            </asp:DropDownList>
        </td>
        <td>
             <asp:DropDownList ID="ddl_Days" runat="server" CssClass="list" Width="75px" 
                Height="19px">
                <asp:ListItem Selected="True">Days</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
</table>
 
 
USER CONTROL CODE BEHIND
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sample.Combo;
using Sample;
 
public partial class Controls_UI_Functions_YearMonthDayDropdown : System.Web.UI.UserControl
{
    #region Constructs
 
    ICalculations _calculate = new Calculations();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Loadlookups();
        Page.DataBind();
    }
 
    #endregion
 
    #region Properties
 
    public string YearMonthDay_Year
    {
        get { return ddl_Years.SelectedValue; }
        set { ddl_Years.SelectedValue = value; }
    }
 
    public string YearMonthDay_Month
    {
        get { return ddl_Months.SelectedValue; }
        set { ddl_Months.SelectedValue = value; }
    }
 
    public string YearMonthDay_Day
    {
        get { return ddl_Days.SelectedValue; }
        set { ddl_Days.SelectedValue = value; }
    }
 
    #endregion
 
    #region Lookup Members
 
    public void Loadlookups()
    {
        ddl_Years.DataSource = PrepareList(20, "Years");
        ddl_Months.DataSource = PrepareList(12, "Months");
        ddl_Days.DataSource = PrepareList(31, "Days"); 
    }
 
    public List<string> PrepareList(int daterange, string defaultItem)
    {
        List<string> item = new List<string>();
        item = _calculate.GetDateRange(daterange);
        item.Insert(0, (new ListItem(defaultItem, "0")).ToString());
        return item;
    }
 
    #endregion
 
    #region ICalculations Members
 
    public List<string> GetDateRange(int range)
    {
        return _calculate.GetDateRange(range);
    }
 
    #endregion
 
}
 
COMMON SERVICE FOR DATA CALCS
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace Sample.Combo
{
    public class Calculations : Sample.Combo.ICalculations
    {
        public List<string> GetDateRange(int range)
        {
            List<string> result = new List<string>();
            for (int i = 0; i <= range; i++)
            {
                result.Add(i.ToString());
            }
 
            return result;
        }
    }
}
 
INTERFACE FOR COMMON SERVICE DATA CALCS
using System;
using System.Collections.Generic;
 
namespace Sample.Combo
{
    public interface ICalculations
    {
        List<string> GetDateRange(int range);
    }
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:preethamonline
ID: 24242540
Thanks guys. I shall try it out and let u guys know.
0
 

Author Comment

by:preethamonline
ID: 24273857
Hi Guys. like i said, i have 3 drop down list box. I tried something like this to compute the dat. On the auto post back in the third ddl i wrote the following code.

Protected Sub ddldoj3_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddldoj3.SelectedIndexChanged
        TextBox1.Text = ddldoj1.SelectedValue + "-" + ddldoj2.SelectedValue + "-" + ddldoj3.SelectedValue
    End Sub
Here's the output. 12th-August-1981
Cani store this date in the database in the same format or need to change it. to something like 12/08/1981 dd/mm/yyyyy
0
 
LVL 3

Assisted Solution

by:RobbP
RobbP earned 800 total points
ID: 24273964
If you want to store it like that you might want to change the field in the DB to string. Preferably store as a datetime() type:

http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=181

Also as a note - have a look at cascading dropdowns in AJAX so that you can populate the months with the correct days, leap years etc
0
 

Author Comment

by:preethamonline
ID: 24274175
I'm using Visual web developer 2008. Do i need to download the AJAX toolkit coz i dont see this conrol out there.
0
 
LVL 3

Expert Comment

by:RobbP
ID: 24274219
Yeah - have a look at it here:
http://www.asp.net/ajax/
0
 
LVL 9

Accepted Solution

by:
AsishRaj earned 1200 total points
ID: 24274238
RobbP

Sorry for the interuption.

i think he is after this

Dim QueryDate As String = DDL_Month.SelectedItem.ToString & "/" & DDL_Day.SelectedItem.ToString & "/" & DDL_Year.SelectedItem.ToString

And use this when inserting it in the DB
Convert.ToDateTime(QueryDate)

When retrieving from the DB use this for that field
CONVERT(VARCHAR(11),FeildName,106) AS FeildName
0
 
LVL 9

Expert Comment

by:AsishRaj
ID: 24274252
preethamonline:

It would be advisable if you could say which method you want to approach then we can unite our expertise and help you
0
 
LVL 3

Expert Comment

by:RobbP
ID: 24274302
Agreed - theres a million ways to complete this one, which are you more comfortable with preethamonline?

Datetime conversion and formatting between presentation and data layers can become very tricky unless you decide on your standard approach from the outset.
0
 

Author Comment

by:preethamonline
ID: 24274366
Sorry for the delay guys! I was just reading about AJAX toolkit. Well Ashish raj, i was definitely thnking the way are right now. Thats what i wanted. Extract the dates from the ddl and store it in the database.
0
 
LVL 9

Expert Comment

by:AsishRaj
ID: 24274381
ok let us know the result after you have tested my code
0
 
LVL 3

Expert Comment

by:RobbP
ID: 24274449
Cool - as AsishRaj suggested:

Eg
DateTime date = new DateTime(2005, 3, 2);

0
 

Author Comment

by:preethamonline
ID: 24274465
okies. just give me 15 mins.
0
 

Author Comment

by:preethamonline
ID: 24274566
Thanks asishraj. Works perfectly. I have a question now. Should I save the date only or should I also save the values of the ddl to the database?

The reason Im asking this is because there might be a scenario later when my boss asks me filter records based on date. If he insists that I use ddl's then I would be stuck right? Or should I convince him to use a calendar control for filtering records and use the ddl for entering data.

Thanks in advance
0
 
LVL 3

Expert Comment

by:RobbP
ID: 24274664
Best practice is always save the date to the DB. You dont want to take up additional space than what is required. When you retrieve the date from the DB then you can do all the necessary formatting, filtering and functionality on the date object - there are also date specific filtering functions you can use
0
 
LVL 9

Expert Comment

by:AsishRaj
ID: 24274678
Another way around is, you store it as Date but when you retireve it, you can display it in the textbox instead of DDL - that y i have given you  -> CONVERT(VARCHAR(11),FeildName,106) AS FeildName
0
 

Author Comment

by:preethamonline
ID: 24274690
Thanks RobbP. I guess i would go with just the date. Lets see it later when it comes. Thanks guys for all your help.
0
 
LVL 3

Expert Comment

by:RobbP
ID: 24274707
One last thing - seriously preethamonline, look into the calendar extender control of AJAX, it will do all the hard work for you that youre doing now. Best of luck. Cheers
0
 

Author Comment

by:preethamonline
ID: 24274728
I did have a look. Its simply suberb. Thanks for introducing me to the AJAX toolkit.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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 video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

621 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