Solved

c# replace double quotes

Posted on 2008-10-18
9
5,393 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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