Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Linq-Sql DateTime?

Linq-Sql DateTime?

Scheduled Pinned Locked Moved ASP.NET
databasehelpcsharplinqtutorial
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Morgs Morgan
    wrote on last edited by
    #1

    Hi guys, Iam trying to update a nullable datetime field in my database using linq-sql. What ever casting method i have tried i keep getting an error: "Specified cast is not valid." Does anyone know how to update a nullable DateTime? field using Linq-Sql? please help me. I have tried:

    myVar.dateredeemed = DateTime.Parse("2010-12-03"); //still throws the same error
    myVar.dateredeemed = (DateTime?)dateTime; //still throws the same error
    myVar.dateredeemed = DateTime.Now; //still throws the same error

    I don't really what else i can try because all of the above won't work. Please help me...

    K 1 Reply Last reply
    0
    • M Morgs Morgan

      Hi guys, Iam trying to update a nullable datetime field in my database using linq-sql. What ever casting method i have tried i keep getting an error: "Specified cast is not valid." Does anyone know how to update a nullable DateTime? field using Linq-Sql? please help me. I have tried:

      myVar.dateredeemed = DateTime.Parse("2010-12-03"); //still throws the same error
      myVar.dateredeemed = (DateTime?)dateTime; //still throws the same error
      myVar.dateredeemed = DateTime.Now; //still throws the same error

      I don't really what else i can try because all of the above won't work. Please help me...

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      None of this screams LINQ-to-SQL. Your code should work is myVar.dateredeemed is of type DateTime? if not, that is the source of your problem. This code works:

      DateTime? foo;
      foo = DateTime.Now;

      However, you should be wary of this:

      myVar.dateredeemed = DateTime.Parse("2010-12-03")

      For two reasons:

      1. If the string really comes from user input, they can enter an incorrect value you need to decide whether this should be null or whether the user *must* enter a valid date. Whichever is the correct strategy, incorrect input will throw an error.
      2. Can the user enter no date, (i.e. "")? If so you code will not result in null

      Finally, I'd say one more thing: If you have luxury of changing the database, avoid nulls (especially in databases) like the plague. You end up with problems similar to the one you have here, potential null reference exceptions, and they make coding SQL harder too. That's not to say don't ever use nulls, but you should think several times before using nullable columns. Personally, I'd use one of the SQL min/max dates in place of null to register "unknown", (a bit like replacing and unknown [null] string with "") but others will disagree. A null date (to represent a date not set yet) is one of the few places I think a null column might be pukka. </rant> - Sorry I didn't mean to go on like this :-).

      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
      -Or-A Dead ringer for Kate Winslett[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups