Solved

Write to database when string contains a '

Posted on 2007-03-22
4
158 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 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