Results 1 to 3 of 3

Thread: [RESOLVED] DateTime DataColumn

  1. #1

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Resolved [RESOLVED] DateTime DataColumn

    I'm adding a DateTime DataColumn to a DataTable like this:

    Code:
    Dim colDate As DataColumn = New DataColumn("Date")
                colDate.DataType = System.Type.GetType("System.DateTime")
                dt.Columns.Add(colDate)

    Then when I try to add it to my datatable, if the value is Nothing I get an error saying string was not recognized as a valid DateTime. I'm having trouble getting this to work. What would you do?

    Thanks

  2. #2

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Re: DateTime DataColumn

    Actually it was pretty easy. If there's an empty string, just set it to Nothing:

    If (LineItems(3).ToString().Length = 0) Then
    LineItems(3) = Nothing
    End If

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] DateTime DataColumn

    First up, do it the easy way:
    vb.net Code:
    1. dt.Columns.Add("Date", GetType(Date))
    As for this error you mention, if it's telling you anything to do with Strings then you're doing it wrong because you should only be putting Date values or DBNull.Value into a Date column. Strings should never come into play at all. You're going to have to show us how you're putting the data into the column so that we can see exactly what you're doing wrong.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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