42
Answers

Problem with converting to Time type

Nel

Nel

10y
8k
1

Hello,


I have a datatable which contains data from a csv file and also from this data table the data should be inserted into sql table

One of the fields in the datatable is Time, and I have problem when trying to insert it in the sql table In the sql table it is of format Time (not datatime)

Here is my code:

 SqlParameter timefromParameter = new SqlParameter
                  ("@TimeFrom",SqlDbType.DateTime );
             
               DateTime timefrom = DateTime.ParseExact(Convert.ToString(row["Time-from"]), "MM/dd/yyyy HH:mm", null);
            
                insertTable.Parameters.AddWithValue("@TimeFrom", timefrom.TimeOfDay);


but I get the following error:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: String was not recognized as a valid DateTime.

on line:

 DateTime timefrom = DateTime.ParseExact(Convert.ToString(row["Time-from"]), "MM/dd/yyyy HH:mm", null);


Can anybody help me please?

Thanks


Answers (42)