Solved

Write to database when string contains a '

Posted on 2007-03-22
4
153 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
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
Jon_Raymond earned 250 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now