Below is my trimmed down C#:
thisConnection.Open();
string commandtext = "Insert into Table (Comments)values('@Comments')";
SqlCommand command = new SqlCommand(commandtext, thisConnection);
command.Parameters.Add("@Comments", SqlDbType.VarChar).Value = Comments;
command.ExecuteNonQuery();
thisConnection.Close();
I can enter almost anything, with certain special characters being stripped out before being entered into the database, which is fine by me, by even just one single quote will throw a spanner in the works. I've tried adding .Replace("'","''"); to the Comments variable but this doesn't change anything, and I though using parameters should prevent this anyway.
I know questions like this have been asked a lot, but everywhere just points at "use parameters!"
Edit: Seeing as four people have said the same thing, I'm replying to it all here.
I have removed the single quotes around @Comments, but the issue is the exact same. Any input with single quotes isn't entered to the database at all.
I have added .replace(/'/g,"''") before we get here with javascript, and this is working, but I don't see why I should have to.
commandtextvariable somewhere that you pass to theSqlCommandconstructor, and you then immediately override theCommandTextproperty of the command, socommandtextis effectively unused.