Why do I have to use .ToString()
for this code to work:
confirmedAcceptance = int.Parse(dr["confirmedAcceptance"].ToString());
This creates error:
confirmedAcceptance = int.Parse(dr["confirmedAcceptance"]);
Why do I have to use .ToString()
for this code to work:
confirmedAcceptance = int.Parse(dr["confirmedAcceptance"].ToString());
This creates error:
confirmedAcceptance = int.Parse(dr["confirmedAcceptance"]);
int.Parse tries to parse the certain Object into an INT. This can go as far as converting a string to INT (in which he calculates it) there is no certain way we know if it actually will work. Thats why we have TryParse which instantly catches the Programm if it actually not works
Convert uses a manually programmed Conversion. like Convert.ToInt() it has implicit Conversions for Double/Etc.
So if u know that it is supported, always use Convert, if u maybe dont know whats coming in and have to poker a bit. Use Parse and if best TryParse..
The ‘Parse’ method accepts a string as a parameter. https://learn.microsoft.com/en-us/dotnet/api/system.int32.parse?view=net-5.0 The ‘Convert’ method also accepts a string as a parameter. https://learn.microsoft.com/en-us/dotnet/api/system.convert.toint32?view=net-5.0
When you pass an integer to Int.Parse() or Convert.ToInt32() methods you are not passing the correct data type to the method.