Try removing all of apostrophes.
HELP: Insert data into SQL table ERROR: Conversion failed when converting date and/or time from character string. ()
Ricardo Montoya | DYMSA
21
Reputation points
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");
3 answers
Sort by: Most helpful
-
-
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
-
Erland Sommarskog 110.4K 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.