D
D
deadspace122020-12-11 16:14:26
MySQL
deadspace12, 2020-12-11 16:14:26

How to fix the error - "data truncate for column"?

these are tables in the database
5fd36f8b5dc2a652295370.png
5fd36fa881cbd131946791.png
5fd36fb3e79c6915226183.png
and pictures with errors why it cannot recognize a field with a float format and a field with a datetime format
5fd3701faa05c897927597.jpeg
5fd37036369a7951436115.jpeg
here is the code with a datetime error

private void button1_Click(object sender, EventArgs e)
{
MySqlConnectionStringBuilder mysqlCSB;
mysqlCSB = new MySqlConnectionStringBuilder();
mysqlCSB.Server = "127.0.0.1";
mysqlCSB.Database = "vlados";
mysqlCSB.UserID = "root";
mysqlCSB.Password = "T2(!)3672+family";
using (MySqlConnection cn = new MySqlConnection(mysqlCSB.ConnectionString))
{
try
{
cn.Open();
string SInsSql = "Insert into servicesandbooking(idbooking, Databooking, id_orders, idstaff, idguests) Values(' { 0 } ',' { 1 } ',' { 2 } ',' { 3 } ',' { 4 } ')";
int idbooking = Convert.ToInt32(textBox1.Text);
DateTime Databooking = dateTimePicker1.Value;
int id_orders = Convert.ToInt32(textBox2.Text);
int idstaff = Convert.ToInt32(textBox3.Text);
int idguests = Convert.ToInt32(textBox4.Text);
string sInsSotr = string.Format(SInsSql, idbooking, Databooking, id_orders, idstaff, idguests);
MySqlCommand cmdIns = new MySqlCommand(sInsSotr, cn);
cmdIns.ExecuteNonQuery();
MessageBox.Show("Запись успешно вставлена !");
cn.Close();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
и код с ошибкой поля типа float
private void button1_Click(object sender, EventArgs e)
{
MySqlConnectionStringBuilder mysqlCSB;
mysqlCSB = new MySqlConnectionStringBuilder();
mysqlCSB.Server = "127.0.0.1";
mysqlCSB.Database = "vlados";
mysqlCSB.UserID = "root";
mysqlCSB.Password = "T2(!)3672+family";
using (MySqlConnection cn = new MySqlConnection(mysqlCSB.ConnectionString))
{
try
{
cn.Open();
string SInsSql = "Insert into menu(id_orders, Nname, serving_size, cost, cooking_time) Values('{0}','{1}','{2}','{3}','{4}')";
int id_orders = Convert.ToInt32(textBox4.Text);
string Nname = textBox1.Text;
int serving_size = Convert.ToInt32(textBox2.Text);
int cost = Convert.ToInt32(textBox3.Text);
float cooking_time = float.Parse(textBox5.Text);
string sInsSotr = string.Format(SInsSql, id_orders, Nname, serving_size, cost, cooking_time);
MySqlCommand cmdIns = new MySqlCommand(sInsSotr, cn);
cmdIns.ExecuteNonQuery();
MessageBox.Show("Запись успешно вставлена !");
cn.Close();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}

please help me i don't know where i could be wrong i looked at my code twice and i don't see anything

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2020-12-11
@deadspace12

  1. Show the structure of tables (types of fields).
  2. Use a debugger or at least MessageBox.Show() to find out exactly what values ​​you are trying to insert.
  3. Field names should be written in lower case.
  4. Perhaps a real number should have a period separator. Or, conversely, a comma. Depends on locale settings.
  5. Perhaps you need to write a string to datetime, not a DateTime object

V
Vasily Bannikov, 2020-12-11
@vabka

It's not a mistake. It's just a message that the data has been truncated to fit. Just catch this exception separately from all other MySqlException (it probably has a different class there)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question