How to decode an integer in MVC5? - c#

I have a method that prints labels from a Web Application in MVC5, for this I am using the Neodinamyc plugin
The problem is that this method delivers the encoded parameters, for the first printerName parameter I was able to decode it in the following way and I succeeded
But I think that the second parameter called location that is of integer type gives it to me as hexadecimal or at least I think so, since the error says: "can not be converted from int to string"
I'm currently testing with
ubicacion = Convert.ToInt32(ubicacion);
and it does not work for me, it still has the same value
Why do I get this hexadecimal type number when I pass an integer?
How can I convert this parameter to an integer? any help for me?

The value doesn't need to be converted, it's already an integer.
Your tooltip is just displaying it in hexadecimal. You can change this by right-clicking the tooltip and unselecting Hexadecimal Display

Related

Set value to property c# datetime error format

When you select the data value in a property of the class the following error is generated "Input string not in a correct format".
I'm converting the date as in the image one it generates correct, but when assigning the error is generated.
I converted it to DateTime and also created it as new, but still the error happens.
The same occurs when use Scheduled = sd,
I am using asp.net core
1. Convert.ToDateTime is bothering me, although is not throwing this exception.
First of all, you should not call Convert.ToDateTime on a date time object.
it does not throw exception but it does not do any conversion also.
Refer MSDN Documentation here.
2. The exception has clear details that it is failed to convert a string to number
Stack trace is showing that "System.Number.StringToNumber" conversion failed.
So it is one of the other 3 properties which are string and you are trying to convert them to Int64 or long.
You would know which property by line number if you are running solution in debug mode.
3. If your values are integer strings (e.g. "10" ) even then if it is not working, then there is problem in culture settings of machine.
Please refer the complete thread here
It is saying that sometimes your conversion fails from "10" to integer value 10 because some registry settings are not set correctly. You can correct them by:
While it is possible to directly edit the value for the problem key,
the preferred method is to change the Regional Settings to a different
Region/Language and then reset it to the desired setting:
Open the Regional and Language Options applet from the Control Panel.
Note the Current Format.
Change the Current Format to English (Australian)
Click Apply.
Change the Current Format to the noted format, eg, English (American).
Click Apply and then click OK.
This should resolve your issue.

What is TryParse and Request.Form doing in this line of C# code?

What does this line of code do? I'm relatively new to C# and I've been trying to figure it out by reading about TryParse and Request.Form, however, I think a more comprehensive explanation would help me.
int.TryParse(Request.Form["yearhidden"], out year);
Request.Form provides the form element posted to the HTTP request.
int.TryParse attempts to take this value and convert it to an integer.
In this case, you're taking the "yearhidden" form element's value, and attempting to convert it to an integer, which gets set in the year variable.
Note that you'd typically check the return value of int.TryParse, and handle the case where a non-numeric value was passed into the yearhidden variable.
TryParse is taking the value from Request.Form["yearhidden"]
Request.Form["yearhidden"] is a form field in your html called yearhidden.
TryParse then attempts to parse it into an integer value. It returns True if it was successful, False if not.
The value is stored in the variable year
int.TryParse returns a boolean that represents whether or not the method was able to parse the first parameter, Request.Form["yearhidden"], into an integer.
If it is able to successfully parse the value, the value of the second parameter, year, will be set to the value.
Request.Form contains all of the information within an html form element that was sent in a given request.
out is a keyword that forces arguments to be passed by reference.
http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.80).aspx

Parsing Text from a Masked Text Box

