Notices

Great community. Great ideas.

Welcome to ASP/Free, a community dedicated to helping beginners and professionals alike in improving their knowledge of Microsoft's development and administration technologies. Sign up today to gain access to the combined insight of tens of thousands of members.

Thread: Display Date of Birth using 3 drop downs

Results 1 to 5 of 5
Share This Thread →
  1. #1
    zeetec1 is offline Contributing User
    Join Date
    Oct 2007
    Posts
    291
    Rep Power
    9

    Display Date of Birth using 3 drop downs

    Hi,

    I have 3 drop downs to represent date of birth.

    The date of birth is entered into a datetime field called DOB using an Insert statment.

    But I now want to retrieve this date of birth from my table and then display the Day part of the date of birth in my Day drop down list, the month part of it in my month drop down and the year part of it in my year drop down.

    So I need to split the DOB value from my table inorder to display the day, month and year in separate drop down lists.

    In classic asp I could use this code:-

    Code:
    <%
    'Split current employer start date into 3 numbers for day, month and year
    if objRS("intdatez") <> "" then mancurdmy = Split(objRS("intdatez"), "/")
    %>
    
    <nobr>
    
    
    
    <tr><td width="175">Date:&nbsp;</td>
      <td align="center" width="193">
    
    <SELECT name="FirstSelectDay" size="1">
    <OPTION SELECTED VALUE="">
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(0) = 1 then response.write "Selected"%> VALUE=1>1
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(0) = 2 then response.write "Selected"%> VALUE=2>2
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(0) = 3 then response.write "Selected"%> VALUE=3>3
    </SELECT>
    
    <SELECT name="FirstSelectMonth">
    <OPTION SELECTED VALUE="">
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(1) = 1 then response.write "Selected"%> VALUE=1>1
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(1) = 2 then response.write "Selected"%> VALUE=2>2
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(1) = 3 then response.write "Selected"%> VALUE=3>3
    </SELECT>
    
    <SELECT name="FirstSelectYear">
    <OPTION SELECTED VALUE="">
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(2) = 2007 then response.write "Selected"%> VALUE=2007>2007
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(2) = 2008 then response.write "Selected"%> VALUE=2008>2008
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(2) = 2009 then response.write "Selected"%> VALUE=2009>2009
    </SELECT>
    </nobr>
    How can I do this in my .net form?

    Thanks.

  2. #2
    rclark's Avatar
    rclark is offline I do .NET for a living
    Join Date
    Sep 2003
    Location
    Florida
    Posts
    1,549
    Rep Power
    210
    Quote Originally Posted by zeetec1
    Hi,

    I have 3 drop downs to represent date of birth.

    The date of birth is entered into a datetime field called DOB using an Insert statment.

    But I now want to retrieve this date of birth from my table and then display the Day part of the date of birth in my Day drop down list, the month part of it in my month drop down and the year part of it in my year drop down.

    So I need to split the DOB value from my table inorder to display the day, month and year in separate drop down lists.

    In classic asp I could use this code:-

    Code:
    <%
    'Split current employer start date into 3 numbers for day, month and year
    if objRS("intdatez") <> "" then mancurdmy = Split(objRS("intdatez"), "/")
    %>
    
    <nobr>
    
    
    
    <tr><td width="175">Date:&nbsp;</td>
      <td align="center" width="193">
    
    <SELECT name="FirstSelectDay" size="1">
    <OPTION SELECTED VALUE="">
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(0) = 1 then response.write "Selected"%> VALUE=1>1
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(0) = 2 then response.write "Selected"%> VALUE=2>2
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(0) = 3 then response.write "Selected"%> VALUE=3>3
    </SELECT>
    
    <SELECT name="FirstSelectMonth">
    <OPTION SELECTED VALUE="">
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(1) = 1 then response.write "Selected"%> VALUE=1>1
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(1) = 2 then response.write "Selected"%> VALUE=2>2
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(1) = 3 then response.write "Selected"%> VALUE=3>3
    </SELECT>
    
    <SELECT name="FirstSelectYear">
    <OPTION SELECTED VALUE="">
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(2) = 2007 then response.write "Selected"%> VALUE=2007>2007
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(2) = 2008 then response.write "Selected"%> VALUE=2008>2008
    <OPTION <%if objRS("intdatez") <> "" then if mancurdmy(2) = 2009 then response.write "Selected"%> VALUE=2009>2009
    </SELECT>
    </nobr>
    How can I do this in my .net form?

    Thanks.
    If the date is in a datetime variable, then you can extract the pieces like this:

    Dim iMonth as Integer = myDate.Month
    Dim iDay as Integer = myDate.Day
    Dim iYear as integer = myDate.Year

    Then use the individual integers to set the selected item in your dropdown boxes.
    Roger (.NET MCP)

  3. #3
    zeetec1 is offline Contributing User
    Join Date
    Oct 2007
    Posts
    291
    Rep Power
    9
    Quote Originally Posted by rclark
    If the date is in a datetime variable, then you can extract the pieces like this:

    Dim iMonth as Integer = myDate.Month
    Dim iDay as Integer = myDate.Day
    Dim iYear as integer = myDate.Year

    Then use the individual integers to set the selected item in your dropdown boxes.
    Ok I'll give it a try, thanks.

  4. #4
    zeetec1 is offline Contributing User
    Join Date
    Oct 2007
    Posts
    291
    Rep Power
    9
    Quote Originally Posted by rclark
    If the date is in a datetime variable, then you can extract the pieces like this:

    Dim iMonth as Integer = myDate.Month
    Dim iDay as Integer = myDate.Day
    Dim iYear as integer = myDate.Year

    Then use the individual integers to set the selected item in your dropdown boxes.
    The 'myDate' that you've used is that my DOB field from my db?

    I'm not quite sure how to fit this code into my code.

    My code currently looks like this:-



    Code:
      int iYear = year.SelectedItem.Value;
                
    
                DateTime DateOfBirth = new DateTime(Convert.ToInt32(ddYear.SelectedItem.Text), Convert.ToInt32(ddMonth.SelectedItem.Text), Convert.ToInt32(ddDay.SelectedItem.Text));
    
                SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["log_sheetsConnectionString"].ToString());
                SqlCommand cmd = cnn.CreateCommand();
                cmd.CommandType = CommandType.Text;
    
                cmd.CommandText = "select * from admin_users where DOB.Year = '" + iYear +"'";
    
                cnn.Open();
    
                cmd.ExecuteNonQuery();
    
                cnn.Close();
                cnn.Dispose();
                cmd.Dispose();

    'year' is the name of my year drop down list.

    It's coming up with cannot implicitly convert type string to int error.

    Pointing to my declaration line:-

    Code:
    int iYear = year.SelectedItem.Value;

  5. #5
    rclark's Avatar
    rclark is offline I do .NET for a living
    Join Date
    Sep 2003
    Location
    Florida
    Posts
    1,549
    Rep Power
    210
    Quote Originally Posted by zeetec1
    The 'myDate' that you've used is that my DOB field from my db?

    I'm not quite sure how to fit this code into my code.

    My code currently looks like this:-



    Code:
      int iYear = year.SelectedItem.Value;
                
    
                DateTime DateOfBirth = new DateTime(Convert.ToInt32(ddYear.SelectedItem.Text), Convert.ToInt32(ddMonth.SelectedItem.Text), Convert.ToInt32(ddDay.SelectedItem.Text));
    
                SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["log_sheetsConnectionString"].ToString());
                SqlCommand cmd = cnn.CreateCommand();
                cmd.CommandType = CommandType.Text;
    
                cmd.CommandText = "select * from admin_users where DOB.Year = '" + iYear +"'";
    
                cnn.Open();
    
                cmd.ExecuteNonQuery();
    
                cnn.Close();
                cnn.Dispose();
                cmd.Dispose();

    'year' is the name of my year drop down list.

    It's coming up with cannot implicitly convert type string to int error.

    Pointing to my declaration line:-

    Code:
    int iYear = year.SelectedItem.Value;
    The ddl value is returning a string. If you're sure the value will be an integer, then just cast it as one:

    Code:
    int iYear = (int) year.SelectedItem.Value;

Share This Thread →

Become Part of This Conversation

Join NowFor Free!

Similar Threads

  1. Drop Down Menu Text Display
    By Cpajoe2001 in forum .NET Development
    Replies: 2
    Last Post: November 29th, 2006, 01:45 PM
  2. Modifying Querystring Information with Drop Downs
    By frostbitten in forum ASP Development
    Replies: 2
    Last Post: May 30th, 2006, 08:16 AM
  3. Two linked Dynamic Drop Downs
    By vijaysingh in forum ASP Development
    Replies: 5
    Last Post: February 20th, 2006, 06:00 AM
  4. Drop downs
    By princecharles in forum ASP Development
    Replies: 5
    Last Post: December 7th, 2005, 12:15 AM
  5. [B]multiple drop downs[/B]
    By arun251 in forum ASP Development
    Replies: 7
    Last Post: July 14th, 2005, 03:49 AM

ASP Free Advertisers and Affiliates