473,657 Members | 2,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

insert into syntax error while using a variable containing quotes

1 New Member
I an adding data into an access table via c# and have run into a roadblock. The syntax is

Expand|Select|Wrap|Line Numbers
  1. addSQL = "INSERT INTO history ([id], [partner], [from], [msgdate], [msgtime], [message]) VALUES ('" + thisid + "','" + chat.DialogPartner + "','" + chatmessage.FromHandle + "','" + chatmessage.Timestamp.Date + "','" + chatmessage.Timestamp.TimeOfDay + "','" + chatmessage.Body + "')";
The problem relates to the last item, chatmessage.Bod y, which can contain anything a person can type, including single and double quotes. So how do I get the contents of that variable inserted into an access table?
Nov 6 '11 #1
3 1808
Rabbit
12,516 Recognized Expert Moderator MVP
Escape the quotes before appending them to the string.
Nov 7 '11 #2
Mihail
759 Contributor
...or double any quote in chatmessage.Bod y
If chatmessage.Bod y is abcd"efgh"ijc then transform this in abcd""efgh""ijc.
Nov 7 '11 #3
NeoPa
32,568 Recognized Expert Moderator MVP
Try :
Expand|Select|Wrap|Line Numbers
  1. string strBody;
  2. strBody = (string)chatmessage.Body
  3. strBody = strBody.Replace("'", "''");
  4. addSQL = "INSERT INTO history " +
  5.          "([id], [partner], [from], [msgdate], [msgtime], [message]) " +
  6.          "VALUES ('" + thisid + "','" + chat.DialogPartner + "','" + 
  7.          chatmessage.FromHandle + "','" + chatmessage.Timestamp.Date + 
  8.          "','" + chatmessage.Timestamp.TimeOfDay + "','" + strBody + "')";
PS. If I got some of the syntax wrong then I'm sure you can fix it. I've not done too much C# code.
Nov 12 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2533
by: JC-Atl | last post by:
I am trying to do this in the SQL page of phpMyAdmin, but I get a syntax error. Can someone please help me out ? Thanks for your time and efforts! JC INSERT INTO `Test` ( `pkey` , `col2` , `col3` ) VALUES ( SELECT max( `pkey` ) FROM Test, `rrrrrrrrr` , `wwwwwwwww` )
5
2000
by: Berend | last post by:
when I try to write to a database I get a syntax error I made the code a simple as possible bur also the VS generated code gives the same error. WHY? private void button1_Click(object sender, System.EventArgs e) { string strSQL; strSQL = "INSERT INTO Overwerk(datum , begin, eind, omschrijving) VALUES('10-01-2004', '12:00', '16:00', 'Test')";
11
2605
by: KavvY | last post by:
Hi Can anyone tell me why I get a SQL syntax error with the following code? string strInsert = "INSERT INTO dateEntry (entry, date) VALUES ('test3', '17/08/2004')"; OleDbCommand cmd = new OleDbCommand(strInsert, oleDbConnection1); try
7
6657
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double checked, and the insert works fine (tried to use it from access)... im using visual C# express 2k5... what could be wrong? thanks!
2
4545
by: devprog | last post by:
objConn = New ADODB.Connection objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath _ & ";Jet OLEDB:Database Password=" & dbPassword) This cause error: (VB.NET. ) sqlString = "insert into table1 " _ & "(Date1,Time,Ext,Co) values " _ & "('04/04/05','06:02PM','101','01');"
11
1898
by: Frankie | last post by:
Hello: New user here...first post to group. I'm getting an SQL syntax error when I try to run the following query: $query = sprintf("SELECT itemNumber, entryDate, modifyDate, thumbnailURL, title, price FROM '%s' WHERE itemNumber = '%s'", $_POST, $_POST);
2
4802
by: technocraze | last post by:
Hi guys, I have encountered this error when updating the values to the MS Acess table. Error : Update on linked table failed. ODBC sql server error Timeout expired. MS Acess is my front end and sql server is my backend server. This error occured whenever i step through and when it reaches rs.update it jux hangs down there and thereafter it shwn the aforementioned error. I am using the RecordSet properties to add the values. Can I use...
8
3374
by: jennwilson | last post by:
I am a somewhat Novice Access user and have limited rather zilch programming skills. Problem: I am receiving the following error: "The expression you entered contains invalid syntax' In the query, I have built several fields containing calculations. In one of the fileds, I am trying to have the field insert one value, if the current value is greater than the other value. For example - if AvgEarn equals 9 and Expected is 8, I want to...
6
1704
by: adigga1 | last post by:
Hello, I am attempting to call a particular table if the OR statement is used. When I try it gives me a Syntax error. Please assist? Here is the code: SELECT T_Physicians.TaxID, T_ECCcpt.CPTcode, T_ECCcpt.Charge, * FROM T_Physicians, T_ECCcpt WHERE (((T_Physicians.TaxID)="58-229xxxx")) OR
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.