Escaping apostrophes inserting into sql

Discussion in 'ASP .Net' started by mister-Ed, Oct 5, 2007.

  1. mister-Ed

    mister-Ed Guest

    I have a datagrid, and when initializing my field variables, I need to
    double up apostrophes so they are accepted into SQL dbase. In the line
    below, i'm trying to do this with the Replace function, but i still
    get an error when entering an apostrophe:

    Dim sCompany As String = CType(e.Item.FindControl("textbox3"),
    textbox).Text.Replace("'", "''")

    ???
    Mr. Ed
    mister-Ed, Oct 5, 2007
    #1
    1. Advertising

  2. mister-Ed

    David Wier Guest

    Why don't you use parameterized queries, or stored Procedures?
    That way, you don't need to worry about things like this, and your
    application will be more secure also.

    David Wier
    http://aspnet101.com
    http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
    bloated markup


    "mister-Ed" <> wrote in message
    news:...
    >I have a datagrid, and when initializing my field variables, I need to
    > double up apostrophes so they are accepted into SQL dbase. In the line
    > below, i'm trying to do this with the Replace function, but i still
    > get an error when entering an apostrophe:
    >
    > Dim sCompany As String = CType(e.Item.FindControl("textbox3"),
    > textbox).Text.Replace("'", "''")
    >
    > ???
    > Mr. Ed
    >
    David Wier, Oct 5, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Huddle

    SQL and apostrophes

    Chris Huddle, Dec 10, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    729
    Steve C. Orr [MVP, MCSD]
    Dec 10, 2003
  2. MS
    Replies:
    3
    Views:
    16,118
    Marcin Grunwald
    Feb 22, 2005
  3. Rob Meade

    apostrophes in SQL statement

    Rob Meade, Apr 21, 2004, in forum: ASP General
    Replies:
    22
    Views:
    348
    Bob Barrows [MVP]
    Apr 22, 2004
  4. Lord Merlin

    inserting apostrophes into DB?

    Lord Merlin, Jul 1, 2004, in forum: ASP General
    Replies:
    4
    Views:
    164
    Aaron [SQL Server MVP]
    Jul 2, 2004
  5. Eric Osman
    Replies:
    2
    Views:
    341
    Eric Osman
    Apr 14, 2004
Loading...

Share This Page