473,396 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Input Error

210 Expert 100+
The problem that i am having is when ever i input text in format like O'Neal as lastname then my code breaks and gives me following error

Incorrect syntax near 'Neal'. Unclosed quotation mark before the character string ''.

If i remove ( ' ) from the Input the code works. How do i solve this? Please advice.

Expand|Select|Wrap|Line Numbers
  1.  
  2. string strSql = " Update Drivers SET CarrierID =@carrierid, LastName =@lastname, FirstName =@firstname, Address =@address, City =@city," +
  3.             "StateID =@stateid, ZipCode =@zipcode, CellAreaCode =@cellareacode, CellPrefix =@cellprefix, CellPhone =@cellphone, "+
  4.             "HomeAReaCode =@homeareacode, HomePrefix =@homeprefix, HomePhone =@homephone, FaxAreaCode =@faxareacode, FaxPrefix =@faxprefix," +
  5.             "Fax =@fax, TruckNumber =@trucknumber where driverid = " + DriverID;
  6.  
  7.         SqlConnection conn = new SqlConnection();
  8.         String constr = ConfigurationManager.ConnectionStrings[("USBSRVR")].ConnectionString;
  9.         conn = new SqlConnection(constr);
  10.         SqlCommand mycommand = new SqlCommand(strSql, conn);
  11.  
  12.         //adding sqlparameters
  13.         #region parameters
  14.  
  15.         SqlParameter carrier = new SqlParameter("@carrierid", SqlDbType.Int);
  16.         mycommand.Parameters.Add(carrier);
  17.         mycommand.Parameters["@carrierid"].Value = CarrierID;
  18.  
  19.         SqlParameter Last = new SqlParameter("@lastname", SqlDbType.NVarChar);
  20.         mycommand.Parameters.Add(Last);
  21.         mycommand.Parameters["@lastname"].Value = LastName;
  22.  
  23.         SqlParameter First = new SqlParameter("@firstname", SqlDbType.NVarChar);
  24.         mycommand.Parameters.Add(First);
  25.         mycommand.Parameters["@firstname"].Value = FirstName;
  26.  
  27.         SqlParameter addres = new SqlParameter("@address", SqlDbType.NVarChar);
  28.         mycommand.Parameters.Add(addres);
  29.         mycommand.Parameters["@address"].Value = Address;
  30.  
  31.         SqlParameter drivercity = new SqlParameter("@city", SqlDbType.NVarChar);
  32.         mycommand.Parameters.Add(drivercity);
  33.         mycommand.Parameters["@city"].Value = City;
  34.  
  35.         SqlParameter driverstate = new SqlParameter("@StateID", SqlDbType.Int);
  36.         mycommand.Parameters.Add(driverstate);
  37.         mycommand.Parameters["@StateID"].Value = StateID;
  38.  
  39.         SqlParameter zip = new SqlParameter("@zipcode", SqlDbType.Char);
  40.         mycommand.Parameters.Add(zip);
  41.         mycommand.Parameters["@zipcode"].Value = ZipCode;
  42.  
  43.         SqlParameter cad = new SqlParameter("@cellareacode", SqlDbType.NVarChar);
  44.         mycommand.Parameters.Add(cad);
  45.         mycommand.Parameters["@cellareacode"].Value = CellAreaCode;
  46.  
  47.         SqlParameter cellpref = new SqlParameter("@cellprefix", SqlDbType.NVarChar);
  48.         mycommand.Parameters.Add(cellpref);
  49.         mycommand.Parameters["@cellprefix"].Value = CellPrefix;
  50.  
  51.         SqlParameter cellphon = new SqlParameter("@cellphone", SqlDbType.NVarChar);
  52.         mycommand.Parameters.Add(cellphon);
  53.         mycommand.Parameters["@cellphone"].Value = CellNumber;
  54.  
  55.         SqlParameter hac = new SqlParameter("@homeareacode", SqlDbType.NVarChar);
  56.         mycommand.Parameters.Add(hac);
  57.         mycommand.Parameters["@homeareacode"].Value = HomeAreaCode;
  58.  
  59.         SqlParameter hp = new SqlParameter("@homeprefix", SqlDbType.NVarChar);
  60.         mycommand.Parameters.Add(hp);
  61.         mycommand.Parameters["@homeprefix"].Value = HomePrefix;
  62.  
  63.         SqlParameter homeph = new SqlParameter("@homephone", SqlDbType.NVarChar);
  64.         mycommand.Parameters.Add(homeph);
  65.         mycommand.Parameters["@homephone"].Value = HomePhone;
  66.  
  67.         SqlParameter fac = new SqlParameter("@faxareacode", SqlDbType.NVarChar);
  68.         mycommand.Parameters.Add(fac);
  69.         mycommand.Parameters["@faxareacode"].Value = FaxAreaCode;
  70.  
  71.         SqlParameter faxpref = new SqlParameter("@faxprefix", SqlDbType.NVarChar);
  72.         mycommand.Parameters.Add(faxpref);
  73.         mycommand.Parameters["@faxprefix"].Value = FaxPrefix;
  74.  
  75.         SqlParameter faxnum = new SqlParameter("@fax", SqlDbType.NVarChar);
  76.         mycommand.Parameters.Add(faxnum);
  77.         mycommand.Parameters["@fax"].Value = FaxNumber;
  78.  
  79.         SqlParameter trucknum = new SqlParameter("@trucknumber", SqlDbType.NVarChar);
  80.         mycommand.Parameters.Add(trucknum);
  81.         mycommand.Parameters["@trucknumber"].Value = TruckNumber;
  82.  
  83.         #endregion
  84.  
  85.         //execution of the sql statement.
  86.         try
  87.         {
  88.             conn.Open();
  89.             mycommand.ExecuteNonQuery();
  90.         }
  91.         catch (SqlException ex)
  92.         {
  93.             throw ex;
  94.         }
  95.         finally
  96.         {
  97.             conn.Close();
  98.         }
  99.  
  100.  
  101.  
May 14 '09 #1
5 1631
Plater
7,872 Expert 4TB
You can do a number of things.
Using the SQLParameteres should ahve solved that issues for you.

Otherwise you need to escape the single quote character ' by making it be two SINGLE quote characters '' (Note: Not the same as a double quote " )
May 14 '09 #2
semomaniz
210 Expert 100+
I also thought that using SQLParameters would fix the problem but still i get the same error. Is there some thing that i am missing?
May 14 '09 #3
Plater
7,872 Expert 4TB
Hmm, I've only used them with StoredProcedures, so maybe that's the part that is different.
May 14 '09 #4
semomaniz
210 Expert 100+
so i am guessing using a Sting.Replace("'","''") is my easiest option to get rid of this error.
May 14 '09 #5
Plater
7,872 Expert 4TB
That's what its sounding like.
Remember you have to do it on the value before it goes into your parameters though
May 14 '09 #6

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

Similar topics

8
by: Oeln | last post by:
If I want to check for input of an integer I've got the following (I get the form input with $input = "$_POST"): if(!ereg("^+$",$_POST)) { echo "Input is incomplete or incorrect."; } If,...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
12
by: sam | last post by:
hi all, i'm starting to put together a program to simulate the performance of an investment portfolio in a monte carlo manner doing x thousand iterations and extracting data from the results. ...
5
by: Kavya | last post by:
I saw these two ways for validating input First Way -------------- #include <iostream> #include <limits> using namespace std; int main() {
3
by: Louis | last post by:
I have a form with multiple input boxes. I want to validate each input box (and force user to correct it) before allowing user to move to another, either using tab key or a mouse click. I try...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
9
by: arnuld | last post by:
Earlier, I have posted a program like this, a month ago IIRC. I have created it again, without looking at the old program. Can I have your opinions on this: 1) I wanted my program to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.