Error: Converting the nvarchar data type to smalldatetime data type resulted in a value out of range

Hi everyone, I am trying to execute the following insert request

SqlDataSource userQuizDataSource = new SqlDataSource();
userQuizDataSource.ConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=quizApp;Integrated Security=True";
userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([DateTimeComplete], [Score], [UserName]) VALUES (@DateTimeComplete, @Score, @UserName)";

userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
userQuizDataSource.InsertParameters.Add("Score", score.ToString());
userQuizDataSource.InsertParameters.Add("UserName", User.Identity.Name);

int rowsAffected = userQuizDataSource.Insert();

Boutes continue to receive the following error:

Converting an nvarchar data type to a smalldatetime data type in a value out of range. Application completed.

Can anyone help me out?

+5
source share
5 answers

What returns your statement DateTime.Now.ToString()?

What language and regional settings do you expect from your SQL Server?

??? , .NET MM/dd/yyyy, SQL Server dd/MM/yyyy ( ).

SQL Server:

DECLARE @test TABLE (smalldate SMALLDATETIME)
INSERT INTO @test VALUES ('02/21/2010 22:00:32') --
SELECT * FROM @test

, .NET DateTime.Now.ToString() - ? SQL Server ?

ISO-8601 (YYYYMMDD) - SQL Server - ?

DECLARE @test TABLE (smalldate SMALLDATETIME)
INSERT INTO @test VALUES ('20100221 22:00:32') --
SELECT * FROM @test
+3

datetime.now SQL Server 'Date', datatype SmallDateTime.

, ( !)

string currentdatetime=
DateTime.Now.Year + "." + DateTime.Now.Month + "." + DateTime.Now.Day +
               " " + DateTime.Now.Hour+(":")+DateTime.Now.Minute+(":")+DateTime.Now.Second

,

+1

:

userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());

:

userQuizDataSource.InsertParameters.Add("@startdate", SqlDbType.DateTime, DateTime.Now.ToString());
0

:

userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now);

: :

userQuizDataSource.InsertParameters.Add("DateTimeComplete", TypeCode.DateTime, DateTime.Now.ToString());

, .. .

0

In Windows 8, if you encounter this problem even after changing Formatsin the location below

Control Panel → Region

You still have to pass these settings on to your users. In the same window, go to the "Administrative" tab, click "Copy Settings".

Select the appropriate check boxes and click OK.

0
source

All Articles