XmlDocument.Save() String / Timestamp as part of the file name - asp.net

I am calling the XmlDocument.Save() method and attempting to append the String value below to my file name. The method call throws an error and is complaining about the format.
Any help would be appreciated! \m/ \m/
Dim strTimestamp As String = "2017-08-30T08:44:40-05:00"
XmlDocument.Save("C:\Temp\FileName_" & strTimestamp & ".xml")

The issue is obviously the special characters in the file name so I am going to use the following DateTime format.
Dim strTimestamp As String = DateTime.Now.ToString("yyyy-MM-dd--hh-mm-ss").

Related

string.split()(1) removes first character

I am running into an issue when i split a string on "_Pub" and get the back half of the string it removes the first character and I don't understand why or how to fix it unless i add the character back in
strFilePath = "/C:/Dev/Edge/_Publications/Ann Report/2013-2016/2016 Edge.pdf"
Dim relPath = strFilepath.Split("_Publications")(1)
lb.CommandArgument = relPath
returns Publications\Ann Report\2013-2016\2016 Edge.pdf
What you have as a delimiter is not a string array "string()" but a regular string. You need a string array to use a string as a delimiter. otherwise it takes the first char of your string.
https://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx
try this
Dim relPath = strFilepath.Split(new string() {"_Publications"}, StringSplitOptions.RemoveEmptyEntries)(1)
It appears that you want to get the part of the path starting at some directory. Splitting the path might not be such a good idea: imagine if there was a file "My_Publications_2017.pdf" in a directory "C:\Dev\Edge\_Publications". The split as you intended in the question would give the array of strings {"C:\Dev\Edge\", "\My", "_2017.pdf"}. As has been pointed out elsewhere, the String.Split you used doesn't do that anyway.
A more robust way would be to find where the starting directory's name is in the full path and get the substring of the path starting with it, e.g.:
Function GetRelativePath(fullPath As String, startingDirectory As String) As String
' Fix some errors in how the fullPath might be supplied:
Dim tidiedPath = Path.GetFullPath(fullPath.TrimStart("/".ToCharArray()))
Dim sep = Path.DirectorySeparatorChar
Dim pathRoot = sep & startingDirectory.Trim(New Char() {sep}) & sep
Dim i = tidiedPath.IndexOf(pathRoot)
If i < 0 Then
Throw New DirectoryNotFoundException($"Cannot find {pathRoot} in {fullPath}.")
End If
' There will be a DirectorySeparatorChar at the start - do not include it
Return tidiedPath.Substring(i + 1)
End Function
So,
Dim s = "/C:/Dev/Edge/_Publications/Ann Report/2013-2016/2016 Edge.pdf"
Console.WriteLine(GetRelativePath(s, "_Publications"))
Console.WriteLine(GetRelativePath(s, "\Ann Report"))
outputs:
_Publications\Ann Report\2013-2016\2016 Edge.pdf
Ann Report\2013-2016\2016 Edge.pdf
Guessing that you might have several malformed paths starting with a "/" and using "/" as the directory separator character instead of "\", I put some code in to mitigate those problems.
The Split() function is supposed to exclude the entire delimiter from the result. Could you re-check & confirm your input and output strings?

Split query string in VB.net doesn't find the delimiter

I am passing a query string between asp.net pages in a VB.net application. I recieve the string by doing the following:
Dim pagename_username As String = Request.QueryString("field1")
The query string made up of a URL and a user_id, and is sent via JavaScript.
The string is then split in the VB page, by doing the following:
Dim parts As String() = pagename_username.Split(New String() {"|"}, StringSplitOptions.None)
Dim pagename As String = parts(0)
Dim username As String = parts(1)
This works fine for the following query string:
field1=http://**********/default.aspx|1
But gives an outside the bounds of the array error for the following query string:
field1=http://**********/docstore/browse.aspx?docstoreid=0&docstoretypeid=2|1
My suspicion is that the second string is too long??
If so, how can I solve it?
If not, what is the problem?
In a query string, you separate the parameters by the '&' sign. For example, the following query string has two parameters:
www.someurl.com?param1=1&param2=2
In your case:
field1=http://**********/docstore/browse.aspx?docstoreid=0&docstoretypeid=2|1
Notice the delimiter (i.e. '|') is in the second query string parameter, which is docstoretypeid, and not included in the value of field1. Therefore, when you call Request.QueryString("field1"), you aren't getting the full string that contains the pipe delimiter. That's why, your split fails and you are getting the exception.

Using ParseQueryString function to get values from QueryString

I needed to get values out of a string in a QueryString format, that is such as: data1=value1&data2=value2...
Someone suggested I used HttpUtility.ParseQueryString to get the value, I've searched and searched but I can't find any documentation or implementation of this and the Microsoft documentation for it doesn't explain how it works, can someone tell me hwat I'm doing wrong, my code is below;
Public Shared Sub ProcessString(ByVal Vstring As String)
Dim var As NameValueCollection = HttpUtility.ParseQueryString(Vstring)
Dim vname As String = var.QueryString("VNAME")
End Sub
MSDN has an example.
' Parse the query string variables into a NameValueCollection.
Dim qscoll As System.Collections.Specialized.NameValueCollection = HttpUtility.ParseQueryString(Vstring)
Dim vname As String = qscoll("VNAME")
A NameValueCollection represents a collection of associated String keys and String values that can be accessed either with the key or with the index.
I found the problem, I was referencing everything fine but I was doing it in a separate .VB dependency file, once I did it on the actual code behind of the aspx form that solved the problem and it all worked. So now I'm just passing the string from Codebehind as a specialised.NameValue collection in to the function.

String / DateTime Conversion problem (asp.net vb)

I have this code:
Dim birthdaystring As String = MonthBirth.SelectedValue.ToString & "/" & DayBirth.SelectedValue.ToString & "/" & YearBirth.SelectedValue.ToString
Dim birthday As DateTime = Convert.ToDateTime(birthdaystring)
Which produces errors (String was not recognized as a valid DateTime.)
The string was "01/31/1963".
Any assistance would be appreciated.
Thanks.
The problem is probably that the culture used for parsing the date expects a MM/dd/yyyy format rather than dd/MM/yyyy format.
Instead of creating a string and parsing it, create the DateTime value directly:
Dim birthday As New DateTime(YearBirth.SelectedValue, MonthBirth.SelectedValue, DayBirth.SelectedValue)
Instead of creating a string which you later try to parse to a date try this:
Dim birthday As DateTime = new DateTime(_
CType(YearBirth.SelectedValue, Integer), _
CType(MonthBirth.SelectedValue, Integer), _
CType(DayBirth.SelectedValue, Integer))
Try changing it to:
DateTime.Parse(birthdaystring);
I'm guessing it will work, but if not - you can add a second parameter to the parse saying the format you input, i.e.:
DateTime.Parse(birthdaystring,"MM/dd/yyyy");

parsing URL ?a=b params with VB.NET from plain string

I am using a string to store key=value pairs, it has same format as QueryString
How can I easily parse it to array? or can I somehow use interal class QueryString("paramname") to access it?
You could use the System.Web.HttpUtility.ParseQueryString, this will give you a NameValueCollection. You can then access your values easily
Dim keynameValue As String = nameValueCollection.Get("Keyname")
' parse to pairs
Dim resultarray As Array = allresultdata.Split("&")
Dim result1 As String
Dim keyvals2 As Array
For Each result1 In resultarray
keyvals2 = result1.Split()
keyvals.Set(keyvals2(0), keyvals2(1))
Next
or can I somehow use interal class
QueryString("paramname") to access it?
If you need to access querystring from another class other then page code behind, use this :
HttpContext.Current.Request.QueryString("parameterName")
String.Split() probably will work for you....

Resources