Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Write to database when string contains a '

Posted on 2007-03-22
4
Medium Priority
?
159 Views
Last Modified: 2010-04-23
I am trying to wrtie the contents of text box to a database but I am getting and error when there is a ' in the text, example justine's

here is the the code I am using.  strrelation2descp = " & txtsourcedes2.Text & ",  I have tried single/double and just double quotes I still get the error.   Thank you for your help

Dim cmd8 As New SqlClient.SqlCommand("UPDATE tblHousehold set curchecking =CAST('" & txtchk.Text & "' as money),cursavings =CAST('" & txtsav.Text & "' as money),curcash =CAST('" & txtcash.Text & "' as money),curcds =CAST('" & txtcd.Text & "' as money) ,curproperty =CAST('" & txtprop.Text & "' as money),curassets =CAST('" & txtother.Text & "' as money),ysnfoodstamps = '" & ysnfs & "' ,strvehicle1 = '" & txtvech1.Text & "',strvehicle1model = '" & txtmod1.Text & "',strvehicle1year = '" & txtyear1.Text & "',curvehicle1owed = cast('" & txtamtowed1.Text & "'as money),strvehicle2 = '" & txtvech2.Text & "',strvehicle2model = '" & txtmod2.Text & "',strvehicle2year = '" & txtyear2.Text & "',strrelation1descp = " & txtsourcedes1.Text & ",strrelation2descp = " & txtsourcedes2.Text & ",strrelation3descp = " & txtsourcedes3.Text & ",strrelation4descp = " & txtsourcedes4.Text & ",strrelation5descp = " & txtsourcedes5.Text & ",curvehicle2owed = cast('" & txtamtowed2.Text & "'as money)where lngHouseid =" & SqlParametersDB.Name.lnghouseid, Connection1)
        Try
            cmd8.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
0
Comment
Question by:running32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
Jon_Raymond earned 1000 total points
ID: 18772876
This is a common problem. You should have a function on the front end that reformats the string and replaces all single quotes (') with 2 singke quotes (''). In visual basic you can use the REPLACE function.
0
 
LVL 17

Assisted Solution

by:ZeonFlash
ZeonFlash earned 1000 total points
ID: 18772880
You might try running a quick single quote double-upper to fix that....something along the lines of:

strrelation2descp = " & txtsourcedes2.Text.Replace("'","''")
0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 18772890
Cursed refresh button....Jon Raymond beat me to it :)
0
 

Author Comment

by:running32
ID: 18773640
thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question