how to add single quotes to string in c#? - c#

I am adding mutliple values to single string , all the values should be like in this format '','' but I am getting "'',''" instead.
How can I remove these double qoutes? Here's the code I'm using:
string one = "\'"+ names[0, 0] +"\'"+","+"\'" + names[1, 0]+"\'";
string[] splitted = one.Split('\'');
string ones = "'" + splitted[1] + "'" +","+ "'" + splitted[3] + "'";

You don't have to escape single quote like "\'" instead you can simply use "'", so you code should be:
string one = "'" + names[0, 0] + "'" + "," + "'" + names[1, 0] + "'";
string[] splitted = one.Split('\'');
string ones = "'" + splitted[1] + "'" + "," + "'" + splitted[3] + "'";

Not sure I fully understand, but there are two ways.
Output: ".","."
var s1 = #"""."","".""";
var s2 = "\".\",\".\"";

The best way to encapsualte escape sequences and special characters within a string is the use of webatim character.
e.g.
String escSeq = "C:\\MyDocuments\\Movie";
can also be stored as:
string escSeq = #"C:\MyDocumens\Movie";
The # character you must put before the string starts and outside the "" characters.

I just worked with your code..it works just fine..
Debugger will show: "'val1','val2'"
Actual Value : 'val1','val2'
From the debugger point you will see "'val1','val2'" but actually it happens for every string values. but actually when you prints the value in a page you will see the actual value as 'val1','val2'
EDIT:
For sending those values to javascript what you can do is just set those values in Hidden fields and then fetch those value from that using document.getElementById('hidden_field').

Related

PowerShell script error "Unexpected token 'h' in expression or statement. The string is missing the terminator:"

