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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Error converting data type nvarchar to datetime, any suggestion

Error converting data type nvarchar to datetime, any suggestion

Scheduled Pinned Locked Moved C#
csharpdatabasehelp
27 Posts 5 Posters 0 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.
  • S suni_dotnet

    hi, i declared as datetime in stored procedure. yeah iam trying in C#. ALTER PROCEDURE InsertSuite @filleddate datetime, @freedate datetime, @nameofcustomer varchar(50) AS BEGIN SET NOCOUNT ON Insert into PresidentialSuite(DateWhenFilled,DateWhenFree,NameOfCustomer) values (@filleddate,@freedate,@nameofcustomer); END dont know why iam getting such error. thanks

    J Offline
    J Offline
    Jon Rista
    wrote on last edited by
    #21

    What is the error you are currently getting?

    S 1 Reply Last reply
    0
    • J Jon Rista

      Actually, there absolutely is a point to the cast. Test it for yourself: string raw = null; DateTime? dt = (String.IsNullOrEmpty(raw) ? null : Convert.ToDateTime(raw)); You will get the following compilation error: Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime' Yes, you are correct, checking for null is not sufficient, checking String.IsNullOrEmpty() is correct. That wasn't the real point of my response, though. But check that ternary statement...there absolutely is a point to that cast, its reqired to compile.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #22

      Your correct, I forgot the IDE wasn't smart enough to figure out its a nullable type and can be null


      only two letters away from being an asset

      J 1 Reply Last reply
      0
      • N Not Active

        Your correct, I forgot the IDE wasn't smart enough to figure out its a nullable type and can be null


        only two letters away from being an asset

        J Offline
        J Offline
        Jon Rista
        wrote on last edited by
        #23

        You would think the C# compiler would be intelligent enough to handle that. Maybe the co/contravariance stuff in C# 4.0 will simplify such scenarios...

        1 Reply Last reply
        0
        • J Jon Rista

          What is the error you are currently getting?

          S Offline
          S Offline
          suni_dotnet
          wrote on last edited by
          #24

          "String was not recognized as a valid DateTime." though whatever conversions i used and formats iam getting same error.

          J 1 Reply Last reply
          0
          • S suni_dotnet

            "String was not recognized as a valid DateTime." though whatever conversions i used and formats iam getting same error.

            J Offline
            J Offline
            Jon Rista
            wrote on last edited by
            #25

            Ok, it sounds like your input string is not in a known date time format. Can you paste a copy of the datetime string your inputting?

            S 1 Reply Last reply
            0
            • J Jon Rista

              Ok, it sounds like your input string is not in a known date time format. Can you paste a copy of the datetime string your inputting?

              S Offline
              S Offline
              suni_dotnet
              wrote on last edited by
              #26

              i inputed different formats 2008/02/02,02/02/2008,02/02/2008 00:00:00, by changing formatting style from / to -,2008-02-02,02-02-2008...

              J 1 Reply Last reply
              0
              • S suni_dotnet

                i inputed different formats 2008/02/02,02/02/2008,02/02/2008 00:00:00, by changing formatting style from / to -,2008-02-02,02-02-2008...

                J Offline
                J Offline
                Jon Rista
                wrote on last edited by
                #27

                As far as I can tell, all of those are valid. That would mean that something is happening to your input between the time you enter it, and the time you try to convert it. Debug your code, and look at what you are actually retrieving from your gridview, and make sure your getting the right information.

                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