PC Review


Reply
Thread Tools Rate Thread

Data with Apostrophes

 
 
Carl Thomas
Guest
Posts: n/a
 
      11th Aug 2007
I have names in a data collection the include apostrophes like O'Hare. When
I use a simple VC#/ADO.NET approach with an SQL INSERT statement and
cmd.ExecuteNonQuery, it always hangs up on the apostrophe saying that there
is an unterminated string. When I try using a DataAdapter and
SqlCommandBuilder, it says I need and INSERT statement, although it will do
data without the apostrophes. Any idea how to handle these cases? I've
also tried parameters, but the results are the same.

TIA

Carl


 
Reply With Quote
 
 
 
 
=?Utf-8?B?S2VycnkgTW9vcm1hbg==?=
Guest
Posts: n/a
 
      11th Aug 2007
Carl,

Can you post your code where you use cmd.ExecuteNonQuery with parameters and
you still have problems?

Kerry Moorman


"Carl Thomas" wrote:

> I have names in a data collection the include apostrophes like O'Hare. When
> I use a simple VC#/ADO.NET approach with an SQL INSERT statement and
> cmd.ExecuteNonQuery, it always hangs up on the apostrophe saying that there
> is an unterminated string. When I try using a DataAdapter and
> SqlCommandBuilder, it says I need and INSERT statement, although it will do
> data without the apostrophes. Any idea how to handle these cases? I've
> also tried parameters, but the results are the same.
>
> TIA
>
> Carl
>
>
>

 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      11th Aug 2007
On Sat, 11 Aug 2007 18:02:02 +0200, Carl Thomas <(E-Mail Removed)> wrote:

> I have names in a data collection the include apostrophes like O'Hare. When
> I use a simple VC#/ADO.NET approach with an SQL INSERT statement and
> cmd.ExecuteNonQuery, it always hangs up on the apostrophe saying that there
> is an unterminated string. When I try using a DataAdapter and
> SqlCommandBuilder, it says I need and INSERT statement, although it will do
> data without the apostrophes. Any idea how to handle these cases? I've
> also tried parameters, but the results are the same.
>
> TIA
>
> Carl
>
>
>


Carl, use SqlParameter to pass the data. Saves you all these kinds of troubles.

--
Happy coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
Carl Thomas
Guest
Posts: n/a
 
      12th Aug 2007
The problem occurs at runtime from the following code snippet:

