Solved

c# replace double quotes

Posted on 2008-10-18
9
5,383 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
 

Expert Comment

by:PedroBuhigas
ID: 22750781
I think what you're after is:
memberID = memberID.Replace("\"\"","other");
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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:Tiggerito
Tiggerito 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now