Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
Anonymous
Not applicable

Getting Error while import csv file datetime value into sql server

error:System.InvalidCastException: 'Specified cast is not valid.'

csv data:
datetime                 Miliseconds  MachineAutoStartStop   pressure
25/10/2022 0:00        655                    1                              0.382796
25/10/2022 0:00       899                     1                              0.382796
25/10/2022 10:31     37                       1                              0.382796
25/10/2022 10:31     38                       1                              0.382796
25/10/2022 10:31     787                    1                                0.382796

error line: cmd.Parameters.AddWithValue("@DateTime", (DateTime)importRow["DateTime"]);

 

code:

using (SqlConnection conn =New SqlConnection(@"Data Source=BL03\SQLEXPRESS; Initial Catalog=HDB; User Id=sa; Password=00"))
{
    conn.Open();

    foreach (DataRow importRow in S2P5.Rows)
    {
        SqlCommand cmd = new SqlCommand ("INSERT INTO S2P5 (DateTime, Miliseconds, MachineAutoStartStop, Pressure)" + 
                                         "VALUES (@DateTime, @Miliseconds, @MachineAutoStartStop, @Pressure)", conn);
      
        cmd.Parameters.AddWithValue("@DateTime", (DateTime)importRow["DateTime"]);
        cmd.Parameters.AddWithValue("@Miliseconds", importRow["Miliseconds"]);
        cmd.Parameters.AddWithValue("@MachineAutoStartStop", importRow["MachineAutoStartStop"]);
        cmd.Parameters.AddWithValue("@Pressure", importRow["Pressure"]);

        cmd.ExecuteNonQuery();
    }
1 REPLY 1
lbendlin
Super User
Super User

You might have wanted to post this in a different forum?

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.

Top Kudoed Authors