Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to insert the in-time of a user into sql server db but the thing is when i click on in time button it is throwing following error..

Error: The conversion of a nvarchar data type to a smalldatetime data type resulted in an out-of-range value

and my c# code is follows..

C#
string sql1 = "insert into Attendance(UserId,InTime,Dated) values({0},{1},{2});" + " SELECT SCOPE_IDENTITY();";
                    result = (int)db.Database.SqlQuery<decimal>(sql1, new object[] { UserId, DateTime.Now.ToString("HH:MM:MM"), DateTime.Now.ToString("d") }).FirstOrDefault();
Posted
Updated 19-Jun-13 21:56pm
v2

Maybe you must delete ToLongTimeString() in
XML
DateTime.Now.ToLongTimeString() 
into
XML
DateTime.Now()


because you data type column inTime is smalldatetime, you can't use ToLongTimeString()
 
Share this answer
 
Comments
[no name] 20-Jun-13 3:56am    
iam passing time as DateTime.Now.ToString("HH:MM:SS")
now my data column type is datetime
Why????

Why are you issuing this kind of statement directly in Entity Framework? Is there no entity in the model you can address? Than why are you using EF on the first hand?

But if you stick to this approach, have a look here: http://www.inforbiro.com/blog-eng/entity-framework-how-to-use-datetime-in-esql-query/[^]

You have an other option though: if you want to execute something outside the context, you can extract the ADO connection string from it, and use regular ADO approach for that. Have a look here: http://stackoverflow.com/questions/13003797/get-the-entity-framework-connection-string[^]
 
Share this answer
 
Comments
[no name] 20-Jun-13 4:08am    
in our project we are using ado.net enity framework
Zoltán Zörgő 20-Jun-13 4:10am    
I figured it out, but why are you issuing this insert statement as raw sql - why not with EF via the entity model? Please read the linked pages also!
[no name] 20-Jun-13 4:21am    
what is the problem with datetime..
Zoltán Zörgő 20-Jun-13 4:23am    
Nothing in general. Read first link!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900