0

I have a problem using the calender control .. I am getting the date in a textbox... and I have to insert this into the database .. using asp.net with c#

In my web application the field property is set to datetime and in the table the column's datatype is date..

How can I do this??

The error I am getting is:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.

Could anyone help me in this regard?

Thanks in advance

4
  • You might want to include some info about database schema and the C# you are using to update the database. However it sounds like you are not using the right column types in your database. Commented Jul 31, 2012 at 5:52
  • I usually don't work with asp.net web forms, so I need to know: Are you inserting/updating the date yourself, or should the control do that? The issue is probably, as always with dates, regional settings, the format of the date. Commented Jul 31, 2012 at 5:56
  • thanks Layoric for your prompt reply actually i have two fileds in my database (sqlserver2008r2) table with the datatype of date now from application(.NET 4) side i want to insert some values in that table but in asp.net C# there is not date datatype i have to use datetime datetype but i am getting this error..i dn't know how to solve this? Commented Jul 31, 2012 at 6:00
  • @SteenT the calendar control is inserting date into textbox in my fronend at the backend which i am inserting into table it is not happenning because in my table i have datatype of date and on otherhand in asp.net webforms without datetime there is not alone date datatype Commented Jul 31, 2012 at 6:03

4 Answers 4

3

The error i am getting is: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

It sounds like you're trying to insert it as a string, which is a generally bad idea. You should use parameterized SQL and set the value as a parameter, still as a .NET DateTime. See the docs for SqlCommand.Parameters for an example of parameterized SQL. You should always keep data in its natural type for as long as possible.

Of course, it's then still possible that you'll get this error if you try to insert a DateTime value which is out of the range that SQL can store. In particular, I believe SQL has a lower limit of 1753 as the year. If your value is DateTime.MinValue for some reason (January 1st, 1AD) then you'd still get this problem. Have you added diagnostics for the value you're trying to insert?

Sign up to request clarification or add additional context in comments.

1 Comment

I've had issues with DateTime like this as well - my main answer is listed as well. But I've had issues with both, one problem I had is that DateTime wasn't big enough to handle a FileTime. So SQL 2008 R2 introduced DateTime2 in SQL. This fixed my problem (a shallow fix, but it worked); alas, always use parameters if possible like @jon-skeet says.
0

Sorry, my first answer missed your question. Try adding the parameter by doing using the standard parameter system.

command.CommandText = "INSERT INTO FooTable (FooDate) VALUES (@FooDate)"; command.Parameters.AddWithValue("@FooDate", DateToUse.Date);

Using the .Date will only return the date part of the object. Here's another reference to it Date vs DateTime

Comments

0

If you're using a parametrized query, I have no trouble whatsoever to insert that DateTime from the ASP.NET calendar control into a SQL Server database table that contains a column of type DATE.

Use something like this:

// define INSERT statement - of course, yours will look quite different!
string insertStmt = "INSERT INTO dbo.DateTest(TheDate) VALUES(@DateValue);";

// set up connection and SqlCommand to do insert
using(SqlConnection conn = new SqlConnection("....your-connection-string-here...."))
using (SqlCommand cmd = new SqlCommand(insertStmt, conn))
{
     // add the parameters - the @DateValue - to the SqlCommand object and
     // define it's datatype (on the database) and set the value to be stored
     cmd.Parameters.Add("@DateValue", SqlDbType.Date).Value = Calendar1.SelectedDate;

     // open connection, execute command, close connection
     conn.Open();
     cmd.ExecuteNonQuery();
     conn.Close();
}

Comments

-1

Try one thing first, try to insert 01/01/2012 in the column, because that could because of wrong culture.... it could be mm/dd/yyyy or dd/mm/yyyy

try this 01/01/2012 first and see if thats working.

thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.