H
H
HelpIT2021-01-16 01:19:39
SQL
HelpIT, 2021-01-16 01:19:39

System.Data.SqlClient.SqlException: "String or binary data would be truncated. The statement has been terminated."?

protected void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings
["StudentConnectionString"].ConnectionString);
conn.open();
SqlCommandcmd;
SqlDataAdapter adapter = new SqlDataAdapter();
String sql = "";

sql = "update Students set firstName = '" + txtBoxfName + "'," +
" lastName = '" + txtBoxlName + "', " +
"studentNo = '" + Convert.ToInt32(txtBoxStudentNo.Text) +
"' where id_student ='"

cmd = new SqlCommand(sql, conn);

adapter.InsertCommand = new SqlCommand(sql, conn);
adapter.InsertCommand.ExecuteNonQuery(); // problem appears on this line
Label2.Text = "student successfully updated";

txtBoxfName.Text = " ";
txtBoxlName.Text = " ";
txtBoxStudentNo.Text = " ";

cmd.dispose();
conn.Close();

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fallenyasha, 2021-01-16
@HelpIT

This error means that you are trying to write a string longer than the field allows, probably firstName or lastName. This check can be disabled, for example, via SET ANSI_WARNINGS ON|OFF, or you can cut the string to fit the field size.

L
Lazy @BojackHorseman, 2021-01-16
SQL

Yes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question