Click here to Skip to main content
15,894,405 members
Home / Discussions / C#
   

C#

 
GeneralRe: Problem with insert query Pin
SeMartens6-Jan-10 2:26
SeMartens6-Jan-10 2:26 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 2:30
<<Tash18>>6-Jan-10 2:30 
GeneralRe: Problem with insert query Pin
SeMartens6-Jan-10 2:40
SeMartens6-Jan-10 2:40 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 20:35
<<Tash18>>6-Jan-10 20:35 
GeneralRe: Problem with insert query Pin
dojohansen7-Jan-10 5:31
dojohansen7-Jan-10 5:31 
AnswerRe: Problem with insert query Pin
OriginalGriff6-Jan-10 0:37
mveOriginalGriff6-Jan-10 0:37 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 0:40
<<Tash18>>6-Jan-10 0:40 
GeneralRe: Problem with insert query Pin
OriginalGriff6-Jan-10 1:01
mveOriginalGriff6-Jan-10 1:01 
OK, there are (at least) two ways to write to a database:
(I have broken these into several strings to make the lines shorter and easier to read.)
SqlCommand cmd = new SqlCommand("INSERT INTO EACS_User_1 " +
                                "(firstName, lastName) " +
                                "VALUES " +
                                "(" + uf_FirstName + "," + uf_LastName + ")");
which has a number of problems. One of these is that if your fields uf_FirstName and / or uf_LastName contain a quote, double quote, semicolon, or various other characters you havce a problem. The other is that this character dependancy can be used to do something called an SQL Injection Attack[^] on your database.
The other solution is called Parameterised Queries:
SqlCommand cmd = new SqlCommand("INSERT INTO EACS_User_1 " +
                                "(firstName, lastName) " +
                                "VALUES " +
                                "(@FN, @LN)");
cmd.Parameters.AddWithValue("@FN", uf_FirstName);
cmd.Parameters.AddWithValue("@LN", uf_LastName);
where @FN and @LN can be any text you like - the '@' character is just to make them easier to see and is not required (Good idea, though).
This gets rid of the problem - uf_FirstName and uf_LastName can contain any characters, including a mix of double and single quotes.

BTW: The convention is to use all UPPER CASE for SQL syntax keywords, so you can see them more easily.

All those who believe in psycho kinesis, raise my hand.

GeneralRe: Problem with insert query Pin
Ashfield6-Jan-10 1:16
Ashfield6-Jan-10 1:16 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 1:17
<<Tash18>>6-Jan-10 1:17 
GeneralRe: Problem with insert query Pin
OriginalGriff6-Jan-10 1:31
mveOriginalGriff6-Jan-10 1:31 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 1:42
<<Tash18>>6-Jan-10 1:42 
GeneralRe: Problem with insert query Pin
OriginalGriff6-Jan-10 2:07
mveOriginalGriff6-Jan-10 2:07 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 20:37
<<Tash18>>6-Jan-10 20:37 
Questionevent firing in combo box Pin
Mahesh_Blr6-Jan-10 0:01
Mahesh_Blr6-Jan-10 0:01 
AnswerRe: event firing in combo box Pin
Mycroft Holmes6-Jan-10 0:13
professionalMycroft Holmes6-Jan-10 0:13 
Questiondatagrid row color Pin
Erdinc275-Jan-10 23:58
Erdinc275-Jan-10 23:58 
AnswerRe: datagrid row color Pin
Mycroft Holmes6-Jan-10 0:06
professionalMycroft Holmes6-Jan-10 0:06 
AnswerRe: datagrid row color Pin
Qendro6-Jan-10 0:57
Qendro6-Jan-10 0:57 
GeneralRe: datagrid row color Pin
David Skelly6-Jan-10 1:37
David Skelly6-Jan-10 1:37 
GeneralRe: datagrid row color Pin
Qendro6-Jan-10 2:37
Qendro6-Jan-10 2:37 
GeneralRe: datagrid row color Pin
David Skelly6-Jan-10 5:09
David Skelly6-Jan-10 5:09 
GeneralRe: datagrid row color Pin
Erdinc276-Jan-10 7:56
Erdinc276-Jan-10 7:56 
AnswerRe: datagrid row color Pin
David Skelly6-Jan-10 1:41
David Skelly6-Jan-10 1:41 
GeneralRe: datagrid row color Pin
Erdinc276-Jan-10 8:00
Erdinc276-Jan-10 8:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.