Search code examples
.netsqlexception

String or binary data would be truncated exception?


 cmd = new SqlCommand("INSERT INTO sms_sentmessages(Mobilefrom,Mobileto,Message,senddate) VALUES('" + str.Substring(0, str.Length - 4).ToString() + "','" + number + "','" + txtmessage.Text.Replace("'", "''").Trim() + "',getdate())", con);
                                    cmd.CommandType = CommandType.Text;
                                    cmd.ExecuteNonQuery();

Solution

  • str.Substring(0, str.Length - 4)
    

    and/ or

    txtmessage.Text.Replace("'", "''").Trim()
    

    are too large to fit with the column you're trying to insert into.