Converting characters to ASCII code - vb.net

Need help with reading special characters within my VB code. ASCII code Char(34) = " works fine but Char(60) = < and Char(62) = > are not being read.
My Code
node.FirstChild.InnerText = Chr(60) & "httpRuntime executionTimeout=" & Chr(34) & "999999" & Chr(34) & " maxRequestLength=" & Chr(34) & "2097151" & Chr(34) & "/" & Chr(62)
Without ASCII Code
'node.FirstChild.InnerText = "<httpRuntime executionTimeout="999999" maxRequestLength="2097151"/>"

Are you trying to modify a Config file? Try:-
node.FirstChild.InnerXml = "<httpRuntime executionTimeout=""999999"" maxRequestLength=""2097151"" />"
Note all that Chr marlarky is unnecessary, were you trying to avoid < and > being encoded as XML entities?

Maybe this doesn't answer your question, but you could use two double quotes to escape the quotes character in VB.NET:
node.FirstChild.InnerText = _
"<httpRuntime executionTimeout=""999999"" maxRequestLength=""2097151"" />"
I'm just guessing: you could use the String.Format method for your purposes:
node.FirstChild.InnerText = _
String.Format( _
"<httpRuntime executionTimeout=""{0}"" maxRequestLength=""{1}"" />", _
timeoutValue.ToString(), reqLenValue.ToString())

You'll need to give more information about how you're "seeing" the results. In my experience, problems with this are as likely to be about viewing strings in the debugger as getting the right strings in the first place.
I don't really see why you need to use Chr(60) etc at all, other than for the quotes. What happens when you just use < and > in your code?
I strongly suggest you dump the string out to the console rather than using the debugger - the debugger tries to show you how you could represent the string in code, rather than showing you the contents verbatim.
Of course, if this is XML then I'd expect serializing the XML out again to end up escaping the < and > - again, more information about what you're trying to do would be helpful. The absolute ideal (IMO) would be a short but complete program demonstrating the problem - a small console app which does one thing, and a description of what you want it to do instead.

Related

Dsum function not working with Text field

