Nel

Nel

  • NA
  • 716
  • 1.1m

Problem with converting to Time type

Aug 28 2014 3:40 PM

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)