2
 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 3

4
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.

Sign up to request clarification or add additional context in comments.

Comments

2

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...)

Comments

0

Your data (or possibly a trigger) is too big to fit into the column you are trying to store it.

At least, that's my best guess.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.