I've tried just about everything i can think of on why i would get this error, but i have had no luck. I wrote a similar code that references that same table with numerical values that works fine, but when searching for text it has problems. The error code says the missing operator lies here: [ExpendetureStore] = 'Lowe's
TotalCostTextBox = DSum("[ExpendetureCost]", "ProjectExpendetures", "[ExpendetureStore] = '" & Me.StoreNameCombo & "'")
Lowe's has an apostrophe in its name. Access query engine is reading that apostrophe as a special character (text delimiter) in the compiled search string. If your data includes apostrophes, one way to deal with is to 'escape' the character - double it in the data with Replace() function. This forces Access to treat the character as normal text.
TotalCostTextBox = DSum("[ExpendetureCost]", "ProjectExpendetures", "[ExpendetureStore] = '" & Replace(Me.StoreNameCombo, "'", "''") & "'")
The same will happen with quote marks and are more challenging to deal with. Note the escaping of quote between quotes.
Replace("somevalue", """", """" & """")
Or may be easier to understand using Chr() function.
Replace("somevalue", Chr(34), Chr(34) & Chr(34))
Side note: Expendeture is a misspelling of Expenditure.

Db Search - Multiple conditions

I am trying to export Documents from a Lotus DB. I have used the Db.search functionality and arrived at below code. However, I want to include 2 conditions/functions - #Contains & #Created together. I am getting Formula error. Any help is much appreciated.
Set GlobalCollection = db.Search("#Created > [01/01/2019]" & " " & "#Contains(" & "App1" & ";" & """Approved""" & ")", Nothing, 0)
The escape symbol for LotusScript is a backslash, \. LotusScript allows you to use more than just double quotes to wrap Strings. You can use curly braces ({...}) or pipes (|...|). This may make it more readable and easier to trubleshoot. There's also no need to have separate strings for each individual piece, which will again minimise risk and help readability. There may have been a mistake with each of those, I'm pretty sure you're missing an ampersand. It's much easier to troubleshoot with fewer strings.
So this should work:
Set GlobalCollection = db.Search({#Created > [01/01/2019] & #Contains(App1;"Approved")}, Nothing, 0)

Replace a String containing a "

I have run into a problem trying to get this code to work:
filereader.Replace(Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000000", Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000001")
I want it to replace a string which is !exaclty! looking like this, containing the quotation marks:
"SomeSetting"=dword:00000000
but what it does is looking for this string:
""SomeSetting""=dword:00000000
and i cant get it to work. Even if i tried this:
Dim Test As String = Chr(34)
Test will look like this:
""
instead of "
what am i missing here?
I think I see your problem... This is a quite common, accidental thing for people to do.
Strings are immutable, which means that once you've created them they cannot be changed without creating a new string instance.
The problem is this:
filereader.Replace(Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000000", Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000001")
The Replace() function returns the new string with the replaced value(s) (since it cannot change the original one), but you never use the instance it returns.
You should set your old string variable to the new string returned by Replace(), like this:
filereader = filereader.Replace(Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000000", Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000001")
To avoid (or at least minimize the risk of) things like this happening, make sure you read the information that Visual Studio's IntelliSense shows you when writing the function call.
If you do bump into problems anyway, make sure to check the MSDN documentation to see if you missed anything. They usually also have examples showing how you can use the methods.

Using a Dlookup in Access VBA when the field has a single quote

I've found a lot on this topic but still can't seem to get it to work. I have the following line of code:
If isNull(DLookup("id", my_table, my_field & "='" & temp_value & "'")) Then
The problem is a value in my_field of my_table is "O'Connell" (with a single quote), and I'm not sure how to get Dlookup to find it. I've tried using:
my_field & "=" & chr(34) & temp_value & chr(34)
And a host of other multi-quote options, but I just can't seem to get it to work. Though I can use VBA to modify the temp_value to include or not include the single quote, since the single quote already exists in the table, I need to make sure it matches. I'm just not sure how to tackle it.
Though the suggestions and answers here do work and resolve many issues with quotes in text, my issue ended up being related to the character I was seeing as a single quote not really being a single quote. For what it's worth, the data I was using was exported from Siebel, and the single quote I was seeing was actually chr(146), where a regular single quote (I say "regular" for lack of a better term) is chr(39).
If having issues with quotes, I found it helpful to examine the chr values of each character in the string. There may be a better way to do this, but this loop should help:
for i=1 to len(a_string)
debug.print mid(a_string,i,1) & " - " & asc(mid(a_string,i,1)
next i
The asc function gives you the chr code for a character, so this loops through the string and shows you each character and its associated chr code in the Immediate window (using debug.print). This also helps in finding other "hidden" (or non-visible) characters that may exist in a string.
Once discovered, I used the replace function to replace chr(146) with two single quotes (two chr(39)s), as suggested by HansUp, and that worked perfectly.
In this example, my_table is the name of my table and my_field is the name of a field in that table.
Dim strCriteria As String
Dim temp_value As String
temp_value = "O'Connell"
' use double instead of single quotes to avoid a '
' problem due to the single quote in the name '
strCriteria = "my_field = """ & temp_value & """"
Debug.Print strCriteria
If IsNull(DLookup("id", "my_table", strCriteria)) Then
MsgBox "no id found"
Else
MsgBox "id found"
End If
If you prefer, you can double up the single quotes within the name. This should work, but make sure you can distinguish between which are double and which are single quotes.
strCriteria = "my_field='" & Replace(temp_value, "'", "''") & "'"

New line character in VB.Net?

I am trying to print a message on a web page in vb.net. I am trying to get the messages in new lines. I tried using the "\r\n" and the new line character. But this is getting printed in the page instead of it comming to the next line. Please let me know if there is any alternative.
Check out Environment.NewLine. As for web pages, break lines with <br> or <p></p> tags.
Environment.NewLine is the most ".NET" way of getting the character, it will also emit a carriage return and line feed on Windows and just a carriage return in Unix if this is a concern for you.
However, you can also use the VB6 style vbCrLf or vbCr, giving a carriage return and line feed or just a carriage return respectively.
The proper way to do this in VB is to use on of the VB constants for newlines. The main three are
vbCrLf = "\r\n"
vbCr = "\r"
vbLf = "\n"
VB by default doesn't allow for any character escape codes in strings which is different than languages like C# and C++ which do. One of the reasons for doing this is ease of use when dealing with file paths.
C++ file path string: "c:\\foo\\bar.txt"
VB file path string: "c:\foo\bar.txt"
C# file path string: C++ way or #"c:\foo\bar.txt"
You need to use HTML on a web page to get line breaks. For example "<br/>" will give you a line break.
If you are using something like this.
Response.Write("Hello \r\n")
Response.Write("World \r\n")
and the output is
Hello\r\nWorld\r\n
Then you are basically looking for something like this
Response.Write("Hello <br/>")
Response.Write("World <br/>")
This will output
Hello
World
you can also just define "<br />" as constant and reuse it
eg.
Public Const HtmlNewLine as string ="<br />"
Response.Write("Hello " & HtmlNewLine)
Response.Write("World " & HtmlNewLine)
it's :
vbnewline
for example
Msgbox ("Fst line" & vbnewline & "second line")
Try Environment.NewLine.
Your need to use the html/xhtml break character:
<br />
you can solve that problem in visual basic .net without concatenating your text, you can use this as a return type of your overloaded Tostring:
System.Text.RegularExpressions.Regex.Unescape(String.format("FirstName:{0} \r\n LastName: {1}", "Nordanne", "Isahac"))
In asp.net for giving new line character in string you should use <br> .
For window base application Environment.NewLine will work fine.
VbCr
Try that.
In this case, I can use vbNewLine, vbCrLf or "\r\n".
vbCrLf is a relic of Visual Basic 6 days. Though it works exactly the same as Environment.NewLine, it has only been kept to make the .NET api feel more familiar to VB6 developers switching.
You can call the String.Replace() function to avoid concatenation of many single string values.
MsgBox ("first line \n second line.".Replace("\n", Environment.NewLine))
Environment.NewLine or vbCrLf or Constants.vbCrLf
More information about VB.NET new line:
http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx
I had the need to store line breaks in a string in a SQL table and have them displayed in vb.NET. My solution was to include a string like this in my database:
"This is the first line{0}This is the second{0}This is the third"
In vb.NET, I processed the string like this before using it:
Label2.Text = String.Format(stringFromSQLquery, vbCrLf)
This replaces every occurance of {0} with vbCrLf