Results 1 to 4 of 4

Thread: Error: String was not recognized as a valid DateTime

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    24

    Error: String was not recognized as a valid DateTime

    Hi,

    I have an asp.net web application,

    I retrieve a stored session value from first page in 2nd page:

    Dim AppFromDate1 As Object
    If Session("FromDate1") Is DBNull.Value Then
    AppFromDate1 = String.Empty
    Else
    AppFromDate1 = (Session("FromDate1"))
    End If

    When it's null string "", the run time value is ""{String}.

    For inserting to the database, I set the parameter value as:

    dbDetail.Parameters.Add(New SqlParameter("@AppFromDate1", SqlDbType.DateTime))

    and I manage the value as :

    Dim sqldatenull As SqlDateTime
    sqldatenull = SqlDateTime.Null
    If (AppFromDate1 = "") Then
    dbDetail.Parameters("@AppFromDate1").Value = sqldatenull
    Else
    dbDetail.Parameters("@AppFromDate1").Value = DateTime.Parse(AppFromDate1)
    End If


    HOWEVER, dbDetail.Parameters("@AppFromDate1").Value = sqldatenull doesn't work, it still put the value to ""{string}, and the insert failed for an error:

    "String was not recognized as a valid DateTime".

    Please help!

  2. #2
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: Error: String was not recognized as a valid DateTime

    Instead of setting your parameter value to SqlDateTime.Null, set it instead to System.DBNull.Value.
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    24

    Re: Error: String was not recognized as a valid DateTime

    Quote Originally Posted by simonm
    Instead of setting your parameter value to SqlDateTime.Null, set it instead to System.DBNull.Value.
    I still got error after I've changed my code to :

    Dim sqldatenull As Object
    sqldatenull = DBNull.Value
    If (AppFromDate1 = "") Then
    dbDetail.Parameters("@AppFromDate1").Value = sqldatenull
    Else
    dbDetail.Parameters("@AppFromDate1").Value = DateTime.Parse(AppFromDate1)
    End If

    I got error when it's inserting the value to database:
    "String was not recognized as a valid DateTime"

    Any suggestions?

  4. #4
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: Error: String was not recognized as a valid DateTime

    This is what I mean:
    VB Code:
    1. dbDetail.Parameters("@AppFromDate1").Value = System.DBNull.Value
    And I know this works fine for passing in NULL to SQL Server stored procedures because I have tested it here.

    Presumably, the column in the table that you are inserting into allows nulls?
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width