简体   繁体   中英

How to insert datetime property from asp.net c# to a date column in sql server?

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

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?

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

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();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM