Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to fetch datetime data in datetimepicker from database in C# .net?
Posted
Updated 3-Apr-12 21:23pm
v3

Try:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myDate) VALUES (@DATE)", con))
        {
        com.Parameters.AddWithValue("@DATE", myDateTimePicker.Value);
        com.ExecuteNonQuery();
        }
    }
 
Share this answer
 
Have a look:
C#
String connectionString = @"Your connection string";
SqlConnection thisConnection = new SqlConnection(connectionString);

try
{
  thisConnection.Open();

  String thisQuery = "INSERT INTO ProjectList (Date) VALUES ('"+dtpDate.Value.Date+"')";
  SqlCommand thisCommand = new SqlCommand(thisQuery, thisConnection);

  thisCommand.ExecuteNonQuery();
  thisConnection.Close();
}
catch (SqlException e)
{
  Console.WriteLine(e.Message);
}
 
Share this answer
 
v3
Comments
Member 13401463 25-Nov-23 9:11am    
The question is about to fetch data from database to datetimepicker not to save data.
datetime in database has same return value with datetimepicker.value

example :

C#
var s = (from x in ent.Flights select x).FirstOrDefault();
dateTimePicker1.Value = s.FlightDate;


this will set datetimepicker1 value with flightdate from table Flight
 
Share this answer
 
v2

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