csv file tranfer into sql server . Get eroor in datetime field

Nov 22 2022 3:00 AM

Error: [System.InvalidCastException: 'Specified cast is not valid.]

code:

private void SaveImportDataToDatabase(DataTable S2P5)
{

    using (SqlConnection conn = new SqlConnection(@"Data Source=BBKUL035127\SQLEXPRESS; Initial Catalog=DB; User Id=sa; Password=1234"))

    {

        conn.Open();
        foreach (DataRow importRow in S2P5.Rows)
        {

            SqlCommand cmd = new SqlCommand
                ("INSERT INTO S2P5 (DateTime,Miliseconds,MachineAutoStartStop,Pressure,Batch,UPCurrent,LPCurrent,LinearSensor,Load,SlidePosition,Step1,Step2,Step3,Step4,Step5,Step6,Step7,Step8,Step9,Step10,UPPlatePosition,LPPlatePosition,SunGearPosition,InternalGearPosition)" + "VALUES (@DateTime,@Miliseconds,@MachineAutoStartStop,@Pressure,@Batch,@UpCurrent,@LpCurrent,@LinearSensor,@Load,@SlidePosition,@Step1,@Step2,@Step3,@Step4,@Step5,@Step6,@Step7,@Step8,@Step9,@Step10,@UPPlatePosition,@LPPlatePosition,@SunGearPosition,@InternalGearPosition)", 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.Parameters.AddWithValue("@Batch", importRow["Batch"]);
            cmd.Parameters.AddWithValue("@UPCurrent", importRow["UPCurrent"]);
            cmd.Parameters.AddWithValue("@LPCurrent", importRow["LPCurrent"]);
            cmd.Parameters.AddWithValue("@LinearSensor", importRow["LinearSensor"]);
            cmd.Parameters.AddWithValue("@Load", importRow["Load"]);
            cmd.Parameters.AddWithValue("@SlidePosition", importRow["SlidePosition"]);
            cmd.Parameters.AddWithValue("@Step1", importRow["Step1"]);
            cmd.Parameters.AddWithValue("@Step2", importRow["Step2"]);
            cmd.Parameters.AddWithValue("@Step3", importRow["Step3"]);
            cmd.Parameters.AddWithValue("@Step4", importRow["Step4"]);
            cmd.Parameters.AddWithValue("@Step5", importRow["Step5"]);
            cmd.Parameters.AddWithValue("@Step6", importRow["Step6"]);
            cmd.Parameters.AddWithValue("@Step7", importRow["Step7"]);
            cmd.Parameters.AddWithValue("@Step8", importRow["Step8"]);
            cmd.Parameters.AddWithValue("@Step9", importRow["Step9"]);
            cmd.Parameters.AddWithValue("@Step10", importRow["Step10"]);
            cmd.Parameters.AddWithValue("@UPPlatePosition", importRow["UPPlatePosition"]);
            cmd.Parameters.AddWithValue("@LPPlatePosition", importRow["LPPlatePosition"]);
            cmd.Parameters.AddWithValue("@SunGearPosition", importRow["SunGearPosition"]);
            cmd.Parameters.AddWithValue("@InternalGearPosition", importRow["InternalGearPosition"]);


            cmd.ExecuteNonQuery();
        }

    }


Answers (2)