Error converting data type nvarchar to datetime, any suggestion
-
Don't convert in the database. You should convert in C# first, because .NET handles times in a completely different way than SQL Server. SQL Server requires times input as strings to follow a specific format. If you convert in C# first, ADO.NET will take care of the rest:
string freedateraw = Gridview2.Rows[e.RowIndex].Cells[3].Text;
DateTime? freedate = (freedateraw == null ? (DateTime?)null : Convert.ToDateTime(freedateraw));cmd.Parameters.AddWithValue("@freedate", freedate);
What if freedateraw is an empty string? freedateraw == null won't work and Convert.ToDateTime(freedateraw) throw an exception. If freedateraw is not null or empty the convert may still throw an exception if it isn't in a valid format. There is no point to this cast;(DateTime?)null
only two letters away from being an asset
-
suni_dotnet wrote:
how can i validate.
:rolleyes: Maybe something like a validation control, regex, using a DateTime picker, using your brain would be the first step.
only two letters away from being an asset
hi i tried alot before posting this question. searched google (1 week)... but couldnot find any solution.. i used my brain by all means but couldnot find solution. iam a learner so does everyone. if everyone knows everything.. there wont be anything... iam here to learn... if possible plz guide me rather than ..
-
hi i tried alot before posting this question. searched google (1 week)... but couldnot find any solution.. i used my brain by all means but couldnot find solution. iam a learner so does everyone. if everyone knows everything.. there wont be anything... iam here to learn... if possible plz guide me rather than ..
In your weeks worth of searching you didn't come across anything like this :omg: :rolleyes: http://msdn.microsoft.com/en-us/library/7kh55542.aspx[^][^]
only two letters away from being an asset
-
Don't convert in the database. You should convert in C# first, because .NET handles times in a completely different way than SQL Server. SQL Server requires times input as strings to follow a specific format. If you convert in C# first, ADO.NET will take care of the rest:
string freedateraw = Gridview2.Rows[e.RowIndex].Cells[3].Text;
DateTime? freedate = (freedateraw == null ? (DateTime?)null : Convert.ToDateTime(freedateraw));cmd.Parameters.AddWithValue("@freedate", freedate);
i tried i got error, string not recognised as valid datetime. i used many formatings "mm/dd/yyyy",... same error
-
In your weeks worth of searching you didn't come across anything like this :omg: :rolleyes: http://msdn.microsoft.com/en-us/library/7kh55542.aspx[^][^]
only two letters away from being an asset
that i know, its getting into argument.. can u help me with little coding. else no problem. thank you
-
that i know, its getting into argument.. can u help me with little coding. else no problem. thank you
What help do you need?!? You have had multiple answers, its time for you to apply it.
only two letters away from being an asset
-
What if freedateraw is an empty string? freedateraw == null won't work and Convert.ToDateTime(freedateraw) throw an exception. If freedateraw is not null or empty the convert may still throw an exception if it isn't in a valid format. There is no point to this cast;(DateTime?)null
only two letters away from being an asset
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.
-
i tried i got error, string not recognised as valid datetime. i used many formatings "mm/dd/yyyy",... same error
You need to change your stored proc, too. If your stored proc is like this: CREATE PROCEDURE SomeProc ( @filldate varchar(10), @freedate varchar(10), @name varchar(50) ) Then you need to change that to: CREATE PROCEDURE SomeProc ( @filldate datetime, @freedate datetime, @name varchar(50) ) And get rid of your conversion in the stored proc. You can't convert in both places...either convert in C#, or convert in the stored proc. If you convert in the stored proc, you will have to manually reformat the freedate and filldate strings to conform to the datetime format required by SQL Server to pass them in and convert them in the proc. Converting to DateTime in C# will be simpler, and a more stable implementation, than reformatting and converting in the proc.
-
You need to change your stored proc, too. If your stored proc is like this: CREATE PROCEDURE SomeProc ( @filldate varchar(10), @freedate varchar(10), @name varchar(50) ) Then you need to change that to: CREATE PROCEDURE SomeProc ( @filldate datetime, @freedate datetime, @name varchar(50) ) And get rid of your conversion in the stored proc. You can't convert in both places...either convert in C#, or convert in the stored proc. If you convert in the stored proc, you will have to manually reformat the freedate and filldate strings to conform to the datetime format required by SQL Server to pass them in and convert them in the proc. Converting to DateTime in C# will be simpler, and a more stable implementation, than reformatting and converting in the proc.
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
-
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
-
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.
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
-
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
-
"String was not recognized as a valid DateTime." though whatever conversions i used and formats iam getting same error.
-
"String was not recognized as a valid DateTime." though whatever conversions i used and formats iam getting same error.
-
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?
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...
-
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...
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.