I am storing and editing some field in a database that involves a long string of one or more sentences. whenever i enter a single quote in the textbox and want to save it it throws an exception like "Incorrect syntax near 'l'. Unclosed quotation mark after the character string ''." is there any idea to avoid that?
EDIT: The query is:
SqlCommand com = new SqlCommand("UPDATE Questions SET Question = '[" +
tbQuestion.Text + "]', Answer = '[" +
tbAnswer.Text + "]', LastEdit = '" +
CurrentUser.Login +
"'WHERE ID = '" + CurrentQuestion.ID + "'");
SET @Query = @Query + ' WHERE ' + '' + @param + ' ' + @operator + ' ' + '' + @val + '' ; Thanks!
As KM said, don't do this!
Do this instead:
private static void UpdateQuestionByID(
int questionID, string question, string answer, string lastEdited)
{
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
const string QUERY =
@"UPDATE Questions " +
@"SET Question = @Question, Answer = @Answer, LastEdit = @LastEdited " +
@"WHERE ID = @QuestionID";
using (var cmd = new SqlCommand(QUERY, conn))
{
cmd.Parameters.AddWithValue("@Question", question);
cmd.Parameters.AddWithValue("@Answer", answer);
cmd.Parameters.AddWithValue("@LastEdited", lastEdited);
cmd.Parameters.AddWithValue("@QuestionID", questionID);
cmd.ExecuteNonQuery();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With