Solved

using a split function

Posted on 2008-10-17
16
246 Views
Last Modified: 2012-05-05
Hello Experts,
I dont know If I am doing something wrong here, please can you help.

All I wanted to do is to split the date by day/month/year and assign the values
back to corresponding dropdownlist controls..

please advise

thanks in advance
regards
kay
RegDetails.DateOfBirth="10/02/1978"
 
  If RegDetails.DateOfBirth <> "" Then
                Dim DateBirth, DayBirth, MonthBirth, YearBirth As String
 
                DateBirth = RegDetails.DateOfBirth.Split("/").ToString
 
                DayBirth = DateBirth(0).ToString
                MonthBirth = DateBirth(1).ToString
                YearBirth = DateBirth(2).ToString
 
                lstDayOfYourBirth.Text = DayBirth
                lstMonthOfYourBirth.Text = MonthBirth
	        lstYearOfYourBirth.Text = YearBirth
  End If

Open in new window

0
Comment
Question by:learningnet
  • 6
  • 3
  • 2
  • +3
16 Comments
 
LVL 25

Accepted Solution

by:
SStory earned 100 total points
ID: 22740220
I wonder if your DataOfBirth is declared as a string or DateTime?
I am thinking if it is a datetime, then you might need to do:
RegDetails.DateOfBirth.ToString.Split("/")

Also this returns an array of strings not a string

dim Parts() as string=RegDetails.DateOfBirth.ToString.Split("/")

should work
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 100 total points
ID: 22740233
Try this:

Dim DateBirth() as string
dim DayBirth, MonthBirth, YearBirth As String
 
                DateBirth = RegDetails.DateOfBirth.Split("/")
 
                DayBirth = DateBirth(0)
                MonthBirth = DateBirth(1)
                YearBirth = DateBirth(2)
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 100 total points
ID: 22740253
Whether String or DateTime, it may be easier just to convert to DateTime and use the built in functionality to get Month, Day, Year, Minute, Seconds, etc. all separate without having to depend on splitting by the "/".  This will allow your code to work even if someone enters date as "20081017" OR "10-17-2008" without you having to add extra code.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 21

Assisted Solution

by:silemone
silemone earned 100 total points
ID: 22740268
well i suggest instead of using split, change date to

Dim objDT as DateTime  = Convert.ToDateTime(RegDetails.DateOfBirth)
DayBirth = objDT.Day
MonthBirth = objDT.Month
YearBirth = objDT.Year

objDT.Day returns an int, so if you need it as a string, then DayBirth = objDT.Day + ""
0
 
LVL 21

Expert Comment

by:silemone
ID: 22740294
just to make clear;  all objDT.Day, objDT.Month, and objDT.Year returns ints so if you're using string just at +""
or toString()
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22740312
Best practice with dates from Microsoft (interesting reading):
http://msdn.microsoft.com/en-us/library/ms973825.aspx#datetime_topic7

DateTime class members:
http://msdn.microsoft.com/en-us/library/system.datetime_members.aspx

You can use the Month() and other functions, but as you will see above you can just call named members of the DateTime structure to get same results.

Regards,
Kevin
0
 
LVL 21

Expert Comment

by:silemone
ID: 22740319
RegDetails.DateOfBirth="10/02/1978"
 
  If RegDetails.DateOfBirth <> "" Then

                Dim objDT as DateTiem = Convert.ToDateTime(RegDetails.DateOfBirth)

               


                lstDayOfYourBirth.Text = objDT.Day.ToString()
                lstMonthOfYourBirth.Text = objDT.Month.ToString()
                lstYearOfYourBirth.Text = objDT.Year.ToString()
  End If
0
 
LVL 6

Expert Comment

by:openshac
ID: 22740321
Cast the value as a DateTime and then use Day, Month and Year properties.
0
 
LVL 21

Expert Comment

by:silemone
ID: 22740336
Fixed typo..

RegDetails.DateOfBirth="10/02/1978"
 
  If RegDetails.DateOfBirth <> "" Then

                Dim objDT as DateTime = Convert.ToDateTime(RegDetails.DateOfBirth)

                lstDayOfYourBirth.Text = objDT.Day.ToString()
                lstMonthOfYourBirth.Text = objDT.Month.ToString()
                lstYearOfYourBirth.Text = objDT.Year.ToString()
  End If
0
 
LVL 21

Expert Comment

by:silemone
ID: 22740373
if you want month and day to be preceeded by 0,   i.e.  if 1,  then 01

RegDetails.DateOfBirth="10/02/1978"
 
  If RegDetails.DateOfBirth <> "" Then
                Dim objDT as DateTime = Convert.ToDateTime(RegDetails.DateOfBirth)
                if  objDT.Day < 10 Then
                     lstDayOfYourBirth.Text = "0" + objDT.Day.ToString()
                else
                     lstDayOfYourBirth.Text = objDT.Day.ToString()
                End If
                 if  objDT.Day < 10 Then
                    lstMonthOfYourBirth.Text = "0" + objDT.Month.ToString()
                 else
                    lstMonthOfYourBirth.Text = objDT.Month.ToString()
                End If
                lstYearOfYourBirth.Text = objDT.Year.ToString()
  End If

