How to set Datetime in Datatable Datetime column

18,040

Solution 1

Try this

    public void MakeDataTable(){
    DataTable myTable;
    DataRow myNewRow; 
    // Create a new DataTable.
    myTable = new DataTable("My Table");
    //ADDING DATETIME COLUMN
    DataColumn colDateTime = new DataColumn("DateTimeCol");
    colDateTime.DataType = System.Type.GetType("System.DateTime");
    myTable.Columns.Add(colDateTime);
    //ADDING ROW TO DATA-Table
    myNewRow = myTable.NewRow();
    myNewRow["DateTimeCol"] = System.DateTime.Now;
    myTable.Rows.Add(myNewRow);
}

Solution 2

According to the error displayed, the data type of the column in the database does not match the input(if column type is datetime, AM OR PM not stored Use datetime2 Type)

Solution 3

The solution is in the error message. C# DateTime is equivalent to either SQL datetime or datetime2. Check the type in your SQL table and, if necessary, set it to either of these date types.

If you look at the definition of smalldatetime here it stores the time differently. If you need the time you will need to strip off all but the hours and minutes, otherwise use the date only. Add .Date after Now.

Solution 4

Basically, a DateTime column would be added as shown below please cross check with your code `

        DataTable dt = new DataTable();

        DataColumn dc = new DataColumn();

        dc.ColumnName = "dCurrantDate";
        dc.DataType = typeof(DateTime);
        dt.Columns.Add(dc);

        DataRow dr = dt.NewRow();

        dr["dCurrantDate"] =DateTime.Now;
        dt.Rows.Add(dr);`
Share:
18,040

Related videos on Youtube

Hardik Leuwa
Author by

Hardik Leuwa

Updated on June 04, 2022

Comments

  • Hardik Leuwa
    Hardik Leuwa almost 2 years

    I'm trying to store currant datetime in Datatable Datetime column using below code

    dtRow["dCurrantDate"] = DateTime.Now;
    

    but getting this error:

    Type of value has a mismatch with column typeCouldn't store <2019-04-18 12:32:11 PM> in dCurrantDate Column. Expected type is DateTime.

    • SᴇM
      SᴇM about 5 years
      Post your errors here in text format, not images.
    • ProgrammingLlama
      ProgrammingLlama about 5 years
      DateTime doesn't have a method Now() - only a property Now. Please correct your code. If DateTime is a custom object, and not System.DateTime, please provide the signature for Now().
    • Roman
      Roman about 5 years
      Maybe DateTime.Now?
    • ProgrammingLlama
      ProgrammingLlama about 5 years
      @Roman Maybe, but given the error, maybe not. Either way, we need a minimal reproducible example.
    • Kevin Shah
      Kevin Shah about 5 years
      Please check your column data type
    • Hardik Leuwa
      Hardik Leuwa about 5 years
      I also try with System.DateTime.Now but still have same error
    • ProgrammingLlama
      ProgrammingLlama about 5 years
      Provide a minimal reproducible example, Hardik.
    • Soner Gönül
      Soner Gönül about 5 years
      Imgur blocked in Turkey. Can you please post your error message as a plain text?
  • Hardik Leuwa
    Hardik Leuwa about 5 years
    datatype in SQL is shortdatetime. is it possible to convert and store in datatable column?
  • Peter Smith
    Peter Smith about 5 years
    Can you change the column type in your SQL table?
  • Hardik Leuwa
    Hardik Leuwa about 5 years
    Ohh Sorry but i cant change column type in SQL table
  • Hardik Leuwa
    Hardik Leuwa about 5 years
    Thanks for answer. My work flow is, : ,In Sql Table have dCurrantDate column by shortdatetime datatype. I have created Datatable by fatching Sql Table columns and now i want to store date in Datatable dCurrantDate column from C#. And also have some other column in which i want to store selected date.
  • Hardik Leuwa
    Hardik Leuwa about 5 years
    I am getting issue in shortdatetime column attach to datatable column and then store value in Datatable