ExecuteNonQuery Error: String or binary data would be truncated. The statement has been terminated

Last Reply one year ago By chetan

Posted one year ago


Server Error in '/' Application.
String or binary data would be truncated.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 59:             cmd.Connection = con;
Line 60:             con.Open();
Line 61:             cmd.ExecuteNonQuery();
Line 62:             con.Close();
Line 63:             // this.GetData();



protected void btnsubmit5_Click(object sender, EventArgs e)
        string strname = ""; // ADDED
        if (FileUploadImage.HasFile)
            strname = FileUploadImage.FileName.ToString();
            FileUploadImage.PostedFile.SaveAs(Server.MapPath("~/UserImage/") + strname);                          // string imagename = FileUpload1.PostedFile.FileName;
                                                                                                                  // FileUpload1.SaveAs(Server.MapPath("~/UserImage/" + imagename));
                                                                                                                  //  string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetRandomFileName());
            SqlConnection con = new SqlConnection(constring);
            SqlCommand cmd = new SqlCommand("INSERT INTO Employees (Name,Address,Phone,Gender,Location,Department,State,Salary,Images) VALUES(@Name,@Address,@Phone,@Gender,@Location,@Department,@State,@Salary,@Images)", con);
            // cmd.Parameters.AddWithValue("@UserName", HttpContext.Current.Session["userName"]);
            cmd.Parameters.AddWithValue("@Name", txtname.Text.Trim());
            cmd.Parameters.AddWithValue("@Address", txtaddress.Text.Trim());
            cmd.Parameters.AddWithValue("@Phone", txtphone.Text.Trim());
            cmd.Parameters.AddWithValue("@Gender", ddlgender.SelectedItem.Text.Trim());
            cmd.Parameters.AddWithValue("@Location", txtlocation.Text.Trim());
            cmd.Parameters.AddWithValue("@Department", ddldepartment.SelectedItem.Text.Trim());
            cmd.Parameters.AddWithValue("@Images", strname);
            cmd.Parameters.AddWithValue("@State", txtstate.Text.Trim());
            cmd.Parameters.AddWithValue("@Salary", txtsalary.Text.Trim());
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = con;
            // this.GetData();


Posted one year ago

Hi micah,

Its the error because of datatype size you are passing for inserting data to that field.

Suppose i you have declared a field named name varchar[20] and trying to insert more than 20 characters then you will get an error like "String or binary data would be truncated".

So please check all the field.

Posted one year ago

hi micah...

check the size of the datatype in database table.

suppose name varchar(20),and if you are inserting more than 20 characters,the error comes saying that "string or binary data would be truncated".