When I try to input data into a column in a table, the error 'Data too long' appears despite my data being 5 characters and the field is a varchar(8). What would I need to change in order for this to work as when I add a watch to the command the character is only 5 characters long still.
Here is my command code:
[HttpPost]
public IActionResult CreateCar(CarModel carModel)
{
if (ModelState.IsValid)
{
var connection = new MySqlConnection(Configuration.GetConnectionString("MySqlConnection"));
var command =
"INSERT INTO `tblcars` (`reg`, `make`, `model`, `colour`, `type`, `fueltype`, `regDate`, `lastService`, `nextService`, `mileage`, `status`) VALUES ('@Reg', '@Make', '@Model', '@Colour', '@Type', '@FuelType', '@RegDate', '@LastService', '@NextService', '@Mileage', '@Status')";
var cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@Reg", carModel.Reg.ToUpper(new CultureInfo("en-GB", false)));
cmd.Parameters.AddWithValue("@Make", carModel.Make.ToUpper(new CultureInfo("en-GB", false)));
cmd.Parameters.AddWithValue("@Model", carModel.Model.ToUpper(new CultureInfo("en-GB", false)));
cmd.Parameters.AddWithValue("@Colour", carModel.Colour.ToUpper(new CultureInfo("en-GB", false)));
cmd.Parameters.AddWithValue("@Type", carModel.Type.ToUpper(new CultureInfo("en-GB", false)));
cmd.Parameters.AddWithValue("@FuelType", carModel.FuelType.ToUpper(new CultureInfo("en-GB", false)));
cmd.Parameters.AddWithValue("@regDate", carModel.RegDate.ToString("yyyy-MM-dd hh:mm:ss"));
cmd.Parameters.AddWithValue("@LastService", carModel.ServiceDate.ToString("yyyy-MM-dd hh:mm:ss"));
cmd.Parameters.AddWithValue("@NextService", carModel.NextServiceDate.ToString("yyyy-MM-dd hh:mm:ss"));
cmd.Parameters.AddWithValue("@Mileage", carModel.Mileage);
cmd.Parameters.AddWithValue("@Status", carModel.Status);
connection.Open();
cmd.ExecuteNonQuery();
connection.Dispose();
cmd.Dispose();
return RedirectToAction("CreateCarConfirmed");
}
return View();
}
The view is set to only accept a dropdown selection for the field that is telling is too long. Here is the view code (pastebin as it tries to display the HTML): https://pastebin.com/ijG8CG9j