Results 1 to 7 of 7

Thread: problem with single quote

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    problem with single quote

    hi pals!!! i have an encryption/decryption method sometimes the encrypted string has a single quote like this
    Code:
    E'å³
    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!!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: problem with single quote

    Ok..i'll try your example..thanks!!

  6. #6

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: problem with single quote

    great!!! it works! thanks!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: problem with single quote

    Cool. Don't forget to resolve your thread from the Thread Tools menu.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width