using (SqlDataReader drdr =
cmdIn.ExecuteReader())
{
drdr.Read();
string latitude =
GetCoordinateString( drdr.GetString( 1 ) );
string longitude =
GetCoordinateString( drdr.GetString( 2 ) );
string strInsertString
= "INSERT INTO
AirportData (ICAO, Latitude, Longitude, Altitude,"

+ " AirportName, CityName,"

+ " StateProvinceName, CountryName)"
+ "VALUES ('" +
drdr.GetString( 0 ) + "', '"
+
latitude + " ', '"
+
longitude + " ', '"
+
drdr.GetString( 3 ) + "', '"
+
drdr.GetString( 4 ) + "', '"
+
drdr.GetString( 5 ) + "', '"
+
drdr.GetString( 6 ) + "', '"
+
drdr.GetString( 7 ) + "')";
using (SqlCommand cmdInsert
= new SqlCommand( strInsertString, cnOut ))
{
cmdInsert.ExecuteNonQuery();
}
}
When it hits O'Hare as once instance, it does an SqlException at
ExecuteNonQuery claiming that there is an unterminated string. I have since
found a way around it that blythly ignores the problem. I've broken the
problem up to retrieve the data from one database using an SqlDataReader and
out it into a class list and then separately reading it from a class list
into an SqlDataAdapter for the insert. It is a little less elegent, but
works without a problem.

Carl

"Kerry Moorman" <(E-Mail Removed)> wrote in message
news:BB72B829-BA04-4BA8-8E3D-(E-Mail Removed)...
> Carl,
>
> Can you post your code where you use cmd.ExecuteNonQuery with parameters
> and
> you still have problems?
>
> Kerry Moorman
>
>
> "Carl Thomas" wrote:
>
>> I have names in a data collection the include apostrophes like O'Hare.
>> When
>> I use a simple VC#/ADO.NET approach with an SQL INSERT statement and
>> cmd.ExecuteNonQuery, it always hangs up on the apostrophe saying that
>> there
>> is an unterminated string. When I try using a DataAdapter and
>> SqlCommandBuilder, it says I need and INSERT statement, although it will
>> do
>> data without the apostrophes. Any idea how to handle these cases? I've
>> also tried parameters, but the results are the same.
>>
>> TIA
>>
>> Carl
>>
>>
>>



 
Reply With Quote
 
Carl Thomas
Guest
Posts: n/a
 
      12th Aug 2007
I haven't tried SqlParameters since I found a work around. I may give it a
try since my solution was less elegent than I'd like (see my reply to Kerry
Moorman's post). Somehow, I've never gotten around to SqlParameters, but it
might be a nice application to try.

Thanx.

Carl

"Morten Wennevik" <(E-Mail Removed)> wrote in message
newsp.twwpsdsdklbvpo@ubuan...
> On Sat, 11 Aug 2007 18:02:02 +0200, Carl Thomas
> <(E-Mail Removed)> wrote:
>
>> I have names in a data collection the include apostrophes like O'Hare.
>> When
>> I use a simple VC#/ADO.NET approach with an SQL INSERT statement and
>> cmd.ExecuteNonQuery, it always hangs up on the apostrophe saying that
>> there
>> is an unterminated string. When I try using a DataAdapter and
>> SqlCommandBuilder, it says I need and INSERT statement, although it will
>> do
>> data without the apostrophes. Any idea how to handle these cases? I've
>> also tried parameters, but the results are the same.
>>
>> TIA
>>
>> Carl
>>
>>
>>

>
> Carl, use SqlParameter to pass the data. Saves you all these kinds of
> troubles.
>
> --
> Happy coding!
> Morten Wennevik [C# MVP]



 
Reply With Quote
 
=?Utf-8?B?S2VycnkgTW9vcm1hbg==?=
Guest
Posts: n/a
 
      12th Aug 2007
Carl,

I don't see any use of parameters, as you mentioned in your first post.

Kerry Moorman


"Carl Thomas" wrote:

> The problem occurs at runtime from the following code snippet:
>
> using (SqlDataReader drdr =
> cmdIn.ExecuteReader())
> {
> drdr.Read();
> string latitude =
> GetCoordinateString( drdr.GetString( 1 ) );
> string longitude =
> GetCoordinateString( drdr.GetString( 2 ) );
> string strInsertString
> = "INSERT INTO
> AirportData (ICAO, Latitude, Longitude, Altitude,"
>
> + " AirportName, CityName,"
>
> + " StateProvinceName, CountryName)"
> + "VALUES ('" +
> drdr.GetString( 0 ) + "', '"
> +
> latitude + " ', '"
> +
> longitude + " ', '"
> +
> drdr.GetString( 3 ) + "', '"
> +
> drdr.GetString( 4 ) + "', '"
> +
> drdr.GetString( 5 ) + "', '"
> +
> drdr.GetString( 6 ) + "', '"
> +
> drdr.GetString( 7 ) + "')";
> using (SqlCommand cmdInsert
> = new SqlCommand( strInsertString, cnOut ))
> {
> cmdInsert.ExecuteNonQuery();
> }
> }
> When it hits O'Hare as once instance, it does an SqlException at
> ExecuteNonQuery claiming that there is an unterminated string. I have since
> found a way around it that blythly ignores the problem. I've broken the
> problem up to retrieve the data from one database using an SqlDataReader and
> out it into a class list and then separately reading it from a class list
> into an SqlDataAdapter for the insert. It is a little less elegent, but
> works without a problem.
>
> Carl
>
> "Kerry Moorman" <(E-Mail Removed)> wrote in message
> news:BB72B829-BA04-4BA8-8E3D-(E-Mail Removed)...
> > Carl,
> >
> > Can you post your code where you use cmd.ExecuteNonQuery with parameters
> > and
> > you still have problems?
> >
> > Kerry Moorman
> >
> >
> > "Carl Thomas" wrote:
> >
> >> I have names in a data collection the include apostrophes like O'Hare.
> >> When
> >> I use a simple VC#/ADO.NET approach with an SQL INSERT statement and
> >> cmd.ExecuteNonQuery, it always hangs up on the apostrophe saying that
> >> there
> >> is an unterminated string. When I try using a DataAdapter and
> >> SqlCommandBuilder, it says I need and INSERT statement, although it will
> >> do
> >> data without the apostrophes. Any idea how to handle these cases? I've
> >> also tried parameters, but the results are the same.
> >>
> >> TIA
> >>
> >> Carl
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      13th Aug 2007
On Sun, 12 Aug 2007 23:13:11 +0200, Carl Thomas <(E-Mail Removed)> wrote:

> I haven't tried SqlParameters since I found a work around. I may give it a
> try since my solution was less elegent than I'd like (see my reply to Kerry
> Moorman's post). Somehow, I've never gotten around to SqlParameters, but it
> might be a nice application to try.
>
> Thanx.
>
> Carl
>
> "Morten Wennevik" <(E-Mail Removed)> wrote in message
> newsp.twwpsdsdklbvpo@ubuan...
>> On Sat, 11 Aug 2007 18:02:02 +0200, Carl Thomas
>> <(E-Mail Removed)> wrote:
>>
>>> I have names in a data collection the include apostrophes like O'Hare.
>>> When
>>> I use a simple VC#/ADO.NET approach with an SQL INSERT statement and
>>> cmd.ExecuteNonQuery, it always hangs up on the apostrophe saying that
>>> there
>>> is an unterminated string. When I try using a DataAdapter and
>>> SqlCommandBuilder, it says I need and INSERT statement, although it will
>>> do
>>> data without the apostrophes. Any idea how to handle these cases? I've
>>> also tried parameters, but the results are the same.
>>>
>>> TIA
>>>
>>> Carl
>>>
>>>
>>>

>>
>> Carl, use SqlParameter to pass the data. Saves you all these kinds of
>> troubles.
>>
>> --
>> Happy coding!
>> Morten Wennevik [C# MVP]

>
>
>


You really should give SqlParameter/OleDBParameter a try, apart from handling strings with ' they also to behind the scenes data conversion that potentially saves a whole lot of grief if you get trouble writing or reading database data.

--
Happy coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
Carl Thomas
Guest
Posts: n/a
 
      13th Aug 2007
Right - they didn't help anything so I kept the simpler approach. I realize
it would be easier to take from one database and copy individual items into
another, but using an intermediary array has worked in all cases so far and
is transparent, if a little more complex. Maybe I was using the parameters
wrong; I'll check it out again.

Carl

"Kerry Moorman" <(E-Mail Removed)> wrote in message
news:CFE871FB-11B3-4A8D-8A0E-(E-Mail Removed)...
> Carl,
>
> I don't see any use of parameters, as you mentioned in your first post.
>
> Kerry Moorman
>
>
> "Carl Thomas" wrote:
>
>> The problem occurs at runtime from the following code snippet:
>>
>> using (SqlDataReader drdr =
>> cmdIn.ExecuteReader())
>> {
>> drdr.Read();
>> string latitude =
>> GetCoordinateString( drdr.GetString( 1 ) );
>> string longitude =
>> GetCoordinateString( drdr.GetString( 2 ) );
>> string strInsertString
>> = "INSERT INTO
>> AirportData (ICAO, Latitude, Longitude, Altitude,"
>>
>> + " AirportName, CityName,"
>>
>> + " StateProvinceName, CountryName)"
>> + "VALUES ('" +
>> drdr.GetString( 0 ) + "', '"
>> +
>> latitude + " ', '"
>> +
>> longitude + " ', '"
>> +
>> drdr.GetString( 3 ) + "', '"
>> +
>> drdr.GetString( 4 ) + "', '"
>> +
>> drdr.GetString( 5 ) + "', '"
>> +
>> drdr.GetString( 6 ) + "', '"
>> +
>> drdr.GetString( 7 ) + "')";
>> using (SqlCommand
>> cmdInsert
>> = new SqlCommand( strInsertString, cnOut ))
>> {
>>
>> cmdInsert.ExecuteNonQuery();
>> }
>> }
>> When it hits O'Hare as once instance, it does an SqlException at
>> ExecuteNonQuery claiming that there is an unterminated string. I have
>> since
>> found a way around it that blythly ignores the problem. I've broken the
>> problem up to retrieve the data from one database using an SqlDataReader
>> and
>> out it into a class list and then separately reading it from a class list
>> into an SqlDataAdapter for the insert. It is a little less elegent, but
>> works without a problem.
>>
>> Carl
>>
>> "Kerry Moorman" <(E-Mail Removed)> wrote in message
>> news:BB72B829-BA04-4BA8-8E3D-(E-Mail Removed)...
>> > Carl,
>> >
>> > Can you post your code where you use cmd.ExecuteNonQuery with
>> > parameters
>> > and
>> > you still have problems?
>> >
>> > Kerry Moorman
>> >
>> >
>> > "Carl Thomas" wrote:
>> >
>> >> I have names in a data collection the include apostrophes like O'Hare.
>> >> When
>> >> I use a simple VC#/ADO.NET approach with an SQL INSERT statement and
>> >> cmd.ExecuteNonQuery, it always hangs up on the apostrophe saying that
>> >> there
>> >> is an unterminated string. When I try using a DataAdapter and
>> >> SqlCommandBuilder, it says I need and INSERT statement, although it
>> >> will
>> >> do
>> >> data without the apostrophes. Any idea how to handle these cases?
>> >> I've
>> >> also tried parameters, but the results are the same.
>> >>
>> >> TIA
>> >>
>> >> Carl
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
Carl Thomas
Guest
Posts: n/a
 
      13th Aug 2007
Actually, I misspoke - I did try the parameter approach, but it didn't help.
Maybe I was using them wrong, but separating the read operations from one
database and the write operations into a second one through an intermediate
array worked. If it was slightly less elegent and complex, it worked. I'll
give the parametric approach another go to help trim the code a bit.

Carl

"Carl Thomas" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I haven't tried SqlParameters since I found a work around. I may give it a
>try since my solution was less elegent than I'd like (see my reply to Kerry
>Moorman's post). Somehow, I've never gotten around to SqlParameters, but
>it might be a nice application to try.
>
> Thanx.
>
> Carl
>
> "Morten Wennevik" <(E-Mail Removed)> wrote in message
> newsp.twwpsdsdklbvpo@ubuan...
>> On Sat, 11 Aug 2007 18:02:02 +0200, Carl Thomas
>> <(E-Mail Removed)> wrote:
>>
>>> I have names in a data collection the include apostrophes like O'Hare.
>>> When
>>> I use a simple VC#/ADO.NET approach with an SQL INSERT statement and
>>> cmd.ExecuteNonQuery, it always hangs up on the apostrophe saying that
>>> there
>>> is an unterminated string. When I try using a DataAdapter and
>>> SqlCommandBuilder, it says I need and INSERT statement, although it will
>>> do
>>> data without the apostrophes. Any idea how to handle these cases?
>>> I've
>>> also tried parameters, but the results are the same.
>>>
>>> TIA
>>>
>>> Carl
>>>
>>>
>>>

>>
>> Carl, use SqlParameter to pass the data. Saves you all these kinds of
>> troubles.
>>
>> --
>> Happy coding!
>> Morten Wennevik [C# MVP]

>
>



 
Reply With Quote
 
=?Utf-8?B?VG9tIEdhcnRo?=
Guest
Posts: n/a
 
      14th Aug 2007
All that you have to do is double up on them. Sinding "O''Hare" will actually
send "O'Hare".
--
Tom Garth


"Carl Thomas" wrote:

> I have names in a data collection the include apostrophes like O'Hare. When
> I use a simple VC#/ADO.NET approach with an SQL INSERT statement and
> cmd.ExecuteNonQuery, it always hangs up on the apostrophe saying that there
> is an unterminated string. When I try using a DataAdapter and
> SqlCommandBuilder, it says I need and INSERT statement, although it will do
> data without the apostrophes. Any idea how to handle these cases? I've
> also tried parameters, but the results are the same.
>
> TIA
>
> Carl
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dealing with Apostrophes in my data -- Access 2007 VBA LA Lawyer Microsoft Access Form Coding 2 11th Feb 2010 02:22 AM
Re: Need Help Handling Apostrophes with New Data Klatuu Microsoft Access Form Coding 3 9th Oct 2008 03:18 PM
apostrophes teepee Microsoft Excel Discussion 21 14th Feb 2008 01:05 AM
Apostrophes in table data =?Utf-8?B?RGVubmlz?= Microsoft Access Form Coding 2 14th Sep 2007 02:03 AM
More apostrophes!! Anthony Speiser Microsoft Access Form Coding 6 24th Mar 2007 07:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:53 PM.