Results 1 to 9 of 9

Thread: how to wrote ' inside string in VB.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    3

    how to wrote ' inside string in VB.net

    Dim strSQL As String = "Select * from Doctors where first_name= '" & First_name.Text & "' and last_name= '" & Last_name.Text & "' "
    Dim da As SqlDataAdapter = New SqlDataAdapter(strSQL, DBConn)
    Dim ds As DataSet = New DataSet
    da.Fill(ds)

    My problem is: Last_name may contain ', for example O'Brien
    the query will fail. strSQl= "select * from Doctors where last_name=O'Brien"

    how can I wrote ' inside the string? for my textbox : Last_name.Text
    it may contain ', what 's correct syntax to write my query?
    Thanks!

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: how to wrote ' inside string in VB.net

    Instead of ', you can write chr(39). 39 is the ASCII value of '
    Show Appreciation. Rate Posts.

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: how to wrote ' inside string in VB.net

    VB Code:
    1. Dim sqlCmd As New SqlClient.SqlCommand("Select * from Doctors where first_name=@First and last_name=@Last")
    2.             sqlCmd.Parameters.Add("@First", First_name.Text)
    3.             sqlCmd.Parameters.Add("@Last", Last_name.Text)

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    3

    Re: how to wrote ' inside string in VB.net

    thanks for your reply.
    but How can I write query? strSQL="select * from Doctors where last_name=
    ' " & Last_name.text & " ' "

    Last_name.text is the texbox value. sometimes it may contain ', how can I do it? Thanks!

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    3

    Re: how to wrote ' inside string in VB.net

    sorry, I did not see last reply. It works. I appreciated your help. Thanks!

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

    Re: how to wrote ' inside string in VB.net

    Just one point to note: if you're using VB 2005 it will be AddWithValue rather than Add. Please specify your version in future threads. Also, don't forget to resolve your thread from the Thread Tools menu if your question has been answered.
    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

  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: how to wrote ' inside string in VB.net

    And just for future reference your problem is a SQL problem, not a VB problem. To get around this, you can simply replace all single quotes, with two single quotes. So in your example, O'Brien would be inserted as O''Brien, and the SQL statement would work.

    To do it in your code, it is as simple as:

    VB Code:
    1. Dim strSQL As String = "Select * from Doctors where first_name= '" & First_name.Text.Replace("'","''") & "' and last_name= '" & Last_name.Text.Replace("'","''") & "' "

  8. #8
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: how to wrote ' inside string in VB.net

    Quote Originally Posted by Negative0
    And just for future reference your problem is a SQL problem, not a VB problem. To get around this, you can simply replace all single quotes, with two single quotes. So in your example, O'Brien would be inserted as O''Brien, and the SQL statement would work.

    To do it in your code, it is as simple as:
    This is not a good idea. Using Parametrized queries is the proper and correct solution.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  9. #9
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: how to wrote ' inside string in VB.net

    Instead of handling single quotes in dynamically built SQL statements I'd recomment using the SqlCommand object and set the parameter values. If this is not feasible then a function like below might help.

    VB Code:
    1. ' This should be in a Utils class and invoked as Utils.HandleQuote("O'Brien")
    2.  
    3. public shared Function HandleQuote(byval data as string) As String
    4.   ' replace single occurrences of single quote with two single quotes
    5.   ' For O'Brien function will return 'O''Brien'
    6.   Return "'" & String.Replace(data, "'", "''") & "'"
    7. End Function
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

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