I have a WinForms application written in C#
I have until recently many textboxes on my forms where the user inputs financial amounts. I have not incorporated any form of mask initially and whenever I need to work with the values input by the users I would Parse the text from each box into Decimal values with Decimal.Parse;
However I have been asked to make the textboxes look like financial amounts
i.e. £1,050.75 rather than 1050.75
I therefore started to change the textboxes into MaskedTextBox and gave them a Mask of £#,##0.00
However now each attempt to Parse the text from the MaskedTextBoxes gives an error 'Input string not in the correct format'.
How do I obtain the users input from the MaskedTextBox and parse into decimal format to work with?
Should I be using MaskedTextBox at all, or is there another way of showing a financial type formatting on the form, without effecting the Decimal.Parse method?
When you are getting the value from Maskedtextbox, it is taking the value as £#,##0.00 . so the symbol will not be converted to decimal. Try to remove the symbol and convert the value to decimal. like
string val= maskedTextBox1.Text.Replace("£","");
Decimal.Parse(val);
You can use a format option with AllowCurrencySymbol. It has to match the currency symbol of the culture. This code I converted from VB so I hope it's correct.
Application.CurrentCulture = New Globalization.CultureInfo("en-GB");
Decimal.Parse("£12,345.67", Globalization.NumberStyles.AllowThousands | Globalization.NumberStyles.AllowDecimalPoint | Globalization.NumberStyles.AllowCurrencySymbol);
Also, see this question if you don't want to change the culture:
Problem parsing currency text to decimal type
You can check MaskFull to see if text is properly enter and then apply anti-mask (by removing that, what your mask is adding).
Unfortunately, I am not aware about automated unmasking. But you can do something like:
if(maskedTextBox1.Mask)
{
var enteredText = maskedTextBox1.SubString(1).Replace(",", null); // remove pound symbol and commas
// ... parse as with normal TextBox
}

Cannot implicitly convert type 'RazorEngine.Text.RawString' to 'string'

Instead of having to define it every single time, I've done a subtemplate to determine whether a product is: in stock, out of stock, discontinued etc.
So in my main template, I want to display something based on the returned value.
When I use #Raw(Model.TemplateUtil.Subtemplate("SubAvailabilityCheck")) I get the proper value (ie. IN STOCK or COMING SOON etc.)
but when I add the following to declare my variable:
string stockCheck = Raw(Model.TemplateUtil.Subtemplate("SubAvailabilityCheck"));
It gives me: ERROR:Cannot implicitly convert type 'RazorEngine.Text.RawString' to 'string'
When I convert to string it outputs the entire html code with the default html comments that mark the beginning and end of the template etc. Where as RAW strips everything but the content.
Any way to work around this?
The Raw() function simply wraps the string you pass it in a RawString, which tells RazorEngine not to HTML-escape it.
As the error is trying to tell you, a RawString isn't a string and cannot be stored in a string variable.
Change the variable to be of type RawString.

Hex Conversion Error from Web Service Form call

I'm receiving an error which looks like it is due to an error using hexidecimal inputs in a uint field. It occurs on both versions of the web service I'm working on.
System.ArgumentException: Cannot convert 0x2 to System.UInt32.
Parameter name: type ---> System.FormatException: Input string was not in a correct format.
However, my coworker says that it works for him on a previous version of the web service I have, when he calls it using C++, but it doesn't work on the current version I'm working on.
Has anyone experienced this?
Are you using something like this in your code? If not, try to implement this (replace "CF01" with your input value):
int i = Convert.ToInt32("CF01", 16);
Edit:
For the particular case with the 0x prefix:
public int32 GetInt32FromHex(string h) {
h = h.substring(2, (h.length - 2));
return convert.ToInt32(h, 16);
}
This is quite an interesting error because it seems like the Parse method of UInt32 is only able to parse numbers of the form [ws][sign]digits[ws] per the documentation. There is a version of the method that can take NumberStyles flags see documentation. One of the values of this is AllowHexSpecifier which you'd think would allow the 0x. However if you read the documentation for both the Parse method and the NumberStyles unable to handle the 0x format at all. If you look at the it says:
If s is the string representation of a hexadecimal number, it cannot be preceded by any decoration (such as 0x or &h) that differentiates it as a hexadecimal number. This causes the conversion to fail.
The AllowHexSpecifier makes it so that only numbers of the form [ws]hexdigits[ws]
It seems like you are going to have to get rid of the leading 0x before parsing or use another method of parsing.
One way to do that, especially if there is a leading 0x is to do the following:
var value = UInt32.Parse( "0x2".TrimStart('0').TrimStart('x'));
You will have to be careful here to do checking to make sure you have the proper base though and you may need to use the NumberStyles.AllowHexSpecifier to parse correctly.

Categories