|
-
Sep 3rd, 2006, 11:24 PM
#1
Thread Starter
Fanatic Member
problem with single quote
hi pals!!! i have an encryption/decryption method sometimes the encrypted string has a single quote like this now my problem is everytime I execute query to insert that string into my database it always gives me an error message saying
Incorrect syntax near 'åÂ'.
Unclosed quotation mark after the character string ')'.
is there any posible solution for this? thanks in advance!!!
-
Sep 3rd, 2006, 11:28 PM
#2
Re: problem with single quote
How exactly are you inserting this data? Are you using parameters like you should be, or are you using string concatenation to build up a literal SQL statement?
-
Sep 4th, 2006, 12:10 AM
#3
Thread Starter
Fanatic Member
Re: problem with single quote
im using string concatenation....
Code:
encPswrd = RndCrypt(txtNewPassword.Text, txtNewUsername.Text.ToUpper());
qryStr = @"insert into users values('" + txtNewUsername.Text.ToUpper() + "','" + encPswrd + "','" + restriction + "')";
sqlCmd = new SqlCommand(qryStr, sqlConn);
sqlCmd.ExecuteNonQuery();
is there a better way to perform the above code?
-
Sep 4th, 2006, 12:20 AM
#4
Re: problem with single quote
Using string concatenation to build SQL statements is a bad idea for several reasons and this is just one of them. Use parameters wherever possible.
Code:
qryStr = "INSERT INTO Users (UserName, EncPswrd, Restriction) VALUES (@UserName, @EncPswrd, @Restriction)";
sqlCmd = new SqlCommand(qryStr, sqlConn);
sqlCmd.Parameters.AddWithValue("@UserName", txtNewUsername.Text.ToUpper());
sqlCmd.Parameters.AddWithValue("@EncPswrd", encPswrd);
sqlCmd.Parameters.AddWithValue("@Restriction", restriction);
-
Sep 4th, 2006, 12:33 AM
#5
Thread Starter
Fanatic Member
Re: problem with single quote
Ok..i'll try your example..thanks!!
-
Sep 4th, 2006, 02:50 AM
#6
Thread Starter
Fanatic Member
Re: problem with single quote
great!!! it works! thanks!
-
Sep 4th, 2006, 02:51 AM
#7
Re: problem with single quote
Cool. Don't forget to resolve your thread from the Thread Tools menu.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|