HELP: Insert data into SQL table ERROR: Conversion failed when converting date and/or time from character string. ()

Ricardo Montoya | DYMSA 21 Reputation points
2020-11-12T11:32:16.167+00:00

Hi, i need to insert data into a SQL database, but i have some problems, can you help me??

         SqlCommand cmd = new SqlCommand();

        cmd.Parameters.Add("@Batch_ID", SqlDbType.NVarChar, 50).Value = Batch_ID;
        cmd.Parameters.Add("@PlantName", SqlDbType.NVarChar, 50).Value =Plant;
        cmd.Parameters.Add("@ReportName", SqlDbType.NVarChar, 50).Value =ReportName;
        cmd.Parameters.Add("@RepDateTime", SqlDbType.DateTime,23).Value =RepDateTime;
        cmd.Parameters.Add("@Operator_Name", SqlDbType.NVarChar, 50).Value =OPname;
        cmd.Parameters.Add("@ProdStartDateTim", SqlDbType.DateTime, 23).Value = ProdStartDateTime;
        cmd.Parameters.Add("@BatchStartDateTime", SqlDbType.DateTime, 23).Value =BStartDateTime;
        cmd.Parameters.Add("@BatchStopDateTime", SqlDbType.DateTime,23).Value = BStopDateTime;
        cmd.Parameters.Add("@Recipe_No", SqlDbType.Int,10).Value = RecipeNo;
        cmd.Parameters.Add("@Recipe_Name", SqlDbType.NVarChar, 50).Value = RecipeName;
        cmd.Parameters.Add("@Recipe_SP", SqlDbType.Real, 24).Value = RecipeSP;
        cmd.Parameters.Add("@Recipe_Achieved", SqlDbType.Real, 24).Value = RecipeAch;
        cmd.Parameters.Add("@Batch_No", SqlDbType.Int, 10).Value =BatchNo;
        cmd.Parameters.Add("@Batches_Total", SqlDbType.Int, 10).Value = BatchTotal;
        cmd.Parameters.Add("@Batch_SP", SqlDbType.Real, 24).Value = BatchSP;
        cmd.Parameters.Add("@Batch_Achieved", SqlDbType.Real, 24).Value =BatchAch;
        cmd.Parameters.Add("@Line", SqlDbType.Int, 10).Value =Line;
        cmd.Parameters.Add("@State", SqlDbType.Int, 10).Value = State;
        cmd.Parameters.Add("@Executor", SqlDbType.BigInt, 19).Value =Executor;
        cmd.CommandText = @"INSERT INTO dbo.batch VALUES ('@Batch_ID','@PlantName','@ReportName','@RepDateTime','@Operator_Name','@ProdStartDateTime','@BatchStartDateTime','@BatchStopDateTime','@Recipe_No','@Recipe_Name','@Recipe_SP','@Recipe_Achieved','@Batch_No','@Batches_Total','@Batch_SP','@Batch_Achieved','@Line','@State','@Executor')";

        oDataadapter = new SqlDataAdapter(cmd.CommandText,CC);
        odataset = new DataSet();
        oDataadapter.Fill(odataset, "batch");
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,106 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
999 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 117.2K Reputation points
    2020-11-12T11:36:27.277+00:00

    Try removing all of apostrophes.


  2. Ricardo Montoya | DYMSA 21 Reputation points
    2020-11-12T15:43:40.48+00:00

    Hello, the problem is solved.

    i changed cmd.Parameters.Add for cmd.Parameters.AddWithVale all the datetime data,

            cmd.Parameters.AddWithValue("@RepDateTime", RepDateTime);
    

    thank you


  3. Erland Sommarskog 111.1K Reputation points MVP
    2020-11-12T23:04:27.037+00:00

    AddWithValue is not very good. What looks odd in your code is that you have lengths even where they not needed. That is,

    cmd.Parameters.Add("@ProdStartDateTim", SqlDbType.DateTime, 23).Value = ProdStartDateTime;
    

    should be

    cmd.Parameters.Add("@ProdStartDateTim", SqlDbType.DateTime).Value = ProdStartDateTime;
    

    Same goes for the Real values.

    Really if the 23 is used somewhere I don't know., but you could try it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.