Single quotes in a string cause errors


Author
Message
Andria Jensen
Andria Jensen
Advanced StrataFrame User (600 reputation)Advanced StrataFrame User (600 reputation)Advanced StrataFrame User (600 reputation)Advanced StrataFrame User (600 reputation)Advanced StrataFrame User (600 reputation)Advanced StrataFrame User (600 reputation)Advanced StrataFrame User (600 reputation)Advanced StrataFrame User (600 reputation)Advanced StrataFrame User (600 reputation)
Group: Forum Members
Posts: 336, Visits: 497
I have some string fields on a few of my forms that have apostrophes (single quotes) in them in the database.  Things like New Year's Day cause errors when I go to save the BO using the Save method.  How do I get around this?
Reply
StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Hehe, that's not a problem.  If you have any problems on the save, just post that debug output along with the post, and it will help us solve the problem much faster.

If you ever need to send values to the server (especially string values, since they can contain single quotes), don't concatenate a bunch of strings together into the whole command; parameterize the command instead.  Parameterizing commands also helps SQL Server to cache the execution path of the command for future executions since the command text itself doesn't change, just the values of the parameters.

So, instead of this:

Me.FillDataTable("SELECT * FROM MyTable WHERE MyField = '" & SomeValue & "'")

Do this:

Dim loCommand As New SqlCommand()
loCommand.CommandText = "SELECT * FROM MyTable WHERE MyField = @MyValue"
loCommand.Parameters.Add("@MyValue", SqlDbType.NVarChar)
loCommand.Parameters("@MyValue").Value = MyValue
Me.FillDataTable(loCommand)

You'll notice that you can pass a DbCommand object to the data source for execution (which is your SqlCommand object).  Have fun Smile

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search