How to set Datetime in Datatable Datetime column
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);`
Related videos on Youtube
Hardik Leuwa
Updated on June 04, 2022Comments
-
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 about 5 yearsPost your errors here in text format, not images.
-
ProgrammingLlama about 5 years
DateTime
doesn't have a methodNow()
- only a propertyNow
. Please correct your code. IfDateTime
is a custom object, and notSystem.DateTime
, please provide the signature forNow()
. -
Roman about 5 yearsMaybe
DateTime.Now
? -
ProgrammingLlama about 5 years@Roman Maybe, but given the error, maybe not. Either way, we need a minimal reproducible example.
-
Kevin Shah about 5 yearsPlease check your column data type
-
Hardik Leuwa about 5 yearsI also try with System.DateTime.Now but still have same error
-
ProgrammingLlama about 5 yearsProvide a minimal reproducible example, Hardik.
-
Soner Gönül about 5 yearsImgur blocked in Turkey. Can you please post your error message as a plain text?
-
-
Hardik Leuwa about 5 yearsdatatype in SQL is shortdatetime. is it possible to convert and store in datatable column?
-
Peter Smith about 5 yearsCan you change the column type in your SQL table?
-
Hardik Leuwa about 5 yearsOhh Sorry but i cant change column type in SQL table
-
Hardik Leuwa about 5 yearsThanks 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 about 5 yearsI am getting issue in shortdatetime column attach to datatable column and then store value in Datatable