I have the following code inside my console application to get a server information using PowerShell:
string PsCmd = "add-pssnapin VMware.VimAutomation.Core; $vCenterServer = '" + vCenterName.Trim() + "';$vCenterAdmin = '" + vCenterUsername.Trim() + "' ;$vCenterPassword = '" + vCenterPassword + "';" + System.Environment.NewLine;
PsCmd = PsCmd + "$VIServer = Connect-VIServer -Server $vCenterServer -User $vCenterAdmin -Password $vCenterPassword;" + System.Environment.NewLine;
PsCmd = PsCmd + "Get-VMHost " + System.Environment.NewLine;
Now the script is working well on the 150 servers we have, but on a single server I am getting this exception:
At line:1 char:131 + ... !*******(********6'*;*****'; + ~
Unexpected token 'h' in expression or statement. At line:1 char:135 +
... **(******';'; + ~~ The string is missing the terminator: '. |
ABC
Which is raising an error on the password variable. And in my case I am getting the password from an external RESP API. Now the password have this format:
\"************
Where it starts with 2 reserved characters \", and the JSON object which I get will be as follow {"PASSWORD":"\"*******"}.. so could the problem be that the password contains reserved set of characters \"?? And how I can fix this?
Now the password have this format:
\"*******
Where it starts with 2 reserved characters \"
If your passwords really come from JSON such as {"PASSWORD":"\"*******"}, your passwords do not start with \" - they start with ", because the \ is simply a JSON escape character needed to embed a literal " in char. in an (always double-quoted) JSON string value.
In other words: If you parse your JSON correctly, the actual password value will be:
"*******
You can verify this as follows:
PS> ('{ "PASSWORD": "\"*******" }' | ConvertFrom-Json).PASSWORD
"*******
Your next problem is that you're embedding the password in a string representing PowerShell source code, and inside that string you're using (embedded) single-quoting ('...') to surround the value[1].
In order to do that, you must escape value-embedded ' instances as '' so as not to cause a syntax error when PowerShell later parses the code:
// Note the .Replace() calls
string PsCmd = "add-pssnapin VMware.VimAutomation.Core; $vCenterServer = '" +
vCenterName.Trim().Replace("'", "''") + "';$vCenterAdmin = '" +
vCenterUsername.Trim().Replace("'", "''") +
"' ;$vCenterPassword = '" + vCenterPassword.Replace("'", "''") + "';" +
System.Environment.NewLine;
With a literal password value of "***'*** (and sample values for the center and username), C# variable PSCmd will receive a string with the following literal contents:
add-pssnapin VMware.VimAutomation.Core; $vCenterServer = 'center1';$vCenterAdmin = 'jdoe' ;$vCenterPassword = '"***''***';
[1] And single quotes are the right choice, because using double quotes could result in unwanted interpretation (string interpolation) of the embedded value by PowerShell.
You need to escape the ". You do this in PowerShell using the ` character, e.g. `"
Try this:
string PsCmd = "Add-PSSnapin VMware.VimAutomation.Core; $vCenterServer = '" + vCenterName.Trim() + "'; $vCenterAdmin = '" + vCenterUsername.Trim() + "'; $vCenterPassword = \"" + vCenterPassword.Replace("\"", "`\"") + "\";" + System.Environment.NewLine;

How to get multiple wildcard % in the middle of a DataView RowFilter same as sql

I have this row filter text:
"[add1] = '" + startAddress+ "%" + middle+ "%" + end +"'"
which fails, but if I put the % at the beginning or end it's OK.
Is there any way to achieve the same result (i.e. "any" string in the middle of the names)?
Full statement is:
dv = new DataView(MyDataTable, "[add1] = '" + startAddress+ "%" + middle+ "%" + end + + "'", "", DataViewRowState.CurrentRows);
Here i want filteration like "%something%someone%" How can i achieve this? Even using linq is also ok with me

Double quotes with Sql Update

I'm trying to update a database with OleDb and .Net4.5.
My updates are working good , even if i use simple quote on a filed, but, when i input a double quote on a field, oledb raise an exception because of this double quote.
Here is a an example of a request :
string strRequest = "update " + strNomTable.Trim() + " set "
+ "evenotes = " + '"' + m_strNote.ToString().Trim() + '"'
+ " where eveNum = " + '"' + strEvtNumeroString.Trim() + '"';
Have you an idea how i could avoid simple and double quotes ?
Note : I tried to use SQL Parametrized updates, bu my DataBase don't appear to support this.
Thanks a lot,
Best regards,
Nixeus
A few options come to mind. Since I do not know what kind of database you are using, I am just guessing:
Use parameters. I know you have tried it, but I would suggest to try again. If it failes, try the following:
Remove the comma for the "where" (one line up!).
Change all your double quotes inside your SQL-statement into single quotes. Literal text should be quotes like "'" + m_strNote.ToString().Trim() + "'" and not '"' + m_strNote.ToString().Trim() + '"'
Replace all single single quotes (') in your values with double single quotes (''): "'" + m_strNote.ToString().Trim().Replace("'", "''") + "'"
If you combine option 2 till 4 you will get this:
string strRequest = "update " + strNomTable.Trim() + " set "
+ "evenotes = '" + m_strNote.ToString().Trim().Replace("'", "''") + "' "
+ "where eveNum = '" + strEvtNumeroString.Trim().Replace("'", "''") + "'";
Visual Fox Pro Database and OleDbParameters
You can use OleDbParameters. Start the name with an #. So:
OleDbCommand command = new OleDbCommand(
"update " + strNomTable.Trim() + " set "
+ "evenotes = #evenotes "
+ "where eveNum = #eveNum");
command.Parameters.AddWithValue("#evenotes", m_strNote.ToString().Trim());
command.Parameters.AddWithValue("#eveNum", strEvtNumeroString.Trim());

C# mysql parameters, point column error

Using the code I keep getting the error "Cannot get geometry object from data you send to the GEOMETRY field" However, if i copy and paste the string into an MySQL editor the query runs fine. Any thoughts?
string geoPOINTSTRING = splitZippy[4] + " " + splitZippy[5];
string atGvar = "GeomFromText('Point(" + geoPOINTSTRING + ")');";
string mySQLfinishedProcessing = " insert into zipcodes " +
"set zipcode = '" + zipcodeString + "'" +
",State = '" + StateString + "'" +
",City = '" + CityString + "'" +
",GEOPoint = #g"+
",StateCode = '" + StateCodeString2 + "'";
MySqlConnection configCON = new MySqlConnection(SQLStringClass.zipCONString);
MySqlCommand CounterLogs = new MySqlCommand(mySQLfinishedProcessing, configCON);
CounterLogs.Parameters.AddWithValue("#g",(string)atGvar);
configCON.Open();
CounterLogs.ExecuteNonQuery();
configCON.Close();
You are completely misusing parameters.
A parameter is a raw value.
You're setting GEOPoint to the literal string GeomFromText('Point(something)');
You need to put the actual values in parameters—zipcodeString, StateString, CityString, geoPOINTSTRING, and StateCodeString2.
You would then write GEOPoint = GeomFromText(#pointString), where pointString is a parameter holding "Point(" + geoPOINTSTRING + ")" (The string you want to pass to GeomFromText)

Tables.Select on Unicode Characters

DataRow[] rows = myDataSet.Tables[0].Select("name = '" + _string + "'");
If _string contains UNICODE characters, do I need to prefix it with N like in TSQL?
DataRow[] rows = myDataSet.Tables[0].Select("name = N'" + _string + "'");
Thanks!
DataRow[] rows = myDatatable.Select("name = '" + _string + "'");
_string includes Myanmar Unicode string and wrong result returns(i.e. it returned all strings that include Myanmar Characters).
What's wrong with me?
I also tried
myDatatable.DefaultView.RowFilter("name = '" + _string + "'");
the same wrong result return.
I tested it. Adding an N-Prefix will throw an exception.
You don't need to put the N-Prefix even if you are working with Unicode characters.

Categories

Resources