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

Posted on 2006-06-05
Last Modified: 2008-02-01
I'm receiving the following error when trying to do an update back to a database:
Cast from string "" 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 "" to type 'Date' is not valid.

Source Error:

Line 606:        cmdHomeCompliance.Parameters.Add("@DiaryDone", SqlDbType.Int, 4).Value = HomeCompliance.DiaryDone
Line 607:        cmdHomeCompliance.Parameters.Add("@PT", SqlDbType.Int, 4).Value = HomeCompliance.PT
**Line 608:        cmdHomeCompliance.Parameters.Add("@PTDCDate", SqlDbType.DateTime, 8).Value = CDate(HomeCompliance.PTDCDate)
Line 609:        cmdHomeCompliance.Parameters.Add("@OT", SqlDbType.Int, 4).Value = HomeCompliance.OT
Line 610:        cmdHomeCompliance.Parameters.Add("@OTDCDate", SqlDbType.DateTime, 8).Value = CDate(HomeCompliance.OTDCDate)
Source File: j:\inetpub\wwwroot\db\chae01\HomeComplianceDB.vb    Line: 608

HomeCompliance.PTDCDate is a string, but I need to store it back in the database as a date.  Sometimes it will not be filled in, and will be "" instead.  I'm not sure how to handle that situation in this section of the code.

Any suggestions on how I can fix this?  Thanks!
Question by:sah18
    LVL 9

    Assisted Solution

    Hi, you have to validate your data:
    If HomeCompliance.PTDCDate="" Then
    ' use a default datetime for example: DateTime.Now
    cmdHomeCompliance.Parameters.Add("@PTDCDate", SqlDbType.DateTime, 8).Value = DateTime.Now
    cmdHomeCompliance.Parameters.Add("@PTDCDate", SqlDbType.DateTime, 8).Value = CDate(HomeCompliance.PTDCDate)
    LVL 2

    Accepted Solution


    suppose u got form with one text box which takes date

    if user try to enter empty text to it, database will throw cast error
    to prevent this check value of text if it is null then set value to nothing

    for example

    if textbox.text <> "" then
         yourcolumn.value = Convert.ToDateTime(textbox.text)
         yourcolumn.value = nothing
    end if

    hope this will help you

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now