RegDetails.DateOfBirth="10/02/1978"
 
  If RegDetails.DateOfBirth <> "" Then

                Dim objDT as DateTime = Convert.ToDateTime(RegDetails.DateOfBirth)

                lstDayOfYourBirth.Text = objDT.Day.ToString()
                lstMonthOfYourBirth.Text = objDT.Month.ToString()
                lstYearOfYourBirth.Text = objDT.Year.ToString()
  End If


0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22740399
You can also use the:
objDT.ToString().PadLeft(2, "0")
0
 

Author Comment

by:learningnet
ID: 22741274
Thanks million for the input folks..

Let me try to put in a precise way , I have the following on the page

 <asp:DropDownList ID="lstDayOfYourBirth" runat="server">                                                                  
                                                                   </asp:DropDownList>
                                                                   <asp:DropDownList ID="lstMonthOfYourBirth" runat="server">                                                                                                                                        
                                                                   </asp:DropDownList>
                                                                   <asp:DropDownList ID="lstYearOfYourBirth" runat="server">                                                                  
                                                                   </asp:DropDownList>    
And I am filling them using the following for the first time the user visits the page

             WebFunctions.FillDropDownListWithDate(lstYearOfYourBirth, 1890, Year(Now))
                    WebFunctions.FillDropDownListWithMonth(lstMonthOfYourBirth)
                    WebFunctions.FillDropDownListWithDate(lstDayOfYourBirth, 1, 31)

And using this....

 Public Shared Sub FillDropDownListWithDate(ByVal lst As DropDownList, ByVal first As Integer, ByVal last As Integer)
        Dim i As Integer

        'lst.Items.Clear()
        lst.Items.Add(New ListItem("-", "0"))
        For i = first To last
            lst.Items.Add(New ListItem(i, i))
        Next
    End Sub


Public Shared Sub FillDropDownListWithMonth(ByVal lst As DropDownList)
        lst.Items.Add(New ListItem("-", "0"))
        For i As Integer = 1 To 12
            lst.Items.Add(New ListItem(MonthName(i), i))
        Next
    End Sub

This all works fine ...

however, when they come to page to view and change details I wanted to load the dropdown list with the values they
chosesn the first time

             Dim DateBirth() As String
                Dim DayBirth, MonthBirth, YearBirth As String

                DateBirth = RegDetails.DateOfBirth.Split("/")

                DayBirth = DateBirth(0)
                MonthBirth = DateBirth(1)
                YearBirth = DateBirth(2)

                lstDayOfYourBirth.Text = DayBirth
                lstMonthOfYourBirth.Text = MonthBirth
                lstYearOfYourBirth.Text = YearBirth

emoreau', suggested code seems to have pulled the data correct , however, the FillDropDownListWithDate is over writing it with "-"s

I hope you see what I am trying to do here?

I am also dealing with initial zeroer in the beginning

 Public Shared Function FormatDateWithZeros(ByVal d As Date) As String
        Return Right("0" & d.Day, 2) & "/" & Right("0" & d.Month, 2) & "/" & d.Year
 End Function

To sum all, when I wanted to assing newly added value back to the drop down I would rather do not display the initial "0"?

Thanks again for all your help folks..

0
 
LVL 6

Assisted Solution

by:openshac
openshac earned 100 total points
ID: 22741460
Use string.Format("{0:dd/MM/yyyy}", myDate) instead of the function FormatDateWithZeros.

To get the number without a zero just use string.Format("{0:#}"), myNumberwithaZero)
0
 
LVL 21

Expert Comment

by:silemone
ID: 22742352
have you tested addin my code in...one code I offered places 0's in front, the other will not...
But it does use DateTime...way less code...I read your post and still not understanding why you can't use DateTime unless specs say you must split...
0
 
LVL 25

Expert Comment

by:SStory
ID: 22744980
seems to me you would convert it to a datetime, then use the
.Month
.Day
.Year
properties of that variable to set them in your combo boxes.

Example... a dropdown is indexed by 0 so

dim dtVar as datetime="10-17-2008"  'initialize however
dtVar.Month-1 will give you the right index for the dropdown for month

dtVar.Day-1 will do the same for day

for the year you will need to search the drop down for the matching year.

HTH,

Shane
0
 

Author Comment

by:learningnet
ID: 22745986
silemone:

thanks for the code,  i have tried adding your script, it worked too...

i think i need to handle this situation in a different way, the new values are not been displayed as it is loading all the time from the Sub routines replacing what  has been captured ....

thanks ALL !!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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