Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Datetime in Datatable Datetime column

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.

like image 627
Hardik Leuwa Avatar asked Mar 18 '26 06:03

Hardik Leuwa


1 Answers

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);
}
like image 60
Ravi Mittal Avatar answered Mar 20 '26 18:03

Ravi Mittal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!