Showing posts with label bug. Show all posts
Showing posts with label bug. Show all posts

Saturday, March 31, 2012

System.FormatException: Input string was not in a correct format.

Is this a bug or am I going crazy??
WILL NOT WORK: DateTime.Now.ToString("h")
THIS ONE WILL THOUGH: DateTime.Now.ToString(" h")
But that space gets put in front of the hour.
Anything with "h" will work, as long as it's not by itself ??Shouldn't it be "hh" ?

"hh" returns the hour with a zero in front if less than 10. "h" is supposed to return the hour without the zero.
DateTime.Now.ToString(" h")
that works, but it won't work if the space is not in front of the h.
Have you tried using string.Format to see if it gives you the same results?
Here's a good reference for the format strings:
http://www.stevex.org/CS/blogs/dottext/articles/158.aspx
If I think of anything else, I'll let you know.

M$ doesn't seem to want you to know the current hour without the 0 in front of it.
I triedString.Format("{0:h}", DateTime.Now) and it has the same problem that theDateTime.Now.ToString("h") method has.
For some reason, you cannot use "h" all by itself. Luckily the way I'm using it allows me to leave it as " h" (with a space in front).
I'm using it in a JavaScript file like so:
var curH = <%= DateTime.Now.ToString(" h") %>;
So, the JavaScript removes the space for me.
My guess is it's a bug. Thanks for you help anyways, but I'm not going to worry about it.

northflacomps wrote:

Is this a bug or am I going crazy??
WILL NOT WORK: DateTime.Now.ToString("h")
THIS ONE WILL THOUGH: DateTime.Now.ToString(" h")
But that space gets put in front of the hour.
Anything with "h" will work, as long as it's not by itself ??


Actually, that behaviour appears to be "by design" and is documented in the MSDN help.
"DateTimeFormatInfo Class... Only format patterns listed in the second table above [which is the table with h and H and so on] can be used to create custom patterns; standard format characters listed in the first table [which is a table with the abbreviation codes such as T => Long Time Pattern] cannot be used to create custom patterns. Custom patterns are at least two characters long"
It appears that since the standard patterns are sometimes single-characters in length, they decided to force custom patterns to be at least 2 characters in length, to avoid collisions and ambiguity. And so on.
For padding, when the desired custom format pattern is only one character long, they apparently want one to use the "%" sign.
Here is some sample code.

PrivateSub TestFormatStringButton_Click( _
ByVal senderAs System.Object, _
ByVal eAs System.EventArgs _
)Handles TestFormatStringButton.Click

'RTE: "Input string was not the correct format".
'Me.Response.Write(DateTime.Now.ToString("h"))

'RTE: "Input string was not in a correct format".
'Me.Response.Write(DateTime.Now.ToString("H"))

'RTE: "Format specifier was invalid".
'Me.Response.Write(DateTime.Now.Hour.ToString("H"))

'This works.
Me.Response.Write(DateTime.Now.ToString("%h"))

EndSub


HTH.
--Mark Kamoski


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctFormat.asp
h
Displays the hour as a number without leading zeros using the 12-hour clock (for example,1:15:15 PM). Use%h if this is the only character in your user-defined numeric format.
Thanks for that information. I looked at MSDN for the DateTime structure but didn't see anything about the "%". Seems kinda dumb to me.

Wednesday, March 28, 2012

System.IO.Stream to resulting file.

I'm kicking myself as I've had this 'bug' before, and I cannot remember what it was that resolved it.

Essentially I'm re-writing my upload control, and its started exhibiting a weird 'corruption' thing.

Essentially I have this code:

Dim FileLenAs Integer = FileData.LengthDim FileDataByes(FileLen)As Byte' Initialize the stream.Dim ResourceStreamAs System.IO.Stream = FileData' Read the file into the byte array. ResourceStream.Read(FileDataByes, 0, FileLen)Dim SavingStreamAs System.IO.FileStream = _ System.IO.File.Create(Server.MapPath(Me.__VirtualPath & FileName))' Copy the byte array into a string.For iAs Integer = 0To FileLen - 1 SavingStream.WriteByte(FileDataByes(i).ToString())Next' Close the streams. ResourceStream.Close() SavingStream.Close()
FileData is a stream of the file, either from a server side file, or from a httppostedfile. The file appears to save ok, the size of the file is correct. And when checking the properties the byte count looks exactly the same.

But the file is quite obviously corrected, it wont open. I'm guessing the issue is to do with encoding. But I can't remember what I did to resolve it. I'm looking at my old framework and the code at this point is identical. Could it be something to do server settings changing the encoding of POSTS?

Pre-cheers.

Stevo.
Scratch that, its the literal 5 minutes after submitted a thread you realise your obvious mistake...

' Create the fragment.Dim FragmentAs String = _ Engine.Convert.ToHexString(FileData, 0, 2)
DOH! can't believe I forget to 'rewind' the stream after getting a fragment identifier...

Cheers anyways!