Solved

c# replace double quotes

Posted on 2008-10-18
9
5,389 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 43
Facial recognition to look through the whole database for a person 3 69
Device Location 4 30
Sent email using Shared Mailbox from outlook in C# 5 25
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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