Solved

c# replace double quotes

Posted on 2008-10-18
9
5,402 Views
Last Modified: 2011-09-27
I searched hundreds of entries and tried tons of examples - I cannot find how to replace double quotes so my sql insert will run.
This runs for single quotes:
memberID = memberID.Replace("'","''");
so I need the same thing for double quotes.

Also, does a replace work on all the characters in the string or just once?
0
Comment
Question by:dcass
[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
9 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 22750737
To replace double quotes:

memberID = memberID.Replace("\"","other");

Replace method will replace all ocurrences
0
 

Author Comment

by:dcass
ID: 22750764
Will this show the double quotes back in the text like the single quote solution will?
0
 

Author Comment

by:dcass
ID: 22750774
What other characters will interfere with an insert?
How about ampersand?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:PedroBuhigas
ID: 22750781
I think what you're after is:
memberID = memberID.Replace("\"\"","other");
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22750785
I think you just have to worry about the single quote, not double quote or other.
I use to replace the single quote with a tick (`):

memberID = memberID.Replace("'","`");
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22750801
but if you want to avoid to use all these, better you use parameters instead.
have a look to:
http://www.codeproject.com/KB/database/SqlInjectionAttacks.aspx
read "Parameterised Queries" section.
0
 
LVL 6

Expert Comment

by:carlsiy
ID: 22751204
why not use....
memberID=@memberID
0
 
LVL 23

Assisted Solution

by:Tony McCreath
Tony McCreath earned 250 total points
ID: 22751502
I developed the following code to resolve encoding problems when directly using quoted text in mySql commands.
private static Regex _FromToRegex = new Regex(@"([\\\n\r\b\t'""\x1a\x00])",RegexOptions.Compiled);
 
private static string FromToMatchEvaluator(Match match)
{
	switch (match.Value)
	{
		case "\\":
			return "\\\\";
		case "\n":
			return "\\n";
		case "\r":
			return "\\r";
		case "\b":
			return "\\b";
		case "\t":
			return "\\t";
		case "\x1a":
			return "\\Z";
		case "\x00":
			return "\\0";
		case "'":
			return "\\'"; 
		case "\"":
			return "\\\"";
		default:
			return match.Value;
	}
}
 
public static string SqlEscapes(string s)
{
	return _FromToRegex.Replace(s,new MatchEvaluator(FromToMatchEvaluator));
}
 
public static string SqlString(String str)
{
	return "'"+SqlEscapes(str)+"'";
}

Open in new window

0
 

Expert Comment

by:rochefc
ID: 36709359
To replace Single Quotes, Double Quotes or Carriage Returns for use with Crystal Reports in C# try
 
fieldValue = fieldValue.Replace("\r\n", "' & chr(010) & '").Replace("'", "''").Replace("\"", "''''");

This allows the Crystal Report to Print with the desired result
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 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