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();
3 Answers
Well, presumably you're trying to insert a value into the table and it exceeds the length for the column. You should validate the data before you try to execute the SQL insert.
However, you should also use a parameterized query - currently your code is vulnerable to a SQL injection attack. See the Bobby Tables site for more information. Basically, parameterized SQL allows you to keep the code (SQL) separate from the data (parameters), making your code clearer and preventing SQL injection attacks (assuming you're not also using dynamic SQL).
(Finally, it's not clear why you're calling ToString() on the result of a Substring call...)