Handling Apostrophes

Results 1 to 4 of 4

Thread: Handling Apostrophes

  1. #1
    Join Date
    Dec 1969

    Default Handling Apostrophes

    I use this cool code to handle apostrophes when I insert a record into SQL database using ADO.NET...<BR>"INSERT INTO Titles (title_id, title)" & _<BR> " VALUES(&#039;" & txtTitleID.Text.ToString & "&#039;, &#039;" & Replace(txtTitle.Text.ToString, "&#039;", "&#039;&#039;") & "&#039;)"<BR><BR>The Replace works great. I am wondering if anyone knows how to do this same task (handle apostrophes) while Updating?<BR><BR>Thanks!

  2. #2
    Join Date
    Dec 1969

    Default RE: Handling Apostrophes

    I would think doing it the same way would work.<BR><BR>"UPDATE Titles " & _<BR>"SET title_id = &#039;" & txtTitleID.Text.ToString & "&#039;, " & _<BR>"title = &#039;" & Replace(txtTitle.Text.ToString, "&#039;", "&#039;&#039;") & "&#039; " & _<BR>"WHERE ..."<BR><BR><BR>T

  3. #3
    Join Date
    Dec 1969

    Default I prefer..

    .. to use the Replace method of the String object:<BR><BR>txtTitle.Text.Replace("&#039;", "&#039;&#039;")

  4. #4

    Default No strings prefered

    I try to avoid building sql strings in my code, so I would put the insert into a stored procedure and avoid the issue created by building a string.<BR><BR>I use Microsoft.ApplicationBlocks.Data (check MSDN) for easy!!! data access. It is designed for SQL Server 7+ (including MSDE) but porting it to your DB is probably worth the effort.<BR><BR>I use the SQLHelper in this example, but you can accomplish the same thing without it.<BR>Creating a parametric text command:<BR>DataSet test = SqlHelper.ExecuteDataset(<BR> ConnectionString<BR> ,CommandType.Text<BR> ,"UPDATE Titles SET title= @Title Where TitleID=@TID"<BR> ,new SqlParameter("@TID", txtTitleID.Text)<BR> ,new SqlParameter("@Title", txtTitle.Text)<BR> );<BR><BR>

Posting Permissions

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