Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have a problem with datetime picker and database datatime datatype while updating my record. can any one suggest me that How to handle datetime picker datavalue to insert and update in Sqlserver?

Thanks and regards,
A.Harisankar
Posted
Comments
justinonday 5-Jan-11 1:13am    
look date format of Sql and .net .may be its different

For the moment, I can think 2 possible ways:
1) Use standard database date time formats (yyyy/mm/dd hh:mm:ss) (eg. 2010/12/31 23:59).
2) Use parameters with your query. Thus, you will able to assign the value of date time picker directly to the parameter.
Hope this helps.
 
Share this answer
 
Comments
Dalek Dave 5-Jan-11 3:43am    
Best Answer.
dmageiras 5-Jan-11 10:34am    
Thanks Dave.
For DateTime values you can use something like the following:
SqlParameter p1 = cmd.Parameters.Add("@SelectedDate", SqlDbType.DateTime);
p1.Value = dateTimePicker.Value;
 
Share this answer
 
Comments
Dalek Dave 5-Jan-11 3:43am    
Fair Enough.
In sql server if the Datatype is datetime, you have pass the datetime picker value in mm/dd/yyyy format. So the selected date should be mm/dd/yyyy format.. if are stil not able to get solution let me know
 
Share this answer
 
Use the following code to update the value from datetime picker control to SQL QUERY

C#
SqlCommand command = new SqlCommand();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Valid connection";
connection.Open();
command.Connection = connection;
command.CommandText = "Insert into Sample values(1, 'kkk','" + dateTimePicker1.Value + "')";
command.ExecuteNonQuery();

As mentioned in the above code value from datetime picker control can be directly used in the sql query.
 
Share this answer
 
v3
Comments
Dalek Dave 5-Jan-11 3:44am    
Edited for Code Block.

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