I have a textarea control that passes it's contents to be stored onto an SQL table. I'm running into problems with my SQL Insert command whenever it tries to pass a string from the textarea that includes an apostraphe; like the sentance "I've got a car".
I'm geussing it is because SQL recognizes the apostraphe in "I've" as the end of a column value. Is there an escape character that will tell SQL that the apostraphe is part of the string input?
Storing an apostraphe in SQL column value
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

bednarjm4 reputationPosted Jun 15, 2009, 12:16 PM EDT
1. use parameters with your sql query (the preferred way):
for command object (cmd}
cmd.Paramaters.AddWithValue
2. escape the single ' with a two ''
string sql = "insert into myTable (id, name) values ("abcd","Fred Flint''stone");
Jim B
Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Jun 15, 2009, 12:22 PM EDT
txtFirstName.Text.Replace("'", "''")
bednarjm4 reputationPosted Jun 15, 2009, 12:22 PM EDT
using (SqlCommand cmd = new SqlCommand(sql, cn)) { cmd.Parameters.AddWithValue("@id","abcd"); cmd.Parameters.AddWithValue("@name","It's a test"); cmd.ExecuteNonQuery; }