problem on date,month year concatenation

asp.net Mauritius
  • 14 years ago

    hi experts,

    actually i run this piece of code and obtained this error.I have 3 drop down control , one for date , one for month and the last is for year and a button to check which will display the result in the label. But i'm getting the following error. Please help as the deadline for my project is becoming near. Please help....

    IPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim strdob As Date

    Dim strday As String

    Dim strmon As String

    Dim stryear As String

    strday = dayddl.SelectedValue

    strmon = mmddl.SelectedValue

    stryear = yearddl.SelectedValue

    strdob = strday & strmon & stryear

    Label1.Text=Convert.ToDateTime(strdob)

     

    End Sub

    Error obtained:

     

    Cast from string "01011980" to type 'Date' is not valid.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details:

    System.InvalidCastException: Cast from string "01011980" to type 'Date' is not valid.

    Source Error:

     
    Line 40:         stryear = yearddl.SelectedValue
    Line 41: 
    Line 42:         strdob = strday & strmon & stryear
    Line 43:         Convert.ToDateTime(strdob)
    Line 44:         Label1.Text = strdob


    Source File:
    c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.vb    Line: 42

    Stack Trace:

     
    [InvalidCastException: Cast from string "01011980" to type 'Date' is not valid.]
       Microsoft.VisualBasic.CompilerServices.DateType.FromString(String Value, CultureInfo culture) +163
       Microsoft.VisualBasic.CompilerServices.DateType.FromString(String Value) +53
       WebApplication1.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.vb:42
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain() +1277

  • 14 years ago

    Basically what you are trying to do is assign the Text property of the label to a DateTime value which is not supported. You should assign a string value to the Text property isn't it?

  • 14 years ago

    Hello Vinay,

    You are assigning String value to DateTime variable.

    strdob is DateTime Variable and the concatinated value is string. So, this error is obvious.

    Now, First of all, Concatinate Day,Month and Year into one string variable.

    for eg.

    Dim strDate as string

    strDate= strDay & "/" & strMonth "/" & strYear

    then after

    strdob=Convert.DateTime(strDate)

    Label1.text=strdob

    This will solve your problem.

     

    Regards,

    Chital

  • 14 years ago

    Hi Friend,

    Could you just correct the code how it should be as i'm a bit novice in asp.net and I've try to work with it several times but in vain.Please correct the code or me and post it in the forum please.

  • 14 years ago
    Thanks a lots friend your code works. It will help me a lot in my project. Thanks again.
  • 14 years ago

    Anytime...

     

    Regards,

     

    Chital

  • 14 years ago

    Hi Chital,

    Sorry to bother you once again, Do you have the validation for these text box such as date month & year so that it will recognise 31 days or 30 days for a month and for february it should be 28 days or 29 ( in case of leap year or common year. If you do have it please make me have it.

    Thanks .

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates