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. -
-
string strSql = " Update Drivers SET CarrierID =@carrierid, LastName =@lastname, FirstName =@firstname, Address =@address, City =@city," +
-
"StateID =@stateid, ZipCode =@zipcode, CellAreaCode =@cellareacode, CellPrefix =@cellprefix, CellPhone =@cellphone, "+
-
"HomeAReaCode =@homeareacode, HomePrefix =@homeprefix, HomePhone =@homephone, FaxAreaCode =@faxareacode, FaxPrefix =@faxprefix," +
-
"Fax =@fax, TruckNumber =@trucknumber where driverid = " + DriverID;
-
-
SqlConnection conn = new SqlConnection();
-
String constr = ConfigurationManager.ConnectionStrings[("USBSRVR")].ConnectionString;
-
conn = new SqlConnection(constr);
-
SqlCommand mycommand = new SqlCommand(strSql, conn);
-
-
//adding sqlparameters
-
#region parameters
-
-
SqlParameter carrier = new SqlParameter("@carrierid", SqlDbType.Int);
-
mycommand.Parameters.Add(carrier);
-
mycommand.Parameters["@carrierid"].Value = CarrierID;
-
-
SqlParameter Last = new SqlParameter("@lastname", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(Last);
-
mycommand.Parameters["@lastname"].Value = LastName;
-
-
SqlParameter First = new SqlParameter("@firstname", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(First);
-
mycommand.Parameters["@firstname"].Value = FirstName;
-
-
SqlParameter addres = new SqlParameter("@address", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(addres);
-
mycommand.Parameters["@address"].Value = Address;
-
-
SqlParameter drivercity = new SqlParameter("@city", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(drivercity);
-
mycommand.Parameters["@city"].Value = City;
-
-
SqlParameter driverstate = new SqlParameter("@StateID", SqlDbType.Int);
-
mycommand.Parameters.Add(driverstate);
-
mycommand.Parameters["@StateID"].Value = StateID;
-
-
SqlParameter zip = new SqlParameter("@zipcode", SqlDbType.Char);
-
mycommand.Parameters.Add(zip);
-
mycommand.Parameters["@zipcode"].Value = ZipCode;
-
-
SqlParameter cad = new SqlParameter("@cellareacode", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(cad);
-
mycommand.Parameters["@cellareacode"].Value = CellAreaCode;
-
-
SqlParameter cellpref = new SqlParameter("@cellprefix", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(cellpref);
-
mycommand.Parameters["@cellprefix"].Value = CellPrefix;
-
-
SqlParameter cellphon = new SqlParameter("@cellphone", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(cellphon);
-
mycommand.Parameters["@cellphone"].Value = CellNumber;
-
-
SqlParameter hac = new SqlParameter("@homeareacode", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(hac);
-
mycommand.Parameters["@homeareacode"].Value = HomeAreaCode;
-
-
SqlParameter hp = new SqlParameter("@homeprefix", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(hp);
-
mycommand.Parameters["@homeprefix"].Value = HomePrefix;
-
-
SqlParameter homeph = new SqlParameter("@homephone", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(homeph);
-
mycommand.Parameters["@homephone"].Value = HomePhone;
-
-
SqlParameter fac = new SqlParameter("@faxareacode", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(fac);
-
mycommand.Parameters["@faxareacode"].Value = FaxAreaCode;
-
-
SqlParameter faxpref = new SqlParameter("@faxprefix", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(faxpref);
-
mycommand.Parameters["@faxprefix"].Value = FaxPrefix;
-
-
SqlParameter faxnum = new SqlParameter("@fax", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(faxnum);
-
mycommand.Parameters["@fax"].Value = FaxNumber;
-
-
SqlParameter trucknum = new SqlParameter("@trucknumber", SqlDbType.NVarChar);
-
mycommand.Parameters.Add(trucknum);
-
mycommand.Parameters["@trucknumber"].Value = TruckNumber;
-
-
#endregion
-
-
//execution of the sql statement.
-
try
-
{
-
conn.Open();
-
mycommand.ExecuteNonQuery();
-
}
-
catch (SqlException ex)
-
{
-
throw ex;
-
}
-
finally
-
{
-
conn.Close();
-
}
-
-
-
5 1631
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 " )
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?
Hmm, I've only used them with StoredProcedures, so maybe that's the part that is different.
so i am guessing using a Sting.Replace("'","''") is my easiest option to get rid of this error.
That's what its sounding like.
Remember you have to do it on the value before it goes into your parameters though
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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...
|
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...
|
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.
...
|
by: Kavya |
last post by:
I saw these two ways for validating input
First Way
--------------
#include <iostream>
#include <limits>
using namespace std;
int main() {
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |